/
bdRigLegMetroid.py
375 lines (270 loc) · 12.5 KB
/
bdRigLegMetroid.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
import pymel.core as pm
def bdRigLegCtrl(side):
print "Rigging %s side leg controller"%side
#create a group based rig for a leg
def bdRigLegBones(side):
ikAnimCon = pm.ls(side.upper() + '_Foot_CON',type='transform')[0]
legBonesNames = ['Thigh','Shin','Foot','Toe']
legBones = []
for bone in legBonesNames:
legBone = pm.ls(side + bone)[0]
legBones.append(legBone)
print legBone.name()
toeEnd = pm.ls(side + 'Toe_end')[0]
legBones.append(toeEnd)
#START setup foot roll
footIk = pm.ikHandle(sol= 'ikRPsolver',sticky='sticky', startJoint=legBones[0],endEffector = legBones[2],name = side + '_foot_ikHandle')[0]
footIk.visibility.set(0)
ballIk = pm.ikHandle(sol= 'ikSCsolver',sticky='sticky', startJoint=legBones[2],endEffector = legBones[3],name = side + '_ball_ikHandle')[0]
ballIk.visibility.set(0)
toeIk = pm.ikHandle(sol= 'ikSCsolver',sticky='sticky', startJoint=legBones[3],endEffector = legBones[4],name = side + '_toe_ikHandle')[0]
toeIk.visibility.set(0)
#create the groups that will controll the foot animations ( roll, bend, etc etc)
footHelpers = pm.ls(side + '*_helper',type='transform')
ankleLoc = bdCreateOffsetLoc(legBones[2],side + '_ankle_loc')
footLoc = bdCreateOffsetLoc(legBones[2],side + '_foot_loc')
ballLoc = bdCreateOffsetLoc(legBones[3],side + '_ball_loc')
ballTwistLoc = bdCreateOffsetLoc(legBones[3],side + '_ball_twist_loc')
toeLoc = bdCreateOffsetLoc(legBones[4],side + '_toe_loc')
toeBendLoc = bdCreateOffsetLoc(legBones[3],side + '_toe_bend_loc')
innerLoc = outerLoc = heelLoc = ''
for helper in footHelpers:
if 'inner' in helper.name():
innerLoc = bdCreateOffsetLoc(helper,side + '_inner_bank_loc')
elif 'outer' in helper.name():
outerLoc = bdCreateOffsetLoc(helper,side + '_outer_bank_loc')
elif 'heel' in helper.name():
heelLoc = bdCreateOffsetLoc(helper,side + '_heel_loc')
#pm.delete(footHelpers)
pm.parent(footIk, footLoc)
pm.parent(ballIk, ballLoc)
pm.parent(toeIk,toeBendLoc)
pm.parent(toeBendLoc,toeLoc)
pm.parent(footLoc,ballLoc)
pm.parent(ballLoc,toeLoc)
pm.parent(toeLoc,ballTwistLoc)
pm.parent(ballTwistLoc,innerLoc)
pm.parent(innerLoc,outerLoc)
pm.parent(outerLoc,heelLoc)
pm.parent(heelLoc,ankleLoc)
#add atributes on the footGrp - will be conected later to an anim controler
autoRollAttrList = ['Roll','ToeStart','BallStraight']
footAttrList = ['HeelTwist','BallTwist','TipTwist','Bank','ToeBend','KneeTwist']
normalRollAttrList = ['HeelRoll','BallRoll','TipRoll']
pm.addAttr(ikAnimCon ,ln='__AutoFootRoll__',nn='__AutoFootRoll__',at='bool' )
ikAnimCon.attr('__AutoFootRoll__').setKeyable(True)
ikAnimCon.attr('__AutoFootRoll__').setLocked(True)
pm.addAttr(ikAnimCon ,ln='Enabled',nn='Enabled',at='long' )
ikAnimCon.attr('Enabled').setKeyable(True)
ikAnimCon.attr('Enabled').setMin(0)
ikAnimCon.attr('Enabled').setMax(1)
ikAnimCon.attr('Enabled').set(1)
pm.addAttr(ikAnimCon ,ln='______',nn='______',at='bool' )
ikAnimCon.attr('______').setKeyable(True)
ikAnimCon.attr('______').setLocked(True)
for attr in autoRollAttrList:
pm.addAttr(ikAnimCon ,ln=attr,nn=attr,at='float' )
ikAnimCon.attr(attr).setKeyable(True)
pm.addAttr(ikAnimCon ,ln='__FootRoll__',nn='__FootRoll__',at='bool' )
ikAnimCon.attr('__FootRoll__').setKeyable(True)
ikAnimCon.attr('__FootRoll__').setLocked(True)
for attr in normalRollAttrList:
pm.addAttr(ikAnimCon ,ln=attr,nn=attr,at='float' )
ikAnimCon.attr(attr).setKeyable(True)
pm.addAttr(ikAnimCon ,ln='__FootAttr__',nn='__FootAttr__',at='bool' )
ikAnimCon.attr('__FootAttr__').setKeyable(True)
ikAnimCon.attr('__FootAttr__').setLocked(True)
for attr in footAttrList:
pm.addAttr(ikAnimCon ,ln=attr,nn=attr,at='float' )
ikAnimCon.attr(attr).setKeyable(True)
ikAnimCon.attr('ToeStart').set(40)
ikAnimCon.attr('BallStraight').set(80)
bdCreateReverseFootRoll(ikAnimCon,heelLoc,ballLoc,toeLoc)
#connect the attributes
ikAnimCon.attr('HeelTwist').connect(heelLoc.rotateY)
ikAnimCon.attr('BallTwist').connect(ballTwistLoc.rotateY)
ikAnimCon.attr('TipTwist').connect(toeLoc.rotateY)
ikAnimCon.attr('ToeBend').connect(toeBendLoc.rotateX)
bdConnectBank(ikAnimCon,innerLoc,outerLoc)
#START no flip knee knee
mirror = 1
if side == 'r':
mirror = -1
offset = 90
poleVectorLoc = pm.spaceLocator(name = side + '_knee_loc_PV')
poleVectorLocGrp = pm.group(poleVectorLoc,n=poleVectorLoc + '_GRP')
thighPos = legBones[0].getTranslation(space='world')
poleVectorLocGrp.setTranslation([thighPos[0] + mirror * 5,thighPos[1],thighPos[2]])
pm.poleVectorConstraint(poleVectorLoc,footIk)
adlNode = pm.createNode('addDoubleLinear',name = side + '_adl_twist')
adlNode.input2.set(mirror * offset)
ikAnimCon.attr('KneeTwist').connect(adlNode.input1)
adlNode.output.connect(footIk.twist)
startTwist = mirror * offset
limit = 0.001
increment = mirror * 0.01
while True:
pm.select(cl=True)
thighRot = pm.xform(legBones[0],q=True,ro=True,os=True)
print thighRot[0]
if ((thighRot[2] > limit)):
startTwist = startTwist - increment
adlNode.input2.set(startTwist)
else:
break
#END knee
pm.parent(ankleLoc,ikAnimCon)
def bdCreateOffsetLoc(destination,name):
loc = pm.spaceLocator(n=name)
destPos = destination.getTranslation(space='world')
destRot = destination.getRotation(space='world')
loc.setTranslation(destPos,space='world')
#loc.setRotation(destPos,space='world')
return loc
def bdConnectChains():
selection = pm.ls(sl=True)
bindChainChildren = []
if len(selection) == 2:
bindChain = selection[0]
ikfkCon = selection[1]
fkJnt = pm.ls(bindChain.name().replace('JNT','FK'))[0]
ikJnt = pm.ls(bindChain.name().replace('JNT','IK'))[0]
bdCreateBlend(bindChain,fkJnt,ikJnt,ikfkCon)
bindChainChildren = bindChain.listRelatives(c=True, type= 'joint',ad=True)
for child in bindChainChildren :
if 'END' not in child.name():
fkJnt = pm.ls(child.name().replace('JNT','FK'))[0]
ikJnt = pm.ls(child.name().replace('JNT','IK'))[0]
bdCreateBlend(child,fkJnt,ikJnt,ikfkCon)
def bdCreateBlend(bindJnt,fkJnt, ikJnt,ikfkCon):
blendColorPos = pm.createNode('blendColors',name = bindJnt.name().replace('JNT','POS_BC'))
blendColorRot = pm.createNode('blendColors',name = bindJnt.name().replace('JNT','ROT_BC'))
blendColorScl = pm.createNode('blendColors',name = bindJnt.name().replace('JNT','SCL_BC'))
ikfkCon.attr('IKFK').connect(blendColorPos.blender)
ikfkCon.attr('IKFK').connect(blendColorRot.blender)
ikfkCon.attr('IKFK').connect(blendColorScl.blender)
fkJnt.translate.connect(blendColorPos.color1)
ikJnt.translate.connect(blendColorPos.color2)
blendColorPos.output.connect(bindJnt.translate)
fkJnt.rotate.connect(blendColorRot.color1)
ikJnt.rotate.connect(blendColorRot.color2)
blendColorRot.output.connect(bindJnt.rotate)
fkJnt.scale.connect(blendColorScl.color1)
ikJnt.scale.connect(blendColorScl.color2)
blendColorScl.output.connect(bindJnt.scale)
def bdCreateReverseFootRoll(ankleLoc,heelLoc,ballLoc,toeLoc):
blendColorHeelAuto = pm.createNode('blendColors',name = heelLoc.name().replace('loc','auto_BC'))
blendColorBallAuto= pm.createNode('blendColors',name = ballLoc.name().replace('loc','auto_BC'))
blendColorToeAuto = pm.createNode('blendColors',name = toeLoc.name().replace('loc','auto_BC'))
ankleLoc.attr('Enabled').connect(blendColorHeelAuto.blender)
ankleLoc.attr('Enabled').connect(blendColorBallAuto.blender)
ankleLoc.attr('Enabled').connect(blendColorToeAuto.blender)
ankleLoc.attr('HeelRoll').connect(blendColorHeelAuto.color2R)
ankleLoc.attr('BallRoll').connect(blendColorBallAuto.color2R)
ankleLoc.attr('TipRoll').connect(blendColorToeAuto.color2R)
#setup auto part
clampHeel = pm.createNode('clamp',n=heelLoc.name().replace('loc','roll_CL'))
clampHeel.minR.set(-90)
setRangeToe = pm.createNode('setRange',n=toeLoc.name().replace('loc','linestep_SR'))
setRangeToe.minX.set(0)
setRangeToe.maxX.set(1)
setRangeBall1 = pm.createNode('setRange',n=ballLoc.name().replace('loc','linestep_SR'))
setRangeBall1.minX.set(0)
setRangeBall1.maxX.set(1)
setRangeBall1.oldMinX.set(0)
mdToeRoll = pm.createNode('multiplyDivide',n=toeLoc.name().replace('loc','roll_MD'))
mdBallRoll = pm.createNode('multiplyDivide',n=ballLoc.name().replace('loc','roll_MD'))
mdBallRange2 = pm.createNode('multiplyDivide',n=ballLoc.name().replace('loc','roll_range_MD'))
pmaBallRange = pm.createNode('plusMinusAverage',n=ballLoc.name().replace('loc','range_PMA'))
pmaBallRange.input1D[0].set(1)
pmaBallRange.operation.set(2)
#connect the heel for negative rolls
ankleLoc.attr('Roll').connect(clampHeel.inputR)
clampHeel.outputR.connect(blendColorHeelAuto.color1R)
blendColorHeelAuto.outputR.connect(heelLoc.rotateX)
#connect the toe
ankleLoc.attr('Roll').connect(setRangeToe.valueX)
ankleLoc.attr('BallStraight').connect(setRangeToe.oldMaxX)
ankleLoc.attr('ToeStart').connect(setRangeToe.oldMinX)
ankleLoc.attr('Roll').connect(mdToeRoll.input2X)
setRangeToe.outValueX.connect(mdToeRoll.input1X)
mdToeRoll.outputX.connect(blendColorToeAuto.color1R)
blendColorToeAuto.outputR.connect(toeLoc.rotateX)
#connect the ball
ankleLoc.attr('Roll').connect(setRangeBall1.valueX)
ankleLoc.attr('ToeStart').connect(setRangeBall1.oldMaxX)
setRangeToe.outValueX.connect(pmaBallRange.input1D[1])
setRangeBall1.outValueX.connect(mdBallRange2.input1X)
pmaBallRange.output1D.connect(mdBallRange2.input2X)
ankleLoc.attr('Roll').connect(mdBallRoll.input2X)
mdBallRange2.outputX.connect(mdBallRoll.input1X)
mdBallRoll.outputX.connect(blendColorBallAuto.color1R)
blendColorBallAuto.outputR.connect(ballLoc.rotateX)
#setup manual
def bdConnectBank(ikAnimCon,innerLoc,outerLoc):
side = 'l'
if 'r' in ikAnimCon.name():
side = 'r'
clampInner = pm.createNode('clamp',n=innerLoc.name().replace('loc','CL'))
if side == 'l':
clampInner.maxR.set(90)
else:
clampInner.minR.set(-90)
clampOuter = pm.createNode('clamp',n=outerLoc.name().replace('loc','CL'))
if side == 'l':
clampOuter.minR.set(-90)
else:
clampOuter.maxR.set(90)
ikAnimCon.attr('Bank').connect(clampInner.inputR)
clampInner.outputR.connect(innerLoc.rotateZ)
ikAnimCon.attr('Bank').connect(clampOuter.inputR)
clampOuter.outputR.connect(outerLoc.rotateZ)
def bdScaleChain(side):
pm.select(cl=True)
ikAnimCon = pm.ls(side + '*foot*IK_CON',type='transform')[0]
legBonesNames = ['Thigh','Knee','Foot']
scaleBones = []
for bone in legBonesNames:
scaleBone = pm.ls(side + '_' + bone + '_SCL')[0]
scaleBones.append(scaleBone)
legBones = []
for bone in legBonesNames:
legBone = pm.ls(side + '_' + bone + '_IK')[0]
legBones.append(legBone)
distance1 = pm.createNode('distanceBetween',name = side + '_thigh_length')
distance2 = pm.createNode('distanceBetween',name = side + '_femur_length')
distanceStraight = pm.createNode('distanceBetween',name = side + '_straight_length')
adlDistance = pm.createNode('addDoubleLinear',name = side + '_leg_length_ADL')
condDistance = pm.createNode('condition',name = side + '_leg_length_CND')
condDistance.colorIfTrueR.set(1)
condDistance.secondTerm.set(1)
condDistance.operation.set(5)
mdScaleFactor = pm.createNode('multiplyDivide',name = side + '_leg_scaleFactor_MD')
mdScaleFactor.operation.set(2)
scaleBones[0].rotatePivotTranslate.connect(distance1.point1)
scaleBones[1].rotatePivotTranslate.connect(distance1.point2)
scaleBones[0].worldMatrix.connect(distance1.inMatrix1)
scaleBones[1].worldMatrix.connect(distance1.inMatrix2)
scaleBones[1].rotatePivotTranslate.connect(distance2.point1)
scaleBones[2].rotatePivotTranslate.connect(distance2.point2)
scaleBones[1].worldMatrix.connect(distance2.inMatrix1)
scaleBones[2].worldMatrix.connect(distance2.inMatrix2)
scaleBones[0].rotatePivotTranslate.connect(distanceStraight.point1)
ikAnimCon.rotatePivotTranslate.connect(distanceStraight.point2)
scaleBones[0].worldMatrix.connect(distanceStraight.inMatrix1)
ikAnimCon.worldMatrix.connect(distanceStraight.inMatrix2)
distance1.distance.connect(adlDistance.input1)
distance2.distance.connect(adlDistance.input2)
adlDistance.output.connect(mdScaleFactor.input2X)
distanceStraight.distance.connect(mdScaleFactor.input1X)
mdScaleFactor.outputX.connect(condDistance.firstTerm)
mdScaleFactor.outputX.connect(condDistance.colorIfFalseR)
condDistance.outColorR.connect(legBones[0].scaleX)
condDistance.outColorR.connect(legBones[1].scaleX)
print scaleBones
print legBones
bdRigLegBones('r')
#bdRigLegBones('R')
#bdConnectChains()
#bdScaleChain('R')
#bdScaleChain('L')