/
MocapToCharacter.py
444 lines (368 loc) · 14.6 KB
/
MocapToCharacter.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
import pymel.core as pm
import pymel.core.datatypes as dt
import datetime as time
from maya import OpenMayaUI as omui
import PySide2
from PySide2 import QtWidgets
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtUiTools import *
from shiboken2 import wrapInstance
import sys
sys.path.append('Path to folder where script is')
#--------------------------------------------------------------------------#
#--------------------------------Functions---------------------------------#
#--------------------------------------------------------------------------#
def getKeyCount(jnt):
numAttr = len(pm.listAttr(jnt, keyable=True))
count = pm.keyframe(jnt, keyframeCount=True, query=True) / numAttr
return count
def checkBoxs(*args):
if loopBox.getValue():
interpBox.setEnable(False)
else:
interpBox.setEnable(True)
if interpBox.getValue():
loopBox.setEnable(False)
else:
loopBox.setEnable(True)
def bakeJoints(jnt, start, end):
pm.select(jnt)
pm.bakeResults(simulation=True, t=(start, end), hierarchy=('below'), sampleBy=1, oversamplingRate=1, disableImplicitControl=True, preserveOutsideKeys=True, sparseAnimCurveBake=False, removeBakedAttributeFromLayer=False, removeBakedAnimFromLayer=False, bakeOnOverrideLayer=False, minimizeRotation=True, controlPoints=False, shape=True)
def jntCtrls(list):
for i, jnt in enumerate(list):
if i < len(sel) - 1:
dst1 = pm.joint(list[i + 1], query=True, position=True)
dst2 = pm.joint(list[i - 1], query=True, position=True)
rot = jnt.getRotation()
dst3 = []
dst3.append((dst2[0]) - (dst1[0]))
dst3.append((dst2[1]) - (dst1[1]))
dst3.append((dst2[2]) - (dst1[2]))
ctrl = pm.circle(c=(0, 0, 0), nr=dst3, sw=360, r=1, d=3, ut=0, tol=0.0001, s=8, ch=1)[0]
pm.move(ctrl, dst2)
def slerp(v0, v1, t):
qm = dt.Quaternion()
cosHalfTheta = (v0.w * v1.w) + (v0.x * v1.x) + (v0.y * v1.y) + (v0.z * v1.z)
if abs(cosHalfTheta) >= 1.0:
qm.w = v0.w
qm.x = v0.x
qm.y = v0.y
qm.z = v0.z
return qm
halfTheta = dt.acos(cosHalfTheta)
sinHalfTheta = dt.sqrt(1.0 - cosHalfTheta*cosHalfTheta)
if dt.fabs(sinHalfTheta) < 0.001:
qm.w = (v0.w * 0.5 + v1.w * 0.5)
qm.x = (v0.x * 0.5 + v1.x * 0.5)
qm.y = (v0.y * 0.5 + v1.y * 0.5)
qm.z = (v0.z * 0.5 + v1.z * 0.5)
return qm
ratioA = dt.sin((1 - t) * halfTheta) / sinHalfTheta
ratioB = dt.sin(t * halfTheta) / sinHalfTheta
qm.w = (v0.w * ratioA + v1.w * ratioB)
qm.x = (v0.x * ratioA + v1.x * ratioB)
qm.y = (v0.y * ratioA + v1.y * ratioB)
qm.z = (v0.z * ratioA + v1.z * ratioB)
return qm
def buttonPressed(*args):
startFrame = minFld.getValue()
endFrame = maxFld.getValue()
difference = endFrame - startFrame
if bakeBox.getValue():
bakeJoints(root, startFrame, endFrame)
if loopBox.getValue():
startInterp = endFrame - int(difference * 0.2)
pm.playbackOptions(min=0, max=difference)
for jnt in sourceJoints:
if (startFrame > 1):
pm.cutKey(jnt, time=(0, startFrame - 1), option='keys')
pm.cutKey(jnt, time=(startInterp, keyCount), option='keys')
pm.copyKey(jnt, time=startFrame)
pm.pasteKey(jnt, time=endFrame)
for i in range(endFrame - int(difference * 0.2), endFrame):
pm.currentTime(i)
pm.setKeyframe(jnt)
animCrvs = pm.ls(type=['animCurveTA', 'animCurveTL', 'animCurveTT', 'animCurveTU'])
for item in animCrvs:
pm.keyframe(item, edit=True, relative=True, timeChange=-startFrame)
for jnt in sourceJoints:
pm.cutKey(jnt, time=(-keyCount, 0), option='keys')
pm.cutKey(jnt, time=(difference+1, keyCount), option='keys')
pm.copyKey(jnt, time=difference)
pm.pasteKey(jnt, time=0)
if interpBox.getValue():
for i in range(startFrame, endFrame):
for jnt in sourceJoints:
pm.currentTime(i + 1)
qt1 = jnt.getRotation().asQuaternion()
pm.currentTime(i)
qt2 = jnt.getRotation().asQuaternion()
newqt1 = slerp(qt1, qt2, 0.5)
jnt.setRotation(newqt1)
pm.setKeyframe(jnt)
"""Returns a list of parents from child to root"""
def getParentPath(currentJoint, jointNames):
jointNames.append(currentJoint)
jointParent = currentJoint.getParent()
if (jointParent) and (str(type(jointParent)) == 'joint'):
print currentJoint
getParentPath(jointParent, jointNames)
return jointNames
"""Removes rotate limitations"""
def removeRotLimits(node):
for child in node.getChildren():
pm.transformLimits(child, erx=(0, 0), ery=(0, 0), erz=(0, 0))
if child.numChildren() > 0:
removeRotLimits(child)
"""Returns a list of parents from root to child"""
def getChildrenPath(node, list):
for child in node.getChildren():
list.append(child)
if child.numChildren() > 0:
getChildrenPath(child, list)
"""Returns Parent Matrix"""
def sRotation(sourceNames):
parentMatrix = dt.Matrix()
for node in sourceNames:
if (node is not sourceNames[0]):
nodeRot = pm.getAttr(node + '.rotate', time=0)
nodeRot = dt.EulerRotation(nodeRot[0], nodeRot[1], nodeRot[2]).asMatrix()
parentMatrix *= node.getOrientation().asMatrix() * nodeRot
return parentMatrix
"""Create list of parent matrixes"""
def assembleTheParentMatrixes(jointList, newMatrixes):
for currentJoint in jointList:
if (jointNames):
jointNames[:] = []
if currentJoint.getParent() is not None:
sourceNames = getParentPath(currentJoint, jointNames)
newMatrixes.append(sRotation(sourceNames))
else:
newMatrixes.append(dt.Matrix())
"""Returns the index of where the source joint is in the target list"""
def searchID(child, list):
counter = 0
nChild = str(child)
for i, item in enumerate(list):
nItem = str(item)
if nItem == nChild:
counter = i
return counter
"""Return a list of all source joints bindposes"""
def getBindPoses(sourceList, newList):
for joints in sourceList:
bindPose = pm.getAttr(joints + '.rotate', t=0)
bindPose = dt.EulerRotation(bindPose[0], bindPose[1], bindPose[2]).asMatrix()
newList.append(bindPose.transpose())
"""Takes the list of string objects created from the QTListWidget and turns the objects into joints"""
def replaceList(list1, list2):
newList = []
for item in list1:
for oldItem in list2:
if item == oldItem:
newList.append(oldItem)
return newList
"""Loops through both the source and targets children"""
def setJointRotation(sourceList, targetList, x, sParentMat, tParentMat):
for i, (sourceChild, targetChild) in enumerate(zip(sourceList, targetList)):
"""Get keyframe rotations as k"""
k = pm.getAttr(sourceChild + '.rotate', time=x)
k = dt.EulerRotation(k[0], k[1], k[2]).asMatrix()
"""Create isolatedRotation"""
isolatedRotation = sBindPoseInversed[i] * k
"""Change of basis to world space rotation"""
sOrientation = sourceChild.getOrientation().asMatrix()
sOrientationInversed = sOrientation.transpose()
sParentsInversed = sParentMat[i].transpose()
worldspaceRotation = sOrientationInversed * sParentsInversed * isolatedRotation * sParentMat[i] * sOrientation
"""Second change of basis to the space of target"""
tOrientation = targetChild.getOrientation().asMatrix()
tOrientationInversed = tOrientation.transpose()
tParentsInversed = tParentMat[i].transpose()
translatedRotation = tOrientation * tParentMat[i] * worldspaceRotation * tParentsInversed * tOrientationInversed
tRot = pm.getAttr(targetChild + '.rotate', t=0)
tRot = dt.EulerRotation(tRot[0], tRot[1], tRot[2]).asMatrix()
finalRotation = tRot * translatedRotation
"""Get rotation values from the translated rotation matrix and set key for target"""
eulRot = dt.EulerRotation(finalRotation)
eulRot = dt.degrees(eulRot)
pm.setAttr(targetChild + '.rotate', eulRot.x, eulRot.y, eulRot.z)
"""If joint is root, then also set translation"""
if sourceChild.getParent() is None:
sKmove = pm.getAttr(sourceChild + '.translate', time=x)
pm.setAttr(targetChild + '.translate', sKmove[0], sKmove[1], sKmove[2])
pm.setKeyframe(targetChild, t=(x), edit=True)
#--------------------------------------------------------------------------#
#---------------------------Global Variables-------------------------------#
#--------------------------------------------------------------------------#
root = pm.ls(sl=True)
targetJoints = []
sourceJoints = []
if len(root) < 2:
keyCount = getKeyCount(root)
sourceJoints.append(root[0])
getChildrenPath(root[0], sourceJoints)
removeRotLimits(root[0])
pm.transformLimits(root[0], erx=(0, 0), ery=(0, 0), erz=(0, 0))
else:
keyCount = getKeyCount(root[0])
sourceJoints.append(root[0])
targetJoints.append(root[1])
getChildrenPath(root[0], sourceJoints)
getChildrenPath(root[1], targetJoints)
removeRotLimits(root[0])
removeRotLimits(root[1])
pm.transformLimits(root[0], erx=(0, 0), ery=(0, 0), erz=(0, 0))
pm.transformLimits(root[1], erx=(0, 0), ery=(0, 0), erz=(0, 0))
allJnts = pm.ls(type='joint')
pm.currentTime(0, edit=True)
counter = 0
jointNames = []
jointOrientations = []
skipSJoints = []
skipTJoints = []
takeSList = []
takeTList = []
newSList = []
newTList = []
sParentMatrixes = []
tParentMatrixes = []
sBindPoseInversed = []
tBindPoseInversed = []
#--------------------------------------------------------------------------#
#------------------------------User Interface------------------------------#
#--------------------------------------------------------------------------#
if len(root) < 2:
win = pm.window(title='My Window')
layout = pm.columnLayout()
loopBox = pm.checkBox(label = 'Loop', value=False, parent=layout, onc=checkBoxs, ofc=checkBoxs)
interpBox = pm.checkBox(label = 'Interpolate', value=False, parent=layout, onc=checkBoxs, ofc=checkBoxs)
bakeBox = pm.checkBox(label = 'Bake animation', value=False, parent=layout)
minTxt = pm.text( label='Min key', align='center', parent=layout)
minFld = pm.intField(min=0, max=keyCount-1, parent=layout)
maxTxt = pm.text( label='Max key', align='center', parent=layout)
maxFld = pm.intField(min=0, max=keyCount-1, parent=layout)
run = pm.button(label='Run', parent=layout)
transfer = pm.button(label='Transfer Animation', parent=layout)
run.setCommand(buttonPressed)
win.show()
if len(root) > 1:
def getMayaWin():
mayaWinPtr = omui.MQtUtil.mainWindow( )
mayaWin = wrapInstance( long( mayaWinPtr ), QtWidgets.QMainWindow )
def loadUI( path ):
loader = QUiLoader()
uiFile = QFile( path )
dirIconShapes = ""
buff = None
if uiFile.exists():
dirIconShapes = path
uiFile.open( QFile.ReadOnly )
buff = QByteArray( uiFile.readAll() )
uiFile.close()
else:
print "UI file missing! Exiting..."
exit(-1)
fixXML( path, buff )
qbuff = QBuffer()
qbuff.open( QBuffer.ReadOnly | QBuffer.WriteOnly )
qbuff.write( buff )
qbuff.seek( 0 )
ui = loader.load( qbuff, parentWidget = getMayaWin() )
ui.path = path
return ui
def fixXML( path, qbyteArray ):
# first replace forward slashes for backslashes
if path[-1] != '/':
path += '/'
path = path.replace( "/", "\\" )
# construct whole new path with <pixmap> at the begining
tempArr = QByteArray( "<pixmap>" + path + "\\" )
# search for the word <pixmap>
lastPos = qbyteArray.indexOf( "<pixmap>", 0 )
while lastPos != -1:
qbyteArray.replace( lastPos, len( "<pixmap>" ), tempArr )
lastPos = qbyteArray.indexOf( "<pixmap>", lastPos + 1 )
return
class UIController:
def __init__( self, ui ):
ui.Transfer.clicked.connect( self.TransferAnimation )
ui.lUp.pressed.connect( self.moveLUP )
ui.lUp.released.connect( self.rMoveLUP )
ui.lDown.clicked.connect( self.moveLDOWN )
ui.lDelete.clicked.connect( self.removeSJointList )
ui.rUp.clicked.connect( self.moveRUP )
ui.rDown.clicked.connect( self.moveRDOWN )
ui.rDelete.clicked.connect( self.removeTJointList )
ui.rootLine.returnPressed.connect( self.fillSourceList )
ui.rootLine2.returnPressed.connect( self.fillTargetList )
self.ui = ui
ui.setWindowFlags( Qt.WindowStaysOnTopHint )
ui.show()
def rMoveLUP( self ):
deltaTimer = 0
def moveLUP( self ):
deltaTimer = 1
while deltaTimer != 0:
index = ui.SourceJointsList.currentRow()
item = ui.SourceJointsList.takeItem(index)
ui.SourceJointsList.insertItem(index - 1, item)
ui.SourceJointsList.setCurrentRow(index - 1)
def moveLDOWN( self ):
index = ui.SourceJointsList.currentRow()
item = ui.SourceJointsList.takeItem(index)
ui.SourceJointsList.insertItem(index + 1, item)
ui.SourceJointsList.setCurrentRow(index + 1)
def moveRUP( self ):
index = ui.TargetJointsList.currentRow()
item = ui.TargetJointsList.takeItem(index)
ui.TargetJointsList.insertItem(index - 1, item)
ui.TargetJointsList.setCurrentRow(index - 1)
def moveRDOWN( self ):
index = ui.TargetJointsList.currentRow()
item = ui.TargetJointsList.takeItem(index)
ui.TargetJointsList.insertItem(index + 1, item)
ui.TargetJointsList.setCurrentRow(index + 1)
def TransferAnimation( self ):
"""Make list out of widget items"""
counter = ui.SourceJointsList.count()
for i in range(counter):
item = ui.SourceJointsList.item(i)
takeSList.append(item.text())
item = ui.TargetJointsList.item(i)
takeTList.append(item.text())
item = None
"""Replace string items with joint items"""
newTList = replaceList(takeTList, targetJoints)
newSList = replaceList(takeSList, sourceJoints)
"""Get inversed bind poses"""
getBindPoses(newSList, sBindPoseInversed)
getBindPoses(newTList, tBindPoseInversed)
"""Calculate joint parent matrix"""
assembleTheParentMatrixes(newSList, sParentMatrixes)
assembleTheParentMatrixes(newTList, tParentMatrixes)
for x in range(keyCount):
print "KEY FRAME: " + str(x) + "!"
pm.currentTime(x, edit=True)
setJointRotation(newSList, newTList, x, sParentMatrixes, tParentMatrixes)
def fillSourceList( self ):
for i in range(len(sourceJoints)):
jointName = str(sourceJoints[i])
ui.SourceJointsList.addItem(jointName)
def fillTargetList( self ):
for i in range(len(targetJoints)):
jointName = str(targetJoints[i])
ui.TargetJointsList.addItem(jointName)
def removeSJointList( self ):
index = ui.SourceJointsList.currentRow()
item = ui.SourceJointsList.takeItem(index)
skipSJoints.append(item.text())
item = None
def removeTJointList( self ):
index = ui.TargetJointsList.currentRow()
item = ui.TargetJointsList.takeItem(index)
skipTJoints.append(item.text())
item = None
ui = loadUI('Path to the .ui file')
cont = UIController(ui)