-
Notifications
You must be signed in to change notification settings - Fork 1
/
RMblendShapesTools.py
363 lines (319 loc) · 19.9 KB
/
RMblendShapesTools.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
import maya.cmds as cmds
import pprint as pp
from RMPY import nameConvention
from RMPY import RMRigTools
import maya.mel as mel
import pymel.core as pm
def RMblendShapeTargetDic(BSNode):
#AliasNames=cmds.listAttr((BSNode +".weight"),m=True);
InputTargetGroup=cmds.getAttr ((BSNode+".inputTarget[0].inputTargetGroup"),mi=True);
BlendShapeDic = {}
for eachTarget in InputTargetGroup:
AliasName = cmds.listAttr((BSNode +".weight["+str(eachTarget)+"]"), m=True);
BlendShapeDic[str(AliasName[0])] = {}
BlendShapeDic[str(AliasName[0])]["TargetGroup"] = eachTarget
Items = cmds.getAttr((BSNode+".inputTarget[0].inputTargetGroup["+str(eachTarget)+"].inputTargetItem"), mi=True);
BlendShapeDic[str(AliasName[0])]["Items"] = Items
return BlendShapeDic
'''
def invertCurrentPaintTargetWeights(ObjectName,index):
#cmds.setAttr("%s.inputTarget[0].inputTargetGroup[%s]paintTargetIndex"%ObjectName)
CompTarget = cmds.getAttr("%s.inputTarget[0].inputTargetGroup[%s].inputTargetItem[6000].inputComponentsTarget"%(ObjectName,index))
NormID =cmds.getAttr("%s.inputTarget[0].inputTargetGroup[%s].normalizationId"%(ObjectName,index))
BaseWeights= cmds.getAttr("%s.inputTarget[0].baseWeights"%(ObjectName))
NM = cmds.getAttr("%s.inputTarget[0].normalizationGroup[%s].normalizationWeights "%(ObjectName,index))
TargetIndex = cmds.getAttr("%s.inputTarget[0].paintTargetIndex"%ObjectName)
print ('CompTarget:%s'% (CompTarget))
print ('TargetIndex:%s'% (TargetIndex))
print ('NormID:%s'% (NormID))
print ('BaseWeights:%s'% (BaseWeights))
print ('NM:%s'% (NM))
weights = cmds.getAttr("%s.inputTarget[0].inputTargetGroup[%s].targetWeights"%(ObjectName,index))
print ('weights:%s'% (weights))
newWeights = []
weightIndex=0
#for i in weights[0]:
# cmds.setAttr("%s.inputTarget[0].inputTargetGroup[%s].targetWeights[%s]"%(ObjectName, index , weightIndex), (float(1.0)- i))
# weightIndex+=1
#weights = cmds.getAttr("%s.inputTarget[0].paintTargetWeights"%ObjectName)
pp.pprint (RMblendShapeTargetDic('blendShape20'))
invertCurrentPaintTargetWeights('blendShape20',3)
'''
def invertCurrentPaintTargetWeights(ObjectName, index):
if index != -1:
weight_map_target = 'inputTargetGroup[%s].targetWeights' % index
else:
weight_map_target = 'baseWeights'
weights = cmds.getAttr("%s.inputTarget[0].%s[*]" % (ObjectName, weight_map_target))
weight_index = cmds.getAttr("%s.inputTarget[0].%s" % (ObjectName, weight_map_target), multiIndices=True)
destination_geo = cmds.listConnections('{}.outputGeometry'.format(ObjectName), source=False)[0]
vertex_number = cmds.polyEvaluate(destination_geo, vertex=True)
for index_vertex in range(vertex_number):
if index_vertex in weight_index:
value = weights[weight_index.index(index_vertex)]
else:
value = 1
cmds.setAttr("%s.inputTarget[0].%s[%s]" % (ObjectName,weight_map_target, index_vertex), float(1.0) - value)
def copyCurrentPaintTargetWeights(ObjectName, index_source, index_destination):
if index_source != -1:
weight_map_source = 'inputTargetGroup[%s].targetWeights' % index_source
else:
weight_map_source = 'baseWeights'
if index_destination != -1:
weight_map_destination = 'inputTargetGroup[%s].targetWeights' % index_destination
else:
weight_map_destination = 'baseWeights'
weights = cmds.getAttr("%s.inputTarget[0].%s[*]" % (ObjectName, weight_map_source))
weight_index = cmds.getAttr("%s.inputTarget[0].%s" % (ObjectName, weight_map_source), multiIndices=True)
destination_weight_index = cmds.getAttr("%s.inputTarget[0].%s" % (ObjectName, weight_map_destination),
multiIndices=True)
for vertex_index in set(weight_index + destination_weight_index):
if vertex_index in weight_index:
set_value = weights[weight_index.index(vertex_index)]
else:
set_value = 1.0
cmds.setAttr("%s.inputTarget[0].%s[%s]" % (ObjectName, weight_map_destination, vertex_index), set_value)
def copy_paint_to_dictionary(ObjectName, indexSource):
if indexSource != -1:
source_weights_token = 'inputTargetGroup[{}].targetWeights'.format(indexSource)
else:
source_weights_token = 'baseWeights'
weights = cmds.getAttr("{}.inputTarget[0].{}[*]".format(ObjectName, source_weights_token))
weights_list = cmds.getAttr("{}.inputTarget[0].{}".format(ObjectName, source_weights_token),
multiIndices=True)
return {'weights': weights, 'weights_list': weights_list}
def paste_paint_from_dictionary(ObjectName, indexDestination, weights_dictionary):
weights_list = weights_dictionary['weights_list']
weights = weights_dictionary['weights']
if indexDestination != -1:
destination_weights_token = 'inputTargetGroup[%s].targetWeights' % indexDestination
else:
destination_weights_token = 'baseWeights'
current_weight_list = cmds.getAttr("%s.inputTarget[0].%s" %
(ObjectName, destination_weights_token), multiIndices=True)
if current_weight_list:
total_weight_list = set(weights_list + current_weight_list)
else:
total_weight_list = weights_list
for each_index in total_weight_list:
if each_index in weights_list:
value = weights[weights_list.index(each_index)]
else:
value = 1
cmds.setAttr("%s.inputTarget[0].%s[%s]" % (ObjectName, destination_weights_token, each_index), value)
def get_blend_shape(geometry):
for each in pm.listHistory(geometry, interestLevel=2, pruneDagObjects=True):
if pm.objectType(each) == 'blendShape':
return each
return None
class BSManager(object):
def __init__(self, NameConv=None) :
if NameConv:
self.NameConv = NameConv
else:
self.NameConv = nameConvention.NameConvention()
RigTools = RMRigTools.RMRigTools(self.NameConv)
self.FaceBlendShapeDic = {}
def AppyBlendShapeDefinition (self, BSDefinition, blendShapeNode = None, objectPrefix = ""):
for eachBSGroup in BSDefinition:
if BSDefinition[eachBSGroup]['Type'] == 'blendShapeDefinition':
if BSDefinition[eachBSGroup]["isSymetrical"] == True:
self.CreateMulipleBlendShapes (BSDefinition, "L", eachBSGroup, blendShapeNode = blendShapeNode,objectPrefix = objectPrefix)
self.CreateMulipleBlendShapes (BSDefinition, "R", eachBSGroup, blendShapeNode = blendShapeNode,objectPrefix = objectPrefix)
else:
self.CreateMulipleBlendShapes (BSDefinition, "", eachBSGroup, blendShapeNode = blendShapeNode,objectPrefix = objectPrefix)
elif BSDefinition[eachBSGroup]['Type'] == 'jointLinkDefinition':
if BSDefinition[eachBSGroup]["isSymetrical"] == True:
self.linkJointDefinition("LF",BSDefinition[eachBSGroup] )
self.linkJointDefinition("RH",BSDefinition[eachBSGroup] )
else:
self.linkJointDefinition("MD",BSDefinition[eachBSGroup] )
def returnBlendShapesByControl(self,connectionPlug, BSDict):
orderedList = {'positive':[] , 'negative':[]}
for eachBlendShape in BSDict:
if BSDict[eachBlendShape]["connection"] == connectionPlug:
if BSDict[eachBlendShape]["value"] > 0:
sign = 'positive'
else:
sign = 'negative'
if len (orderedList[sign]) == 0:
#print "Toinsert0:%s"%eachBlendShape
orderedList[sign].append(eachBlendShape)
else:
#print "Toinsert1:%s"%eachBlendShape
index = 0
for blendShapes in orderedList[sign]:
if (abs(BSDict[eachBlendShape]['value']) > abs(BSDict[blendShapes]['value'])):
index += 1
else:
break
orderedList[sign].insert(index,eachBlendShape)
pass
return orderedList
def returnJointsByControl(self, connectionPlug, JointDic):
orderedList = []
for eachJoint in JointDic:
if JointDic[eachJoint]['connection'] == connectionPlug:
orderedList.append(eachJoint)
return orderedList
def connectFromDefinition(self, BSDefinition, currentBlendShape, blendShapeNode, prefix, extremeValue, objectPrefix = ''):
if prefix!="":
Side = self.getSideFromPrefix(prefix)
#print "Connecting:%s.%s To %s.%s"% (self.NameConv.RMSetFromName(BSDefinition["control"],Side,Token = "Side" ), BSDefinition['blendShapes'][currentBlendShape]["connection"], blendShapeNode, prefix +currentBlendShape)
control = self.NameConv.set_from_name(BSDefinition["control"], Side, Token ="Side")
else:
control = BSDefinition["control"]
if extremeValue > 0:
RMRigTools.RMConnectWithLimits("%s.%s"%(control, BSDefinition['blendShapes'][currentBlendShape]["connection"]) ,"%s.%s"%(blendShapeNode,prefix + objectPrefix + currentBlendShape),[[ 0,0], [extremeValue,1]] )
else:
RMRigTools.RMConnectWithLimits("%s.%s"%(control, BSDefinition['blendShapes'][currentBlendShape]["connection"]) ,"%s.%s"%(blendShapeNode,prefix + objectPrefix +currentBlendShape) ,[[extremeValue,1], [ 0,0]] )
def CreateBlendShapesByControl (self, BlendShapeNode, Plug , BSDefinition, prefix, objectPrefix = ''):
''' This function adds new blendShapes to a node(BlendShapeNode) based on a plug,
first it createds a dictionary with self.returnBlendShapesByControl function,
This dictionary is then match againts the BS definition and the negative BS are added to a single BS
and the positive ones are added to a new one.'''
if cmds.objExists(BlendShapeNode):
BlendShapesOfSingleControl = self.returnBlendShapesByControl(Plug, BSDefinition["blendShapes"])
blendShapeOriginalGeo = self.findMeshByBlendShapeNode(BlendShapeNode)
BlendShapeDict = self.RMblendShapeTargetDic(BlendShapeNode)
NewTargetIndex = len(BlendShapeDict)
if 'positive' in BlendShapesOfSingleControl:
if len (BlendShapesOfSingleControl['positive']) >= 1:
SMX = BSDefinition['blendShapes'][BlendShapesOfSingleControl['positive'][len (BlendShapesOfSingleControl['positive']) - 1]]["value"]
else: SMX = 10
else:
SMX = 0
if 'negative' in BlendShapesOfSingleControl:
if len (BlendShapesOfSingleControl['negative']) >= 1:
SMN = BSDefinition['blendShapes'][BlendShapesOfSingleControl['negative'][len (BlendShapesOfSingleControl['negative']) - 1]]["value"]
else:
SMN = 0
else:
SMN = 0
#connection = BSDefinition['blendShapes'][BlendShapesOfSingleControl['negative'][len (BlendShapesOfSingleControl['negative']) - 1]]["connection"]
if prefix != '':
Side = self.getSideFromPrefix(prefix)
control = self.NameConv.set_from_name(BSDefinition["control"], Side, Token ="Side")
else:
control =BSDefinition["control"]
if "keyable" in BSDefinition['attributes'][Plug]:
if BSDefinition['attributes'][Plug]["keyable"]:
keyable = 1
else:
keyable = 0
else:
keyable = 1
self.AddAttributes (control, Plug , SMN, SMX, keyable )
#print BlendShapesOfSingleControl
#pp.pprint (BlendShapeDict)
#print "NewTargetIndex:%s"%(NewTargetIndex)
AtLeastOne = False
if 'positive' in BlendShapesOfSingleControl and len(BlendShapesOfSingleControl['positive']) >= 1:
if (prefix + BlendShapesOfSingleControl['positive'][len(BlendShapesOfSingleControl['positive']) - 1]) not in BlendShapeDict:
for eachBS in BlendShapesOfSingleControl['positive']:
if cmds.objExists(prefix + eachBS):
print("adding pblendShape:%s"% (prefix + eachBS))
cmds.blendShape(BlendShapeNode, edit=True, target = [blendShapeOriginalGeo, NewTargetIndex + 1, prefix + objectPrefix + eachBS, float (abs(BSDefinition['blendShapes'][eachBS]["value"])) / 10.0 ])
#NewTargetIndex += 1
AtLeastOne = True
else:
print (" BS %s not found on scene"%(prefix+ objectPrefix + eachBS))
if AtLeastOne:
NewTargetIndex += 1
self.connectFromDefinition( BSDefinition,BlendShapesOfSingleControl['positive'][len(BlendShapesOfSingleControl['positive']) - 1], BlendShapeNode, prefix, 10, objectPrefix = objectPrefix)
else:
self.connectFromDefinition( BSDefinition,BlendShapesOfSingleControl['positive'][len(BlendShapesOfSingleControl['positive']) - 1], BlendShapeNode, prefix, 10, objectPrefix = objectPrefix)
AtLeastOne = False
if 'negative' in BlendShapesOfSingleControl and len(BlendShapesOfSingleControl['negative']) >= 1:
if (prefix + BlendShapesOfSingleControl['negative'][len(BlendShapesOfSingleControl['negative']) - 1]) not in BlendShapeDict:
for eachBS in BlendShapesOfSingleControl['negative']:
if cmds.objExists(prefix + eachBS):
print ("adding blendShape:%s"% (prefix+objectPrefix + eachBS))
cmds.blendShape(BlendShapeNode, edit=True, target = [blendShapeOriginalGeo, NewTargetIndex + 1, prefix+ objectPrefix + eachBS, float (abs(BSDefinition['blendShapes'][eachBS]["value"])) / 10.0 ])
#NewTargetIndex += 1
AtLeastOne = True
else:
print (" BS %s not found on scene"%(prefix + objectPrefix + eachBS))
if AtLeastOne:
NewTargetIndex += 1
self.connectFromDefinition( BSDefinition,BlendShapesOfSingleControl['negative'][len(BlendShapesOfSingleControl['negative']) - 1], BlendShapeNode, prefix, -10, objectPrefix = objectPrefix)
else :
self.connectFromDefinition( BSDefinition,BlendShapesOfSingleControl['negative'][len(BlendShapesOfSingleControl['negative']) - 1], BlendShapeNode, prefix, -10, objectPrefix = objectPrefix)
def findMeshByBlendShapeNode (self,BSNode):
mesh = cmds.listConnections ("%s.outputGeometry[0]"%BSNode, type='mesh')
return mesh[0]
def getSideFromPrefix(self, prefix):
if prefix == "":
Side = "MD"
elif prefix=="R":
Side = "RH"
elif prefix=="L":
Side = "LF"
return Side
def CreateMulipleBlendShapes (self,BSDefinition, prefix, BSGroups, blendShapeNode = None, objectPrefix = ''):
self.FaceBlendShapeDic = None
if BSDefinition[BSGroups]['Type'] == "blendShapeDefinition":
print ("blendShapeNode%s"%blendShapeNode)
if blendShapeNode == None :
BSNodeArray = mel.eval('''source RMDeformers.mel;\nstring $BSNode[]=GetDeformer("'''+ objectPrefix + BSDefinition[BSGroups]['baseMesh']+'''","blendShape");''')
if len(BSNodeArray) == 0:
print ("No blendshape Found on %s" % BSDefinition[BSGroups]['baseMesh'])
BSName = BSGroups + objectPrefix + "BS"
#blendShapeOriginalGeo = cmds.duplicate(prefix + BSDefinition[BSGroups]['baseMesh'], name = self.NameConv.RMGetAShortName(BSDefinition[BSGroups]['baseMesh']) + BSGroups)
Side = self.getSideFromPrefix(prefix)
#blendShapeOriginalGeo = self.NameConv.RMRenameNameInFormat( blendShapeOriginalGeo, System = "faceRig",Side = Side)
#cmds.blendShape(blendShapeOriginalGeo, name = BSName)
cmds.blendShape(BSDefinition[BSGroups]['baseMesh'], name = BSName)
BSName = self.NameConv.rename_name_in_format(BSName, {'system': "faceRig", 'side': Side})
else:
BSName = BSNodeArray[0]
else :
BSName = blendShapeNode
for eachControl in BSDefinition [BSGroups]['order']:
self.CreateBlendShapesByControl(BSName, eachControl, BSDefinition [BSGroups], prefix, objectPrefix = objectPrefix)
return self.FaceBlendShapeDic
def AddAttributes (self, Object, Attribute, SMN, SMX,keyable = 1 ):
#print ("Adding Attribute:%s to Object:%s Min:%s MAx %s"%(Attribute,Object,SMN,SMX))
AttrList = cmds.listAttr(Object)
if Attribute not in AttrList:
cmds.addAttr(Object, at="float", ln = Attribute ,hnv = 1, hxv = 1, h = 0, k = keyable, smn = SMN, smx = SMX)
def linkBlendShapesAttr (self, Object, AttributesDefinition, BSNode):
blendShapeTargetDic = self.RMblendShapeTargetDic(BSNode)
#for eachAttr in AttributesDefinition:
def RMblendShapeTargetDic (self, BSNode):
#AliasNames=cmds.listAttr((BSNode +".weight"),m=True);
InputTargetGroup = cmds.getAttr ((BSNode+".inputTarget[0].inputTargetGroup"), mi=True);
BlendShapeDic={}
if InputTargetGroup:
for eachTarget in InputTargetGroup:
AliasName=cmds.listAttr((BSNode + ".weight[" + str(eachTarget) + "]"), m=True)
BlendShapeDic[str(AliasName[0])] = {}
BlendShapeDic[str(AliasName[0])]["TargetGroup"] = eachTarget
Items = cmds.getAttr ((BSNode+".inputTarget[0].inputTargetGroup["+str(eachTarget)+"].inputTargetItem"), mi = True)
BlendShapeDic[str(AliasName[0])]["Items"] = Items
return BlendShapeDic
def linkJointDefinition (self, Side, jointLinkDefinition):
control = jointLinkDefinition['control']
if Side in ["LF","RH"]:
control = self.NameConv.set_from_name(jointLinkDefinition['control'], Side, Token ='Side')
if cmds.objExists( control):
for eachAttribute in jointLinkDefinition['order']:
jointsList = self.returnJointsByControl( eachAttribute, jointLinkDefinition['joints'])
for eachJoint in jointsList:
JointName = eachJoint
if Side in [ "LF", "RH"]:
JointName = self.NameConv.set_from_name(eachJoint, Side, Token ='Side')
if cmds.objExists(JointName):
if jointLinkDefinition['joints'][eachJoint]['value'] == None:
cmds.connectAttr ('%s.%s'%(control,jointLinkDefinition['joints'][eachJoint]['connection'] ), '%s.%s'%(JointName, jointLinkDefinition['joints'][eachJoint]['inputPlug']),force=True)
else:
self.AddAttributes ( control, eachAttribute, jointLinkDefinition['attributes'][eachAttribute]['min'], jointLinkDefinition['attributes'][eachAttribute]['max'])
RMRigTools.RMConnectWithLimits('%s.%s'%(control,jointLinkDefinition['joints'][eachJoint]['connection'] ), '%s.%s'%(JointName, jointLinkDefinition['joints'][eachJoint]['inputPlug']), jointLinkDefinition['joints'][eachJoint]['value'])
else:
print ("Joint object doesnt exists:%s"%JointName)
else:
print ("Control object doesnt exists:%s"%control)
control = self.NameConv.set_from_name(jointLinkDefinition['control'], Side, Token='Side')
#if __name__=="__main__":
# Manager = BSManager()
# Manager.AppyBlendShapeDefinition( SkinedJoints)