-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSMPL_maya_plugin.py
executable file
·1300 lines (1101 loc) · 47.3 KB
/
SMPL_maya_plugin.py
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 2021 Meshcapade GmbH and Max Planck Gesellschaft. All rights reserved.
More information about the SMPL model research project is available here http://smpl.is.tue.mpg
For SMPL Model commercial use options, please visit https://meshcapade.com/infopages/licensing.html
For comments or questions, please email us at: [email protected]
Current versions supported:
---------------------------
Maya 2023+
About the Script:
----------------
The script displays a UI to apply the pose-corrective blendshapes for SMPL, SMPLH, SMPLX and STAR models in Maya. Load this plugin into Maya. It will create a window with 3 options:
1- Apply Pose Blend Shapes to Current Frame:
If you repose the model in Maya, then click this to
compute and apply the pose blend shapes in the current frame.
You can also choose whether or not to set the keyframes for the
pose blendshapes. Check the 'Reset Keyframes' checkbox if you
would like to lock blendShape values at given frame by setting
a keyframe.
2- Apply Pose Blend Shapes to Frames in above Range:
Specify a range of frames in an animation and then compute/apply
the pose blendshapes for all the frames in range. Check the
'Reset Keyframes' checkbox if you would like to lock blendShape
values at given frame range by setting a keyframe at each frame in the
given range.
3- Make Pose Blend Shapes fire interactively:
Click this button to turn on automatic pose-correctives for any SMPL(-/H/X) or STAR rigged mesh. Once this is set to ON, then any time you repose the SMPL model, the pose-correctives will automatically be applied to the mesh.
Always make sure to click on the mesh in the 3D view to select it before
using any of the functions in the plugin. Select only the mesh of the model
you want to update and then click the appropriate button on the UI.
"""
import maya.cmds as cmds
import maya.OpenMaya as oM
import maya.OpenMayaMPx as OpenMayaMPx
from functools import partial
import sys
# import pickle
from os.path import exists, split
import logging
# try to load any necessary Maya plugins here:
plugins = [
'mde_poseblends_driver',
'mde_py_poseblends_driver'
]
for plugin in plugins:
if cmds.pluginInfo(plugin, query=True, loaded=True):
continue
try:
cmds.loadPlugin(plugin)
except RuntimeError as e:
print(e)
continue
VERSION = '1.0.6'
SCRIPT_NAME = 'SMPL_maya_plugin'
# # TODO:
# # 1. Show error messages in plugin window instead of maya script editor
# try:
# import numpy as np
# except:
# python_version = sys.version_info
# sys.path.append('/usr/local/lib/python%d.%d/site-packages/' % (python_version.major, python_version.minor))
# import numpy as np
units = 'm'
if units == 'mm':
scale_up = 1000.
elif units == 'cm':
scale_up = 100.
else:
scale_up = 1.
class SMPL_generic_ops:
@staticmethod
def get_base_common_joint_names():
"""
These are joint names common to the SMPL flavors:
-SMPL
-SMPLH
-SMPLX
It is not a complete list of joints for any of those flavors,
though.
"""
# Initialize result with the SMPL default values
result = {
0: 'Pelvis',
1: 'L_Hip', 4: 'L_Knee', 7: 'L_Ankle', 10: 'L_Foot',
2: 'R_Hip', 5: 'R_Knee', 8: 'R_Ankle', 11: 'R_Foot',
3: 'Spine1', 6: 'Spine2', 9: 'Spine3', 12: 'Neck', 15: 'Head',
13: 'L_Collar', 16: 'L_Shoulder', 18: 'L_Elbow', 20: 'L_Wrist',
14: 'R_Collar', 17: 'R_Shoulder', 19: 'R_Elbow', 21: 'R_Wrist',
}
return result
@staticmethod
def get_joint_names(
MODEL_TYPE="SMPL"
):
"""
Given a SMPL MODEL_TYPE: return the SMPL:
-joint_names
:param MODEL_TYPE: a string, the result of get_MODEL_TYPE. Defaults to 'SMPL'.
:return: a dictionary keyed on integers with string elements representing the names of joints in the SMPL model.
"""
# Initialize result with the SMPL default values
result = SMPL_generic_ops.get_base_common_joint_names()
if(MODEL_TYPE == 'STAR'):
# read the joint names as if for SMPL:
result = SMPL_generic_ops.get_joint_names('SMPL')
return result
# Modify/augment result based on MODEL_TYPE:
if MODEL_TYPE == 'SMPLH':
result.update({
22: 'lindex0', 23: 'lindex1', 24: 'lindex2',
25: 'lmiddle0', 26: 'lmiddle1', 27: 'lmiddle2',
28: 'lpinky0', 29: 'lpinky1', 30: 'lpinky2',
31: 'lring0', 32: 'lring1', 33: 'lring2',
34: 'lthumb0', 35: 'lthumb1', 36: 'lthumb2',
37: 'rindex0', 38: 'rindex1', 39: 'rindex2',
40: 'rmiddle0', 41: 'rmiddle1', 42: 'rmiddle2',
43: 'rpinky0', 44: 'rpinky1', 45: 'rpinky2',
46: 'rring0', 47: 'rring1', 48: 'rring2',
49: 'rthumb0', 50: 'rthumb1', 51: 'rthumb2'
})
elif MODEL_TYPE == 'SMPLX':
# read the joint names as if for SMPLH:
result_SMPLH = SMPL_generic_ops.get_joint_names('SMPLH')
result.update({
22: 'Jaw', 23: 'L_eye', 24: 'R_eye'
})
# use the same values as in SMPLH from 22 -> 52(ie the finger joints),
# but with key incremented by 3(ie to make room for the Jaw and
# L|R_eye joints added immediately above):
[result.update({ii + 3:result_SMPLH[ii]}) for ii in range(22, max(result_SMPLH.keys()) + 1)]
else:
result.update({
22: 'L_Hand', 23: 'R_Hand',
})
return result
@staticmethod
def get_MODEL_TYPE(
joints,
num_blend_shapes
):
"""
Given a list of SMPL joint names: return the SMPL:
-MODEL_TYPE
:param joints: a list of joint names of joints in the scene. This should be a list of unique(ie non-repeating) names.
:return: string representing the SMPL MODEL_TYPE
"""
num_joints = len(set(joints))
logging.debug('num_joints:', num_joints)
num_blend_shapes_per_joint = float(num_blend_shapes) / float(num_joints)
logging.debug('num_blend_shapes:', num_blend_shapes)
logging.debug('num_blend_shapes_per_joint:', num_blend_shapes_per_joint)
if num_joints <= 24:
if(num_blend_shapes_per_joint < 6):
# for STAR: should be 4 blendShape targets per joint, but no
# blendShape targets are defined for the root joint.
# for any SMPL-related models: should be 9 blendShape targets
# per joint, but no blendShape targets are defined for
# the root joint.
# So: if it's less than 6 targets/joint: STAR.
MODEL_TYPE = 'STAR'
else:
MODEL_TYPE = 'SMPL'
elif num_joints <= 52:
MODEL_TYPE = 'SMPLH'
elif num_joints <= 55:
MODEL_TYPE = 'SMPLX'
else:
raise TypeError("Model type is not supported")
return MODEL_TYPE
@staticmethod
def get_root_joint_prefix(joints):
"""
Given a list of SMPL joint names: return the SMPL:
-root joint_prefix
:param joints: a list of joint names.
:return: string representing the SMPL root joint_prefix
"""
joint_prefix = None
rootJoint = [b for b in joints if 'root' in b]
if not rootJoint:
rootJoint = [b for b in joints if 'Pelvis' in b]
joint_prefix = rootJoint[0].replace('_Pelvis', '')
else:
joint_prefix = rootJoint[0].replace('_root', '')
return joint_prefix
@staticmethod
def get_joint_info(
joints,
num_blend_shape_targets
):
"""
Given a list of joint names: return the SMPL:
-MODEL_TYPE
-joint_names
-root_joint_prefix
:param joints: a list of joint names.
:return: a dictionary containing the joint info.
"""
MODEL_TYPE = SMPL_generic_ops.get_MODEL_TYPE(
joints,
num_blend_shape_targets
)
logging.debug('MODEL_TYPE:', MODEL_TYPE)
joint_names = SMPL_generic_ops.get_joint_names(
MODEL_TYPE
)
root_joint_prefix = SMPL_generic_ops.get_root_joint_prefix(joints)
result = {}
result.update({'root_joint_prefix':root_joint_prefix})
result.update({'MODEL_TYPE':MODEL_TYPE})
result.update({'joint_names':joint_names})
return result
class maya_ops:
@staticmethod
def set_MMatrix_cell(
matrix,
value,
row,
column
):
oM.MScriptUtil.setDoubleArray(
matrix[row],
column,
value
)
@staticmethod
def from_1x16_list_to_4x4_MMatrix(
list_1x16,
transpose = False
):
result = oM.MMatrix()
index1D = 0
for ii in range(0, 4):
for jj in range(0, 4):
row_index = ii
column_index = jj
if(transpose):
row_index = jj
column_index = ii
maya_ops.set_MMatrix_cell(
result,
list_1x16[index1D],
row_index,
column_index
)
index1D += 1
return result
@staticmethod
def get_LUT_deformer_to_geo(
deformer_type='blendShape'
):
deformerLUT = {}
all_deformers_of_type = cmds.ls(type=deformer_type)
deformer_geomGetter = partial(cmds.deformer, q=True, geometry=True)
[deformerLUT.update({x: deformer_geomGetter(x)}) for x in all_deformers_of_type]
return deformerLUT
@staticmethod
def get_LUT_geo_to_deformer(
deformer_type='blendShape',
deformer_LUT=None
):
if not deformer_LUT:
deformer_LUT = maya_ops.get_LUT_deformer_to_geo(
deformer_type=deformer_type
)
deformer_reverse_LUT = {}
for deformer, geometries in deformer_LUT.items():
for current_geo in geometries:
if current_geo not in deformer_reverse_LUT:
deformer_reverse_LUT.update({current_geo: set()})
deformer_reverse_LUT[current_geo].add(deformer)
return deformer_reverse_LUT
@staticmethod
def get_info_geo(
maya_geo
):
"""
:param maya_geo: a transform parent of a geo, or a geo shape child in Maya.
:return: a dictionary with the transform parent and all the geo shape children
"""
geo_parent = None
if cmds.nodeType(maya_geo) == 'transform':
# maya_geo is a transform, so assume it is the parent of geo geometry shape nodes:
geo_parent = maya_geo
else:
# maya_geo is not a transform so assume it is the sibling of geo geometry shape nodes:
geo_parent = cmds.listRelatives(maya_geo, parent=True)[0]
geo_shapes = cmds.listRelatives(geo_parent, children=True, shapes=True)
geo_shapes = cmds.ls(geo_shapes, type='geometryShape')
shape_type = None
if geo_shapes and len(geo_shapes) > 0:
shape_type = cmds.nodeType(geo_shapes[0])
result = {}
result.update({'parent': geo_parent})
result.update({'shapes': geo_shapes})
result.update({'shape_type': shape_type})
return result
@staticmethod
def get_associated_deformer(
maya_geo,
deformer_type='blendShape'
):
objset = cmds.listConnections(maya_geo, type='objectSet')
result = None
if objset:
result = cmds.listConnections(objset, type=deformer_type)
if result and cmds.objExists(result[0]):
result = result[0]
return result
# if the evaluation is here: it means a deformer
# could not be found from deformer membership sets(ie 'objset').
# Starting ~Maya 2020: Autodesk started doing away
# with sets membership preferring "componentTags".
# ???
# Anyway: we'll see if we can find a command to
# just return the deformer either way:
deformer_reverse_LUT = maya_ops.get_LUT_geo_to_deformer(
deformer_type=deformer_type
)
geo_info = maya_ops.get_info_geo(
maya_geo
)
geo_parent_and_shapes = [geo_info['parent']]
geo_parent_and_shapes.extend(geo_info['shapes'])
geo_parent_and_shapes_set = set(geo_parent_and_shapes)
result = None
for elem in geo_parent_and_shapes_set:
if elem not in deformer_reverse_LUT:
continue
# at this point in the evaluation: elem is in deformer_reverse_LUT:
# so: return the deformer at this dictionary element:
result = list(deformer_reverse_LUT[elem])
if result:
result = result[0]
break
return result
@staticmethod
def has_no_deformer_of_type(
maya_geo,
deformer_type='blendShape'
):
"""
:param maya_geo: transform parent or child geo shape of a Maya geo
:param deformer_type: the type of the deformer the user wants to check if maya_geo is deformed by.
:return:
True if maya_geo is not deformed by any deformer of deformer_type
False otherwise
"""
candidate = maya_ops.get_associated_deformer(
maya_geo,
deformer_type=deformer_type
)
result = True
if candidate and cmds.objExists(candidate) and cmds.nodeType(candidate) == deformer_type:
result = False
return result, candidate
@staticmethod
def is_not_skinned(maya_mesh):
result = maya_ops.has_no_deformer_of_type(
maya_mesh,
deformer_type='skinCluster'
)
return result
@staticmethod
def has_no_blendShape(maya_mesh):
result = maya_ops.has_no_deformer_of_type(
maya_mesh,
deformer_type='blendShape'
)
return result
@staticmethod
def get_LUT_blendShape_to_geo():
result = maya_ops.get_LUT_deformer_to_geo(
deformer_type='blendShape'
)
return result
@staticmethod
def get_LUT_skinCluster_to_geo():
result = maya_ops.get_LUT_deformer_to_geo(
deformer_type='skinCluster'
)
return result
@staticmethod
def get_LUT_geo_to_blendShape(
blendShape_LUT=None
):
result = maya_ops.get_LUT_geo_to_deformer(
deformer_type='blendShape',
deformer_LUT=blendShape_LUT
)
return result
@staticmethod
def get_LUT_geo_to_skinCluster(
skinCluster_LUT=None
):
result = maya_ops.get_LUT_geo_to_deformer(
deformer_type='skinCluster',
deformer_LUT=skinCluster_LUT
)
return result
@staticmethod
def get_info_mesh(
maya_mesh
):
"""
:param maya_mesh:
:return: a dictionary of information about maya_mesh(transform parent, mesh shapes)
"""
result = maya_ops.get_info_geo(
maya_mesh
)
if result['shape_type'] != 'mesh':
logging.error('No mesh shapes found for geo: "%s"', maya_mesh)
result = None
return result
@staticmethod
def getVtxPos(shapeNode):
"""
Get the vertices of a maya mesh 'shapeNode'
:param shapeNode: name of maya mesh
:return vertices of the maya mesh object
"""
vtxWorldPosition = []
vtxIndexList = cmds.getAttr(shapeNode + ".vrts", multiIndices=True)
for i in vtxIndexList:
current_vtx = str(shapeNode) + ".pnts[" + str(i) + "]"
curPointPosition = cmds.xform(
current_vtx,
query=True,
translation=True,
worldSpace=True
) # [1.1269192869360154, 4.5408735275268555, 1.3387055339628269]
vtxWorldPosition.append(curPointPosition)
return vtxWorldPosition
def get_SMPL_blendShape_weight_attr_alias(
start_weight_index,
offset_index,
pattern = 'Pose%03d'
):
"""
Any time this library needs the name of a blendShape's weight attr: it should get it here.
The default looks like:
Pose(start_weight_index + offset_index)
:param start_weight_index: the 0-based index from which to start the current weights.
:param offset_index: integer offset from start_weight_index
:param pattern: a string with an integer conversion somewhere which will be replaced by the sum of start_weight_index and offset_index.
:return: A string representing the aliased name of an element of blendShape_node.weights[<some index>].
"""
result = pattern % (start_weight_index + offset_index)
return result
class mde_poseblends_driver_ops:
@staticmethod
def connect_input_attrs_joint_single(
mde_poseblends_driver_node,
joint,
joint_index,
MODEL_TYPE = 'SMPL'
):
"""
Maya joint matrices -> mde_poseblends_driver_node.inputJoint[joint_index]
Connect joint(a Maya transform) to the joint_indexth entry of the inputJoint attribute of
mde_poseblends_driver_node
:param joint: the name of a transform node in the Maya scene.
:param joint_index: the index into the input attribute: mde_poseblends_driver_node + '.' + 'inputJoint'
:param mde_poseblends_driver_node: the node into whose inputs to connect joint's matrix(ces) outputs.
:return: None
"""
obj_attr = mde_poseblends_driver_node + '.' + 'inputModelType'
possible_types = set(mde_poseblends_driver_ops.possible_node_types())
has_model_type_attr = True
has_model_type_attr = has_model_type_attr and cmds.objExists(mde_poseblends_driver_node)
has_model_type_attr = has_model_type_attr and (cmds.nodeType(mde_poseblends_driver_node) in possible_types)
has_model_type_attr = has_model_type_attr and cmds.objExists(obj_attr)
# if(has_model_type_attr == False and MODEL_TYPE == 'STAR'):
# error_string = ""
# error_string += "It seems like you are trying to make "
# error_string += "poseblends drivers for a STAR model, but "
# error_string += "you are using an older version of "
# error_string += "mde_poseblends_driver which does not "
# error_string += "support STAR."
# logging.error(error_string)
# return
if(has_model_type_attr):
model_type_value = None
if(MODEL_TYPE == 'STAR'):
model_type_value = 1
else:
# SMPL:
model_type_value = 0
cmds.setAttr(
obj_attr,
model_type_value
)
base_dest_obj_attr = mde_poseblends_driver_node + '.' + 'inputJoint' + '[' + str(joint_index) + ']'
# fill source/dest attrs:
source_dest_attr_pairs = [
{'source':'matrix', 'dest':'inputJointMatrix'},
{'source':'worldMatrix', 'dest':'inputJointWorldMatrix'},
{'source':'parentMatrix', 'dest':'inputJointWorldParentMatrix'},
{'source':'parentInverseMatrix', 'dest':'inputJointWorldParentInverseMatrix'}
]
# connectAttrs:
num_attr_pairs = len(source_dest_attr_pairs)
for ii in range(0, num_attr_pairs):
source_attr = source_dest_attr_pairs[ii]['source']
dest_attr = source_dest_attr_pairs[ii]['dest']
source_obj_attr = joint + '.' + source_attr
dest_obj_attr = base_dest_obj_attr + '.' + dest_attr
cmds.connectAttr(
source_obj_attr,
dest_obj_attr,
force = True
)
@staticmethod
def connect_output_attrs_joint_single(
mde_poseblends_driver_node,
joint_index,
blendShape_node,
blendShape_joint_weight_start_index,
MODEL_TYPE = 'SMPL'
):
"""
mde_poseblends_driver_node.outputJoint[joint_index] -> Maya blendShape_node's weights
Connect mde_poseblends_driver_node's joint_indexth outputJoint to drive the blendShape_node's
weights starting at blendShape_joint_weight_start_index.
:param mde_poseblends_driver_node: the node whose outputs to drive blendShape_node's weights inputs.
:param joint_index: the index into the output attribute: mde_poseblends_driver_node + '.' + 'outputJoint'
:param blendShape_node: the blendShape node whose input weights to drive with mde_poseblends_driver_node's outputs.
:param blendShape_joint_weight_start_index: the index of blendShape_node's weights to start connecting to.
:return: None
:return: None
"""
base_source_obj_attr = mde_poseblends_driver_node + '.' + 'outputJoint' + '[' + str(joint_index) + ']'
weights_per_joint = None
if('SMPL' in MODEL_TYPE):
weights_per_joint = 9
else:
# STAR:
weights_per_joint = 4
source_dest_attr_pairs = []
# fill source/dest attrs:
for ii in range(0, weights_per_joint):
source_attr = 'outputJointBlendShapeWeights' + '[' + str(ii) + ']'
blendShape_weight_attr = get_SMPL_blendShape_weight_attr_alias(
blendShape_joint_weight_start_index,
ii
)
dest_attr = blendShape_weight_attr
current_value = {}
current_value.update({'source':source_attr})
current_value.update({'dest':dest_attr})
source_dest_attr_pairs.append(current_value)
# connectAttrs:
num_attr_pairs = len(source_dest_attr_pairs)
for ii in range(0, num_attr_pairs):
source_attr = source_dest_attr_pairs[ii]['source']
dest_attr = source_dest_attr_pairs[ii]['dest']
source_obj_attr = base_source_obj_attr + '.' + source_attr
dest_obj_attr = blendShape_node + '.' + dest_attr
existing_source_obj_attr = cmds.listConnections(dest_obj_attr, p = True, source = True, destination = False)
if(existing_source_obj_attr and len(existing_source_obj_attr) > 0):
existing_source_obj_attr = existing_source_obj_attr[0]
cmds.disconnectAttr(
existing_source_obj_attr,
dest_obj_attr
)
cmds.connectAttr(
source_obj_attr,
dest_obj_attr,
force=True
)
@staticmethod
def connect_input_and_output_attrs_joint_single(
mde_poseblends_driver_node,
joint,
joint_index,
blendShape_node,
blendShape_joint_weight_start_index,
MODEL_TYPE = 'SMPL'
):
"""
Maya joint matrices -> mde_poseblends_driver_node.inputJoint[joint_index],
mde_poseblends_driver_node.outputJoint[joint_index] -> blendShape_node's weights
:param joint: the name of a transform node in the Maya scene.
:param joint_index:
the index into the input attribute: mde_poseblends_driver_node + '.' + 'inputJoint'
the index into the output attribute: mde_poseblends_driver_node + '.' + 'outputJoint'
:param mde_poseblends_driver_node:
the node into whose inputs to connect joint's matrix(ces) outputs.
the node whose outputs to drive blendShape_node's weights inputs.
:param blendShape_node: the blendShape node whose input weights to drive with mde_poseblends_driver_node's outputs.
:param blendShape_joint_weight_start_index: the index of blendShape_node's weights to start connecting to.
:return: None
"""
# connect:
# -joint's matrices
# to
# -mde_poseblends_driver_node's inputJoint[joint_index]:
mde_poseblends_driver_ops.connect_input_attrs_joint_single(
mde_poseblends_driver_node,
joint,
joint_index,
MODEL_TYPE = MODEL_TYPE
)
# connect:
# -mde_poseblends_driver_node's outputJoint[joint_index]
# to
# -blendShape_node's weights:
mde_poseblends_driver_ops.connect_output_attrs_joint_single(
mde_poseblends_driver_node,
joint_index,
blendShape_node,
blendShape_joint_weight_start_index,
MODEL_TYPE = MODEL_TYPE
)
@staticmethod
def possible_node_types():
mpbd_candidates = [
# C++ version:
'mde_poseblends_driver',
# Python version:
'mde_py_poseblends_driver'
]
return mpbd_candidates
@staticmethod
def get_node_type_to_use():
"""
# So: there's two implementations of mde_poseblends_driver:
# 1. mde_poseblends_driver(C++)
# 2. mde_py_poseblends_driver(Python)
#
# prefer the C++ one if it is loaded, but use the Python one otherwise:
#
"""
mpbd_candidates = mde_poseblends_driver_ops.possible_node_types()
result = None
for candidate in mpbd_candidates:
candidate_loaded = cmds.pluginInfo(candidate, q = True, loaded = True)
if(candidate_loaded == False):
continue
# if the evaluation is here: it means the candidate is loaded.
# So: use it:
result = candidate
break
return result
@staticmethod
def create_and_connect(
joints,
joint_indices,
blendShape_node,
mode = 1,
MODEL_TYPE = 'SMPL'
):
"""
Create mde_poseblends_driver nodes to drive blendShape_node weights based on the rotations of the joints.
:param joints: the name of a transform nodes in the Maya scene.
:param blendShape_node: the blendShape node whose input weights to drive with the (one or more) mde_poseblends_driver_node's outputs.
:param mode:
0: create a single mde_poseblends_driver node, and drive all the blendShape weights with it.
This may not be the best option for performance because I think the whole mde_poseblends_driver node
will have to re-evaluate even if only one of the input joints changes.
1: create a mde_poseblends_driver's node for each joint. I think this will be the preferable
mode, because it leaves the parallel-ization to the Maya evaluation graph, and that will make it
where if only one joint changes, only that related mde_poseblends_driver node will be re-evaluated.
The rest will just return whatever value is cached on their outputs.
:return: None
"""
mpbd_node_type = mde_poseblends_driver_ops.get_node_type_to_use()
if not mpbd_node_type:
logging.error('there is no mde_poseblends_driver plugin loaded. Returning now without doing anything.')
return
num_joints = len(joints)
num_nodes_to_create = 0
mde_poseblends_driver = []
if(mode == 0):
num_nodes_to_create = 1
else:
num_nodes_to_create = num_joints
for ii in range(0, num_nodes_to_create):
current_node = cmds.createNode(mpbd_node_type, name = mpbd_node_type + "#")
mde_poseblends_driver.append(current_node)
logging.debug('mde_poseblends_driver: ' + str(mde_poseblends_driver))
weights_per_joint = None
if('SMPL' in MODEL_TYPE):
# SMPL family(SMPL, SMPLX, SMPLH):
# use rotation part of matrix(3x3)
# to generate blendShape weight drivers:
weights_per_joint = 9
else:
# STAR:
# use quaternion rotation representation(x, y, z, w)
# to generate blendShape weight drivers:
weights_per_joint = 4
if(mode == 0):
logging.debug(': in if(mode == 0) block: ')
# connect all the joints to a single mde_poseblends_driver node:
current_node = mde_poseblends_driver[0]
for ii in range(0, num_joints):
current_joint_index = joint_indices[ii]
current_joint = joints[ii]
current_blendShape_joint_weight_start_index = (weights_per_joint * current_joint_index)
mde_poseblends_driver_ops.connect_input_and_output_attrs_joint_single(
current_node,
current_joint,
current_joint_index,
blendShape_node,
current_blendShape_joint_weight_start_index,
MODEL_TYPE = MODEL_TYPE
)
else:
logging.debug(': in if(mode == 1) block: ')
# connect all the joints to their own mde_poseblends_driver node(ie one mde_poseblends_driver node per joint):
for ii in range(0, num_joints):
current_joint_index = joint_indices[ii]
current_node = mde_poseblends_driver[ii]
current_joint = joints[ii]
current_blendShape_joint_weight_start_index = (weights_per_joint * current_joint_index)
mde_poseblends_driver_ops.connect_input_and_output_attrs_joint_single(
current_node,
current_joint,
current_joint_index,
blendShape_node,
current_blendShape_joint_weight_start_index,
MODEL_TYPE
)
class ui:
def __init__(self, winName='SMPL_model_maya_script'):
self.winTitle = 'SMPL - Rigging & Pose Corrections Toolbox for Maya'
self.winName = winName
self.j_names = SMPL_generic_ops.get_base_common_joint_names()
@staticmethod
def get_maya_mesh_from_selection():
## Get selection
selection = cmds.ls(
selection=True,
showType=True
)
## Check if selected object is only one mesh & return if not
if len(selection) != 2:
print('\nError: select only the mesh')
return
if selection[1] != 'transform' and selection[1] != 'mesh':
print("\nError: Please select a mesh object")
return
maya_mesh = selection[0] if selection[1] == 'mesh' else cmds.listRelatives(selection[0], type='mesh')[0]
return maya_mesh
def create(self):
if cmds.window(self.winName, exists=True):
cmds.deleteUI(self.winName)
cmds.window(self.winName, title=self.winTitle)
cmds.columnLayout(adjustableColumn=True, rowSpacing=5, columnAlign='center')
cmds.separator(height=5, style='none')
## POSE BLEND SHAPES FOR CURRENT FRAME
cmds.rowLayout(numberOfColumns=2, columnAttach=[(1, 'left', 20), (2, 'both', 10)])
self.frame_checkbox = cmds.checkBox(label=' Reset \n Keyframes', align='center', value=True)
#current_frame_func = lambda self: self.applyBlendshapes(self)
#self.bttn_blend = cmds.button(label='Apply Pose Blend Shapes to\nCurrent Frame',
# c=current_frame_func, width=170, height=50)
self.bttn_blend_current = cmds.button(label='Apply Pose Blend Shapes to\nCurrent Frame',
c=self.applyBlendshapes, width=170, height=50)
cmds.setParent('..')
cmds.separator(height=10, style='in')
## POSE BLENDSHAPES FOR RANGE
cmds.rowLayout(numberOfColumns=1, columnAttach=[(1, 'both', -30)])
self.framesField = cmds.intFieldGrp(numberOfFields=2, label='Frame Range', value1=0, value2=10)
cmds.setParent('..')
cmds.rowLayout(numberOfColumns=2, columnAttach=[(1, 'left', 20), (2, 'both', 10)])
self.range_checkbox = cmds.checkBox(label=' Reset \n Keyframes', align='center', value=True)
#range_frame_func = partial(ui.applyBlendshapes, self, use_timeline=True)
range_frame_func = lambda *args: self.applyBlendshapes(use_timeline=True)
self.bttn_blend_range = cmds.button(label='Apply Pose Blend Shapes to\n Frames in above Range ',
c=range_frame_func, width=170, height=50)
cmds.setParent('..')
cmds.separator(height=10, style='in')
## MDE_poseblends_driver:
cmds.rowLayout(numberOfColumns=1, columnAttach=[(1, 'both', 70)])
create_driver_func = lambda *args: self.create_mde_poseblends_driver(mode=1)
self.bttn_mde_poseblends_driver = cmds.button(label='Make Pose Blend Shapes fire\n interactively ',
c=create_driver_func, width=170, height=50)
cmds.setParent('..')
cmds.separator(height=10, style='in')
# ## RECOMPUTE SKELETON
# cmds.rowLayout(numberOfColumns=2, columnAttach=[(1, 'left', 35), (2, 'both', 10)])
# Tpose_func = lambda *args: ui.getTpose()
# self.bttn_Tpose = cmds.button(label='Set Mesh to \n Bind-Pose', c=Tpose_func, width=120, height=50)
# rerig_func = lambda *args: self.reRig()
# self.bttn_rerig = cmds.button(label='Recompute \n Skeleton', c=rerig_func, width=120, height=50)
# cmds.setParent('..')
# cmds.separator(height=5, style='none')
cmds.showWindow(self.winName)
# @staticmethod
# def getTpose():
# """
# Set the Mesh & pose blendshapes to 0
# :return: None
# """
# maya_mesh = ui.get_maya_mesh_from_selection()
#
# if(not maya_mesh or cmds.objExists(maya_mesh) == False):
# print('\nError: Please select a SMPL mesh.')
# return
#
# is_not_skinned, lbs_cluster = maya_ops.is_not_skinned(maya_mesh)
# if is_not_skinned:
# print('\nError: Selected object has no Skin Cluster node (skeleton is not attached)')
# return
#
# has_no_blendShape, blendShape_node = maya_ops.has_no_blendShape(maya_mesh)
# if has_no_blendShape:
# print('\nError: Selected object has no blendShape node')
# return
#
# ## get all bones including root
# bones = cmds.listConnections(lbs_cluster, type='joint')
#
# weights_per_joint = 9
# ## Set all joints to 0...:
# for currentBone in enumerate(bones):
# cmds.rotate(0.0, 0.0, 0.0, currentBone)
#
# ## ...& pose blendShapes to 0(get T - pose)
# for bidx, currentBone in enumerate(bones):
# if bidx >= 23:
# continue
#
# # if the evaluation is here: it means bidx < 23:
# start_weight_index = (weights_per_joint * bidx)
# for pidx in range(weights_per_joint):
# blendShape_weight_attr = get_SMPL_blendShape_weight_attr_alias(
# start_weight_index,
# pidx
# )
# blendShape_weight_obj_attr = '%s.%s' % (blendShape_node, blendShape_weight_attr)
# cmds.setAttr(blendShape_weight_obj_attr, 0.0)
def create_mde_poseblends_driver(
self,
mode = 1
):
maya_mesh = ui.get_maya_mesh_from_selection()
if(not maya_mesh or cmds.objExists(maya_mesh) == False):
print('\nError: Please select a SMPL or STAR mesh.')
return