-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCoDCharacterTools.py
More file actions
1228 lines (958 loc) · 48.8 KB
/
CoDCharacterTools.py
File metadata and controls
1228 lines (958 loc) · 48.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2023 Kyle Wood
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import webbrowser
import maya.cmds as cmds
import maya.mel as mel
import pymel.core as pymel
import CoDMayaTools
import SEToolsPlugin
def error( message ):
cmds.confirmDialog( title = "An error has occurred", message = message )
def confirm_dialog( message ):
cmds.confirmDialog( title = "Confirmation", message = message )
def prompt_dialog( title, message ):
result = cmds.promptDialog( title = title, message = message, button = ["Confirm", "Cancel"], defaultButton = "Confirm", cancelButton = "Cancel" )
if result == "Confirm":
return cmds.promptDialog( query = True, text = True )
return None
def remove_namespaces():
if pymel.listNamespaces( recursive = True, internal = False ):
namespaces = []
for namespace in pymel.listNamespaces( recursive = True, internal = False ):
namespaces.append( namespace )
for namespace in reversed( namespaces ):
pymel.namespace( removeNamespace = namespace, mergeNamespaceWithRoot = True )
namespaces[:] = []
def set_attribute( node, attribute, value ):
if cmds.objExists( node ):
if cmds.objExists( node + "." + attribute ):
cmds.setAttr( node + "." + attribute, value )
def get_groups():
groups = []
for node in get_joints() + get_meshes():
parent = cmds.listRelatives( node, parent = True, fullPath = True )
if not parent == None:
if len( parent ) > 0:
parent = parent[0].split( "|" )[1]
if parent not in groups:
groups.append( parent )
return groups
def get_joints():
return cmds.ls( type = "joint" )
def get_meshes():
return cmds.ls( "*SEModelMesh*" )
def get_skinclusters():
return cmds.ls( type = "skinCluster" )
def get_skincluster_for_mesh( mesh ):
return mel.eval( "findRelatedSkinCluster " + mesh )
def get_selection():
return cmds.ls( selection = True )
def enable_move_joints( enable = False ):
for skinCluster in get_skinclusters():
cmds.skinCluster( skinCluster, edit = True, moveJointsMode = enable )
def enable_x_ray( enable = True ):
if enable:
mel.eval( 'modelEditor -e -jointXray true modelPanel4' )
else:
mel.eval( 'modelEditor -e -jointXray false modelPanel4' )
def create_joint_attributes( joint ):
joint_attributes = {
"name": joint.split( "|" )[-1].split( ":" )[-1],
"parent": cmds.listRelatives( joint, parent = True )[0],
"translateX": cmds.getAttr( joint + ".translateX" ),
"translateY": cmds.getAttr( joint + ".translateY" ),
"translateZ": cmds.getAttr( joint + ".translateZ" ),
"rotateX": cmds.getAttr( joint + ".rotateX" ),
"rotateY": cmds.getAttr( joint + ".rotateY" ),
"rotateZ": cmds.getAttr( joint + ".rotateZ" ),
"jointOrientX": cmds.getAttr( joint + ".jointOrientX" ),
"jointOrientY": cmds.getAttr( joint + ".jointOrientY" ),
"jointOrientZ": cmds.getAttr( joint + ".jointOrientZ" ),
"translateXWorld": cmds.xform( joint, query = True, worldSpace = True, translation = True )[0],
"translateYWorld": cmds.xform( joint, query = True, worldSpace = True, translation = True )[1],
"translateZWorld": cmds.xform( joint, query = True, worldSpace = True, translation = True )[2],
"rotateXWorld": cmds.xform( joint, query = True, worldSpace = True, rotation = True )[0],
"rotateYWorld": cmds.xform( joint, query = True, worldSpace = True, rotation = True )[1],
"rotateZWorld": cmds.xform( joint, query = True, worldSpace = True, rotation = True )[2]
}
return joint_attributes
def create_new_rig( namespace, joints_with_attributes ):
# Deselect anything that's already selected
cmds.select( clear = True )
# Abort if something goes wrong
if len( cmds.ls( "*" + namespace + ":" + "*" ) ) > 0:
error( "This namespace already exists!" )
return
if len( namespace ) < 1:
error( "No namespace given!" )
return
if len( joints_with_attributes ) < 1:
error( "No joint attributes given!" )
return
# Create namespace for combined joints
cmds.namespace( add = namespace )
# Create new joints
for joint_with_attributes in joints_with_attributes:
cmds.select( clear = True )
cmds.joint( name = namespace + ":" + joint_with_attributes["name"] )
cmds.select( clear = True )
# Parent them
for joint_with_attributes in joints_with_attributes:
if "Joints" not in joint_with_attributes["parent"]:
cmds.parent( namespace + ":" + joint_with_attributes["name"], namespace + ":" + joint_with_attributes["parent"] )
cmds.select( clear = True )
# Group them
for joint_with_attributes in joints_with_attributes:
if "Joints" in joint_with_attributes["parent"]:
cmds.group( namespace + ":" + joint_with_attributes["name"], name = namespace + ":Joints" )
# Deselect after grouping
cmds.select( clear = True )
# Set attributes
for joint_with_attributes in joints_with_attributes:
if cmds.objExists( namespace + ":" + joint_with_attributes["name"] ):
set_attribute( namespace + ":" + joint_with_attributes["name"], "translateX", joint_with_attributes["translateX"] )
set_attribute( namespace + ":" + joint_with_attributes["name"], "translateY", joint_with_attributes["translateY"] )
set_attribute( namespace + ":" + joint_with_attributes["name"], "translateZ", joint_with_attributes["translateZ"] )
set_attribute( namespace + ":" + joint_with_attributes["name"], "rotateX", joint_with_attributes["rotateX"] )
set_attribute( namespace + ":" + joint_with_attributes["name"], "rotateY", joint_with_attributes["rotateY"] )
set_attribute( namespace + ":" + joint_with_attributes["name"], "rotateZ", joint_with_attributes["rotateZ"] )
set_attribute( namespace + ":" + joint_with_attributes["name"], "jointOrientX", joint_with_attributes["jointOrientX"] )
set_attribute( namespace + ":" + joint_with_attributes["name"], "jointOrientY", joint_with_attributes["jointOrientY"] )
set_attribute( namespace + ":" + joint_with_attributes["name"], "jointOrientZ", joint_with_attributes["jointOrientZ"] )
def get_joints_with_attributes( input = [] ):
joints_with_attributes = []
if len( input ) > 0:
joints = input
else:
joints = get_joints()
# Add the ones that have tag_origin first, because those are correct rotations
for joint in joints:
joint_path = cmds.ls( joint, long = True )[0]
if cmds.nodeType( joint_path ) == "joint":
if "tag_origin" in cmds.listRelatives( joint_path, parent = True, fullPath = True )[0]:
joint_attributes = create_joint_attributes( joint_path )
# Only append this joint if it hasn't been already
jointAdded = False
for index in range( len( joints_with_attributes ) ):
if joints_with_attributes[index]["name"] == joint_path.split( "|" )[-1].split( ":" )[-1]:
jointAdded = True
if not jointAdded:
joints_with_attributes.append( joint_attributes )
# Do it again without the condition, rest will be added now
for joint in joints:
joint_path = cmds.ls( joint, long = True )[0]
if cmds.nodeType( joint_path ) == "joint":
joint_attributes = create_joint_attributes( joint_path )
# Only append this joint if it hasn't been already
jointAdded = False
for index in range( len( joints_with_attributes ) ):
if joints_with_attributes[index]["name"] == joint_path.split( "|" )[-1].split( ":" )[-1]:
jointAdded = True
if not jointAdded:
joints_with_attributes.append( joint_attributes )
return joints_with_attributes
def get_mirror_joint_attributes( joint_to_mirror ):
# Deselect anything that's already selected
cmds.select( clear = True )
# Stop if joint doesn't exist
if not cmds.objExists( joint_to_mirror ):
error( "That joint doesn't exist!" )
return
suffix = None
# Store joint suffix
if joint_to_mirror.endswith( "_le" ):
suffix = "_le"
elif joint_to_mirror.endswith( "_left" ):
suffix = "_left"
elif joint_to_mirror.endswith( "_ri" ):
suffix = "_ri"
elif joint_to_mirror.endswith( "_right" ):
suffix = "_right"
if suffix == None:
error( "Can't mirror this joint!" )
return
# Create namespaces
cmds.namespace( add = "ORIGINAL" )
cmds.namespace( add = "MIRRORED" )
# Add namespaces to joints
for joint in get_joints():
cmds.rename( joint, "ORIGINAL:" + joint )
# Mirror the given joint
cmds.select( "ORIGINAL:" + joint_to_mirror )
mel.eval( 'mirrorJoint -mirrorXZ -mirrorBehavior -searchReplace "ORIGINAL:" "MIRRORED:"' )
# Store mirrored joint
mirrored_joint = cmds.ls( selection = True, long = True )[0]
cmds.select( clear = True )
# Create array for mirrored joints
joints = [ mirrored_joint ]
# Store all mirrored joints
for joint in cmds.listRelatives( mirrored_joint, allDescendents = True, fullPath = True ):
joint_path = cmds.ls( joint, long = True )[0]
joints.append( joint_path )
# Store attributes for mirrored joints
joints_with_attributes = get_joints_with_attributes( joints )
# Delete mirrored joints
cmds.delete( mirrored_joint )
# Remove namespaces
remove_namespaces()
# Flip the names
for joint_with_attributes in joints_with_attributes:
if suffix == "_le":
joint_with_attributes["name"] = joint_with_attributes["name"].replace( "_le", "_ri" )
elif suffix == "_left":
joint_with_attributes["name"] = joint_with_attributes["name"].replace( "_left", "_right" )
elif suffix == "_ri":
joint_with_attributes["name"] = joint_with_attributes["name"].replace( "_ri", "_le" )
elif suffix == "_right":
joint_with_attributes["name"] = joint_with_attributes["name"].replace( "_right", "_left" )
return joints_with_attributes
def get_rig_name( semodel ):
semodel_name = os.path.splitext( semodel )[0]
type = semodel_name.split( "_" )
rig_types = {
"male": "Male",
"female": "Female",
"fb": "[Fullbody]",
"vh": "[Viewhands]",
"vl": "[Viewlegs]",
"t5": "Black Ops 1",
"iw5": "Modern Warfare 3",
"t6": "Black Ops 2",
"iw6": "Ghosts",
"s1": "Advanced Warfare",
"t7": "Black Ops 3",
"iw7": "Infinite Warfare",
"h1": "ModernWarfareRemastered",
"s2": "World War 2",
"t8": "Black Ops 4",
"iw8": "Modern Warfare 2019",
"h2": "ModernWarfare2CR",
"t9": "Cold War",
"s4": "Vanguard",
"iw9": "Modern Warfare 2"
}
for index in range( len( type ) ):
for rig_type in rig_types:
if type[index] == rig_type:
type[index] = rig_types[rig_type]
return " ".join( type )
def get_animations_dir():
return cmds.internalVar( userScriptDir = True ) + "CoDCharacterTools/Animations/"
def get_targets_dir():
return cmds.internalVar( userScriptDir = True ) + "CoDCharacterTools/Targets/"
def import_target_rig( file_name ):
# Deselect anything that's already selected
cmds.select( clear = True )
# Create array for joints
joints = []
# Store current groups in outliner
existing_groups = get_groups()
# File path
file_path = get_targets_dir() + file_name
# Make sure the directory exists
if not os.path.isdir( get_targets_dir() ):
error( "Path: " + get_targets_dir() + "\n\nDoesn't exist." )
return
# Make sure the file exists
if not os.path.isfile( file_path ):
error( "File: " + file_path + "\n\nDoesn't exist." )
return
# Import the rig
cmds.file( file_path, i = True )
remove_namespaces()
# Delete the empty group that was created for meshes
if cmds.objExists( os.path.splitext( file_name )[0] ):
cmds.delete( os.path.splitext( file_name )[0] )
cmds.select( clear = True )
# Store joints attributes for the target rig
for group in get_groups():
if group not in existing_groups:
for joint in cmds.listRelatives( group, allDescendents = True, fullPath = True ):
joints.append( cmds.ls( joint, long = True )[0] )
# Store joints with attributes
joints_with_attributes = get_joints_with_attributes( joints )
# Delete after storing joints with attributes
for group in get_groups():
if group not in existing_groups:
if cmds.objExists( group ):
cmds.delete( group )
return joints_with_attributes
def any_node_exists( nodes ):
for node in nodes:
if cmds.objExists( node ):
return True
return False
def is_in_a_skincluster( input ):
# Checks if a joint or any joint in an array of joints is in a skincluster
if isinstance( input, list ):
for joint in input:
for skinCluster in get_skinclusters():
if joint in cmds.skinCluster( skinCluster, query = True, influence = True ):
return True
else:
for skinCluster in get_skinclusters():
if input in cmds.skinCluster( skinCluster, query = True, influence = True ):
return True
return False
def is_joint_in_rig( joints_with_attributes, joint ):
for joint_with_attributes in joints_with_attributes:
if joint_with_attributes["name"] == joint:
return True
return False
def mirror_joint( joint_to_mirror ):
# Make sure the joint exists
if not cmds.objExists( joint_to_mirror ):
error( joint_to_mirror + " doesn't exist." )
return
# Get mirrored joint rotations for the given joint
joints_with_attributes = get_mirror_joint_attributes( joint_to_mirror )
# Rotate them
for joint_with_attributes in joints_with_attributes:
if cmds.objExists( joint_with_attributes["name"] ):
set_attribute( joint_with_attributes["name"], "rotateX", joint_with_attributes["rotateX"] )
set_attribute( joint_with_attributes["name"], "rotateY", joint_with_attributes["rotateY"] )
set_attribute( joint_with_attributes["name"], "rotateZ", joint_with_attributes["rotateZ"] )
def lock_all_weights( enable = True ):
for joint in get_joints():
set_attribute( joint, "lockInfluenceWeights", enable )
def rotate_models():
# Deselect anything that's already selected
cmds.select( clear = True )
for joint_with_attributes in get_joints_with_attributes():
for joint in get_joints():
if joint_with_attributes["name"] == joint.split( "|" )[-1].split( ":" )[-1]:
# Exclude tag_origin, we don't want to move it
if "tag_origin" not in joint_with_attributes["name"]:
# If it's parent isn't a joint, we know it's a root joint. This is what we need to rotate & move
if not cmds.nodeType( cmds.listRelatives( joint, parent = True, path = True )[-1] ) == "joint":
cmds.select( joint )
cmds.rotate( joint_with_attributes["rotateXWorld"], joint_with_attributes["rotateYWorld"], joint_with_attributes["rotateZWorld"] )
cmds.move( joint_with_attributes["translateXWorld"], joint_with_attributes["translateYWorld"], joint_with_attributes["translateZWorld"] )
cmds.select( clear = True )
def semodel_unique_names():
# Deselect anything that's already selected
cmds.select( clear = True )
num = 0
for mesh in get_meshes():
num += 1
cmds.rename( mesh, "SEModelMesh_" + str( num ) )
def set_cosmetic_parent( show_message = True ):
if not cmds.objExists( "head" ):
error( "\"head\" does not exist." )
return
if not cmds.objExists( "XModelExporterInfo.Cosmeticbone" ):
CoDMayaTools.ShowWindow( "xmodel" )
if cmds.getAttr( "XModelExporterInfo.Cosmeticbone", "head" ) != "head":
cmds.setAttr( "XModelExporterInfo.Cosmeticbone", "head", type = "string" )
if show_message:
confirm_dialog( "\"head\" has now been set as the cosmetic parent." )
else:
error( "\"head\" is already the cosmetic parent." )
def set_skincluster_attributes():
for skinCluster in get_skinclusters():
set_attribute( skinCluster, "normalizeWeights", 1 )
set_attribute( skinCluster, "weightDistribution", 0 )
set_attribute( skinCluster, "maintainMaxInfluences", 1 )
set_attribute( skinCluster, "maxInfluences", 15 )
def set_vertex_colors():
for mesh in get_meshes():
cmds.select( mesh )
cmds.polyColorPerVertex( alpha = 1, rgb = ( 1, 1, 1 ), colorDisplayOption = True )
cmds.select( clear = True )
def set_zero_rotations( nodes ):
if len( nodes ) < 1:
error( "Nothing to rotate!" )
return
for node in nodes:
set_attribute( node, "rotateX", 0 )
set_attribute( node, "rotateY", 0 )
set_attribute( node, "rotateZ", 0 )
def transfer_weight( source, target ):
# Deselect anything that's already selected
cmds.select( clear = True )
# Make sure no animation is in the scene
if len( cmds.ls( "*SENotes*" ) ) > 0:
error( "You have an animation in the scene,\n\nReset scene first." )
return
if not cmds.objExists( source ):
error( source + " doesn't exist!" )
return
if not cmds.objExists( target ):
error( target + " doesn't exist!" )
return
# Set skincluster attributes
set_skincluster_attributes()
# Check we're in painting mode, if not switch to it.
if cmds.currentCtx() != "artAttrSkinContext":
mel.eval( "ArtPaintSkinWeightsTool" )
# Transfer weight
for mesh in get_meshes():
influences = cmds.skinCluster( get_skincluster_for_mesh( mesh ), query = True, influence = True )
if source not in influences:
continue
# Add target to skincluster if it isn't already
if target not in influences:
# Lock all weights
lock_all_weights( True )
# Add target to skincluster
cmds.skinCluster( get_skincluster_for_mesh( mesh ), edit = True, addInfluence = target )
# Unlock all weights
lock_all_weights( False )
mel.eval( "changeSelectMode -object; select " + mesh )
mel.eval( "artSkinSelectInfluence artAttrSkinPaintCtx " + source )
mel.eval( "skinCluster -edit -selectInfluenceVerts " + source + " " + get_skincluster_for_mesh( mesh ) )
mel.eval( "skinPercent -transformMoveWeights " + source + " -transformMoveWeights " + target + " " + get_skincluster_for_mesh( mesh ) )
mel.eval( "skinCluster -e -ri " + source + " " + get_skincluster_for_mesh( mesh ) )
# Set tool back to select
cmds.setToolTo( "selectSuperContext" )
enable_x_ray( False )
cmds.select( clear = True )
def merge_verts( mesh ):
if mesh not in get_meshes():
error( "This is not a valid mesh!" )
return
# Don't ask, this never works on the first try (Probably a Maya 2018 bug)
for index in range( 1, 5 ):
cmds.select( clear = True )
mel.eval( "select " + mesh )
# Mesh cleanup
mel.eval( 'polyCleanupArgList 4 { "0","1","1","0","0","0","0","0","0","1e-05","0","1e-05","0","1e-05","0","-1","0","0" };' )
# Delete all non-deformer history
mel.eval( "BakeAllNonDefHistory" )
# Merge vertices
mel.eval( 'polyMergeVertex -d 0.01 -am 1 -ch 1 ' + mesh )
cmds.select( clear = True )
def delete_non_target_joints():
# Deletes any joint not in the target rig and transfers the weights to the closest parent
target_rig = import_target_rig( "fb_t8_male_and_female.mb" )
# Joints to delete
joints_to_delete = []
# Add the joints we want to delete to the array (non-t7-joints)
for joint in get_joints():
if not is_joint_in_rig( target_rig, joint ):
if joint not in joints_to_delete:
joints_to_delete.append( joint )
# Loop them until they're done
while any_node_exists( joints_to_delete ):
for joint_to_delete in joints_to_delete:
if cmds.objExists( joint_to_delete ):
if cmds.listRelatives( joint_to_delete, allDescendents = True ) == None:
parent = cmds.listRelatives( joint_to_delete, parent = True )[0]
# Lock all weights
lock_all_weights( True )
# Make sure parent is also in the skincluster, if not then add it
for mesh in get_meshes():
influences = cmds.skinCluster( get_skincluster_for_mesh( mesh ), query = True, influence = True )
for joint in influences:
if joint == joint_to_delete:
if parent not in influences:
cmds.skinCluster( get_skincluster_for_mesh( mesh ), edit = True, addInfluence = parent, weightDistribution = 1, smoothWeights = 0.5, smoothWeightsMaxIterations = 2 )
# Unlock weights for joint and it's parent
set_attribute( parent, "lockInfluenceWeights", False )
set_attribute( joint_to_delete, "lockInfluenceWeights", False )
# Delete the joint
mel.eval( "select " + joint_to_delete )
mel.eval( "Delete" )
cmds.select( clear = True )
# Unlock all weights
lock_all_weights( False )
def rig_combiner( show_message = True ):
# Deselect anything that's already selected
cmds.select( clear = True )
# Make sure the scene isn't empty
if len( get_joints() + get_meshes() ) < 1:
error( "No SEModels could be found!" )
return
# Remove existing namespaces first
remove_namespaces()
# Set skincluster attributes
set_skincluster_attributes()
# Store joints attributes
joints_with_attributes = get_joints_with_attributes()
# Give the semodels unique names so we don't clash
semodel_unique_names()
# Rotate models to correct positions
rotate_models()
# Copy & paste meshes, to make duplicates
for group in get_groups():
if "COMBINED" not in group and "Joints" not in group and "group" not in group:
cmds.select( group )
mel.eval( "CopySelected" )
mel.eval( "PasteSelected copy" )
cmds.select( clear = True )
# Create new rig
create_new_rig( "COMBINED", joints_with_attributes )
# Delete construction history for original meshes
for group in get_groups():
if "COMBINED" not in group and "Joints" not in group and "group" not in group:
# Assume it's this group
for node in cmds.listRelatives( group, allDescendents = True ):
if node in get_meshes():
cmds.delete( node, constructionHistory = True )
# Remove original joints
for group in get_groups():
if "Joints" in group:
if "COMBINED" not in group and "group" not in group:
cmds.delete( group )
# Bind combined joints
for mesh in get_meshes():
cmds.select( clear = True )
if( "pasted__" in mesh ):
for joint in cmds.skinCluster( get_skincluster_for_mesh( mesh ), query = True, influence = True ):
cmds.select( "COMBINED:" + joint.split( "pasted__" )[-1], add = True )
cmds.select( mesh.split("pasted__")[-1], add = True )
cmds.skinCluster( get_selection(), mesh.split("pasted__")[-1], toSelectedBones = True, maximumInfluences = 15, obeyMaxInfluences = True, dropoffRate = 5.0, removeUnusedInfluence = False, normalizeWeights = 1 )
cmds.select( clear = True )
# Copy skinweights from old rigs to combined rig
for mesh in get_meshes():
if( "pasted__" in mesh ):
cmds.copySkinWeights( sourceSkin = get_skincluster_for_mesh( mesh ), destinationSkin = get_skincluster_for_mesh( mesh.split( "pasted__" )[-1] ), noMirror = True, surfaceAssociation = "closestPoint", influenceAssociation = "oneToOne" )
# Delete pasted groups
for group in get_groups():
if "group" in group:
cmds.delete( group )
# Remove namespaces
remove_namespaces()
# Delete unused nodes in hypershade
mel.eval( "MLdeleteUnused" )
# Set vertex colors
#set_vertex_colors()
# Deselect anything that's already selected
cmds.select( clear = True )
# Done
if show_message:
print( "Combined SEModels." )
confirm_dialog( "Combined SEModels." )
def rig_converter( target_rig, rig_name ):
# Deselect anything that's already selected
cmds.select( clear = True )
# Make sure the scene isn't empty
if len( get_joints() + get_meshes() ) < 1:
error( "No SEModels could be found!" )
return
# Set skincluster attributes
set_skincluster_attributes()
# Combine if the model is in parts and hasn't been combined
if len( cmds.ls( "Joints*" ) ) > 1:
rig_combiner( False )
# Joints to rename
rename_joints = {
"j_wristfronttwist1_le": "j_wristtwist_le",
"j_wristfronttwist1_ri": "j_wristtwist_ri",
"j_metaindex_le_1": "j_indexbase_le",
"j_metaindex_ri_1": "j_indexbase_ri",
"j_metaring_le_1": "j_ringbase_le",
"j_metaring_ri_1": "j_ringbase_ri",
"j_metapinky_le_1": "j_pinkybase_le",
"j_metapinky_ri_1": "j_pinkybase_ri"
}
# Rename joints
for joint in rename_joints:
if cmds.objExists( joint ):
cmds.rename( joint, rename_joints[joint] )
cmds.select( clear = True )
# Get rid of useless joints for viewhands
if "Viewhands" in rig_name:
if cmds.objExists( "j_clavicle_le" ) and cmds.objExists( "j_shoulder_le" ) and cmds.objExists( "j_clavicle_ri" ) and cmds.objExists( "j_shoulder_ri" ):
# Take them out of groups
cmds.select( clear = True )
cmds.parent( "j_shoulder_le", world = True )
cmds.select( clear = True )
cmds.parent( "j_shoulder_ri", world = True )
# Deleting like this using mel will transfer the weights, because sometimes we have weights on clavicle joints
cmds.select( clear = True )
cmds.select( "j_clavicle_le" )
mel.eval( "Delete" )
cmds.select( clear = True )
cmds.select( "j_clavicle_ri" )
mel.eval( "Delete" )
# Get rid of everything else
cmds.select( clear = True )
for group in get_groups():
if "Joints" in group:
cmds.delete( group )
# Create this again now and group because it's expected to be there
cmds.select( clear = True )
cmds.select( "j_shoulder_le", add = True )
cmds.select( "j_shoulder_ri", add = True )
cmds.group( name = "Joints" )
cmds.select( clear = True )
# Source rig
source_rig = get_joints_with_attributes()
# Move joints mode
enable_move_joints( True )
# Move all joints to world
for joint in get_joints():
cmds.parent( joint, world = True )
cmds.select( clear = True )
# Create T7 joints which don't exist
for joint_with_attributes in target_rig:
if not cmds.objExists( joint_with_attributes["name"] ):
# We don't need all of the T7 face joints
if not joint_with_attributes["parent"] == "head":
cmds.joint( name = joint_with_attributes["name"] )
cmds.select( clear = True )
# Parent target joints
for joint_with_attributes in target_rig:
if cmds.objExists( joint_with_attributes["name"] ):
if "Joints" not in joint_with_attributes["parent"]:
cmds.parent( joint_with_attributes["name"], joint_with_attributes["parent"] )
cmds.select( clear = True )
# Set translations, rotations for target joints
for joint_with_attributes in target_rig:
if cmds.objExists( joint_with_attributes["name"] ):
set_attribute( joint_with_attributes["name"], "translateX", joint_with_attributes["translateX"] )
set_attribute( joint_with_attributes["name"], "translateY", joint_with_attributes["translateY"] )
set_attribute( joint_with_attributes["name"], "translateZ", joint_with_attributes["translateZ"] )
set_attribute( joint_with_attributes["name"], "rotateX", joint_with_attributes["rotateX"] )
set_attribute( joint_with_attributes["name"], "rotateY", joint_with_attributes["rotateY"] )
set_attribute( joint_with_attributes["name"], "rotateZ", joint_with_attributes["rotateZ"] )
set_attribute( joint_with_attributes["name"], "jointOrientX", joint_with_attributes["jointOrientX"] )
set_attribute( joint_with_attributes["name"], "jointOrientY", joint_with_attributes["jointOrientY"] )
set_attribute( joint_with_attributes["name"], "jointOrientZ", joint_with_attributes["jointOrientZ"] )
# Parent source joints
for joint_with_attributes in source_rig:
for joint in get_joints():
if joint_with_attributes["name"] == joint:
if cmds.listRelatives( joint, parent = True ) == None:
if "Joints" not in joint_with_attributes["parent"]:
# For fullbody, the head joints need to be under "head" instead of "j_head"
if "Fullbody" in rig_name:
if joint_with_attributes["parent"] == "j_head":
joint_with_attributes["parent"] = "head"
# Parent them
if cmds.objExists( joint_with_attributes["parent"] ):
cmds.parent( joint_with_attributes["name"], joint_with_attributes["parent"] )
cmds.select( clear = True )
# Move eyes back to source positions
for joint_with_attributes in source_rig:
if "j_eyeball" in joint_with_attributes["name"]:
if not cmds.listRelatives( joint_with_attributes["name"], parent = True ) == None:
parent = cmds.listRelatives( joint_with_attributes["name"], parent = True )[0]
cmds.parent( joint_with_attributes["name"], world = True )
cmds.move( joint_with_attributes["translateXWorld"], joint_with_attributes["translateYWorld"], joint_with_attributes["translateZWorld"] )
cmds.parent( joint_with_attributes["name"], parent )
cmds.select( clear = True )
# Array for useless joints that we don't need to keep
useless = []
# Get rid of useless non-t7 joints with no weights
for joint in get_joints():
if cmds.listRelatives( joint, allDescendents = True ) == None:
if not is_joint_in_rig( target_rig, joint ):
if not is_in_a_skincluster( joint ):
if joint not in useless:
useless.append( joint )
# Delete them
if len( useless ) > 0:
for joint in useless:
if cmds.objExists( joint ):
cmds.delete( joint )
# Move joints mode
enable_move_joints( False )
# Zero all rotations
set_zero_rotations( get_joints() )
# Put them back in group
if "Viewhands" in rig_name:
set_attribute( "tag_view", "translateX", 0 )
set_attribute( "tag_view", "translateY", 0 )
set_attribute( "tag_view", "translateZ", 0 )
set_attribute( "tag_ads", "translateX", 0 )
set_attribute( "tag_ads", "translateY", 0 )
set_attribute( "tag_ads", "translateZ", 0 )
set_attribute( "tag_torso", "translateX", 0 )
set_attribute( "tag_torso", "translateY", 0 )
set_attribute( "tag_torso", "translateZ", 0 )
set_attribute( "j_shoulder_le", "translateX", -2.652 )
set_attribute( "j_shoulder_le", "translateY", 20.266 )
set_attribute( "j_shoulder_le", "translateZ", -10.853 )
set_attribute( "j_shoulder_ri", "translateX", -2.652 )
set_attribute( "j_shoulder_ri", "translateY", -20.266 )
set_attribute( "j_shoulder_ri", "translateZ", -10.853 )
set_attribute( "tag_weapon_left", "translateX", 35.329 )
set_attribute( "tag_weapon_left", "translateY", 45.914 )
set_attribute( "tag_weapon_left", "translateZ", -44.023 )
set_attribute( "tag_weapon_left", "jointOrientX", 41.248 )
set_attribute( "tag_weapon_left", "jointOrientY", 23.941 )
set_attribute( "tag_weapon_left", "jointOrientZ", 20.443 )
set_attribute( "tag_weapon_right", "translateX", 35.240 )
set_attribute( "tag_weapon_right", "translateY", -45.670 )
set_attribute( "tag_weapon_right", "translateZ", -44.124 )
set_attribute( "tag_weapon_right", "jointOrientX", -41.887 )
set_attribute( "tag_weapon_right", "jointOrientY", 24.888 )
set_attribute( "tag_weapon_right", "jointOrientZ", -21.812 )
cmds.parent( "tag_view", "Joints" )
elif "Fullbody" in rig_name:
cmds.parent( "tag_origin", "Joints" )
set_cosmetic_parent( False )
# Deselect anything that's already selected
cmds.select( clear = True )
# Joints we're going to delete, need to be handled differently because these have weights
joints_to_delete = []
# Add joints that we need to get rid of that are still in a skincluster to array
if cmds.objExists( "j_wrist_le" ) and cmds.objExists( "j_wrist_ri" ):
for joint in get_joints():
if cmds.objExists( joint ):
if not is_joint_in_rig( target_rig, joint ):
if joint not in joints_to_delete:
# For viewhands, get rid of everything that isn't a t7 joint
if "Viewhands" in rig_name:
joints_to_delete.append( joint )
# For fullbody get rid of anything under wrist, as some tags are used on the body for accessories and if we transfer those weights it might offset them
elif "Fullbody" in rig_name:
if joint in cmds.listRelatives( "j_wrist_le", allDescendents = True ) or joint in cmds.listRelatives( "j_wrist_ri", allDescendents = True ):
joints_to_delete.append( joint )
# Delete them
if len( joints_to_delete ) > 0:
# Keep doing this until none of them are influences
while is_in_a_skincluster( joints_to_delete ):
for joint_to_delete in joints_to_delete:
if cmds.objExists( joint_to_delete ):
if cmds.listRelatives( joint_to_delete, allDescendents = True ) == None:
parent = cmds.listRelatives( joint_to_delete, parent = True )[0]
if "j_wrist_" in parent:
# Lock all weights
lock_all_weights( True )
set_attribute( joint_to_delete, "lockInfluenceWeights", 0 )
set_attribute( parent, "lockInfluenceWeights", 0 )
cmds.select( joint_to_delete )
mel.eval( "Delete" )
cmds.select( clear = True )
if "j_wrist_" in parent:
# Unlock all weights
lock_all_weights( False )
# Deselect anything that's already selected
cmds.select( clear = True )
# Delete leftovers
for joint_to_delete in joints_to_delete:
if cmds.objExists( joint_to_delete ):
cmds.delete( joint_to_delete )
# Done
print( "Converted." )
confirm_dialog( "Converted." )
def add_wristtwist_influences():
# Make sure the scene isn't empty
if len( get_joints() + get_meshes() ) < 1:
error( "No SEModels could be found!" )
return
# Make sure no animation is in the scene
if len( cmds.ls( "*SENotes*" ) ) > 0:
error( "You have an animation in the scene,\n\nReset scene first." )
return
suffixes = ["le", "ri"]
# Make sure wristtwists exist
for suffix in suffixes:
for index in range( 1, 7 ):
if not cmds.objExists( "j_wristtwist" + str( index ) + "_" + suffix ):
error( "Can't find wristtwists\n\nDid you forget to convert first?\n\nOperation cancelled..." )
return
# Make sure they're not already attached
if is_in_a_skincluster( "j_wristtwist" + str( index ) + "_" + suffix ):
error( "The wristtwists are already attached to a skincluster!\n\nOperation cancelled..." )
return
# Set skincluster attributes
set_skincluster_attributes()
# Lock weights for joints that we don't want to be affected by these new influences
for joint in get_joints():
if "shoulder" not in joint:
if "elbow" not in joint:
if "wristtwist" not in joint:
if "wrist" not in joint:
set_attribute( joint, "lockInfluenceWeights", 1 )
# Add influences for the wristtwists
for suffix in suffixes:
for mesh in get_meshes():
for index in range( 1, 7 ):
# Make sure skincluster is suitable for this operation
influences = cmds.skinCluster( get_skincluster_for_mesh( mesh ), query = True, influence = True )
if ( "j_wristtwist_" + suffix ) in influences:
# Don't add the influence if there's fingers in this skincluster
if ( "j_thumb_" + suffix + "_1" ) in influences or ( "j_index_" + suffix + "_1" ) in influences or ( "j_mid_" + suffix + "_1" ) in influences or ( "j_ring_" + suffix + "_1" ) in influences or ( "j_pinky_" + suffix + "_1" ) in influences:
continue
cmds.skinCluster( get_skincluster_for_mesh( mesh ), edit = True, addInfluence = "j_wristtwist" + str( index ) + "_" + suffix, weightDistribution = 1, smoothWeights = 0.5, smoothWeightsMaxIterations = 2 )