/
mirrorAnimation.py
310 lines (233 loc) · 9.12 KB
/
mirrorAnimation.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
import os
import pymel.core as pm
from maya import OpenMayaUI as omui
try:
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2 import __version__
from shiboken2 import wrapInstance
except ImportError:
from PySide.QtCore import *
from PySide.QtGui import *
from PySide import __version__
from shiboken import wrapInstance
# TODO
# Get the Maya window so we can parent our widget to it.
mayaMainWindowPtr = omui.MQtUtil.mainWindow()
mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QWidget)
def xformMirror(transforms=[], across='YZ', behaviour=True):
""" Mirrors transform across hyperplane.
transforms -- list of Transform or string.
across -- plane which to mirror across.
behaviour -- bool
"""
# No specified transforms, so will get selection
if not transforms:
transforms = pm.selected(type='transform')
# Check to see all provided objects is an instance of pymel transform node,
elif not all(map(lambda x: isinstance(x, pm.nt.Transform), transforms)):
raise ValueError("Passed node which wasn't of type: Transform")
# Validate plane which to mirror across,
if not across in ('XY', 'YZ', 'XZ'):
raise ValueError("Keyword Argument: 'across' not of accepted value ('XY', 'YZ', 'XZ').")
for transform in transforms:
# Get the worldspace matrix, as a list of 16 float values
mtx = pm.xform(transform, q=True, ws=True, m=True)
# Invert rotation columns,
rx = [n * -1 for n in mtx[0:9:4]]
ry = [n * -1 for n in mtx[1:10:4]]
rz = [n * -1 for n in mtx[2:11:4]]
# Invert translation row,
t = [n * -1 for n in mtx[12:15]]
# Set matrix based on given plane, and whether to include behaviour or not.
if across is 'XY':
mtx[14] = t[2] # set inverse of the Z translation
# Set inverse of all rotation columns but for the one we've set translate to.
if behaviour:
mtx[0:9:4] = rx
mtx[1:10:4] = ry
elif across is 'YZ':
mtx[12] = t[0] # set inverse of the X translation
if behaviour:
mtx[1:10:4] = ry
mtx[2:11:4] = rz
else:
mtx[13] = t[1] # set inverse of the Y translation
if behaviour:
mtx[0:9:4] = rx
mtx[2:11:4] = rz
def mirrorMatrix(mtx, plane):
""" """
if not isinstance(mtx, pm.dt.Matrix):
raise ValueError("mtx must be a PyMel Matrix")
if not isinstance(plane, str):
raise ValueError("Plane must be of type (str)")
if plane.upper() in ('X', 'Y', 'Z'):
raise ValueError("Plane must be either of X, Y or Z")
pass
class Window(QWidget):
def __init__(self, parent=mayaMainWindow):
super(Window, self).__init__(parent=parent)
if os.name is 'posix':
self.setWindowFlags(Qt.Tool)
else:
self.setWindowFlags(Qt.Window)
self.setWindowTitle("Mirror Animation Tool")
# Integer for jobnumber of scripjob we're going to create,
self.jobNumber = None
layout = QGridLayout()
self.table = QTableWidget()
self.table.setColumnCount(1)
# Set column width to 100%
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.table.verticalHeader().setVisible(False)
sel = pm.selected()
self.table.setHorizontalHeaderItem(
0,
QTableWidgetItem(sel[0].nodeName() if sel else "Driver")
)
layout.addWidget(self.table, 0, 0, 1, 2, Qt.AlignHCenter)
offsetLayout = QHBoxLayout()
self.offset = QLineEdit()
self.offset.setValidator(QDoubleValidator())
self.setDefaultOffset()
offsetLayout.addWidget(QLabel("Mirror Offset:"))
offsetLayout.addWidget(self.offset)
layout.addLayout(offsetLayout, 1, 0, 1, 2, Qt.AlignHCenter)
btnLayout = QHBoxLayout()
# Mirror Button
self.mirrorBtn = QPushButton("Mirror")
self.mirrorBtn.clicked.connect(self.mirror)
# Invert Curves
self.invertBtn = QPushButton("Invert")
self.invertBtn.clicked.connect(self.invert)
btnLayout.addWidget(self.mirrorBtn)
btnLayout.addWidget(self.invertBtn)
layout.addLayout(btnLayout, 2, 0, 1, 2, Qt.AlignHCenter)
self.setLayout(layout)
def setDefaultOffset(self):
""" Assume half the timerange is to be equal to desired offset time,"""
minTime = pm.playbackOptions(query=True, minTime=True)
maxTime = pm.playbackOptions(query=True, maxTime=True)
self.offset.insert(
str((maxTime - minTime) / 2)
)
def animatedAttributes(self, node):
""" Return a list of tuples pairs for all animated attributes,
with AnimationCurve and nice name,
"""
# attributes = [(attr, attr.split('_')[-1]) for attr in pm.keyframe(node, attribute=pm.listAttr(node, keyable=True), query=True, name=True)]
attributes = list()
animationCurves = pm.keyframe(
node,
query=True,
name=True,
attribute=pm.listAttr(node, keyable=True)
)
for curve in animationCurves:
attributes.append(
(curve, curve.split('_')[-1])
)
return attributes
def populateColumn(self, column, header, attributes):
""" """
headerItem = QTableWidgetItem(header)
self.table.setHorizontalHeaderItem(column, headerItem)
for row, attribute in enumerate(attributes):
# Unpack attribute,
animCurve, attrName = attribute
attributeItem = AnimationCurveItem(animCurve, attrName)
attributeItem.setFlags(attributeItem.flags() ^ Qt.ItemIsEditable)
self.table.setItem(row, column, attributeItem)
def callback(self):
""" """
sel = pm.selected()
if sel:
# Set column header to first selection,
header = sel[0].nodeName()
# Get all animated attributes,
attributes = self.animatedAttributes(sel[0])
self.table.setRowCount(len(attributes))
self.populateColumn(0, header, attributes)
else:
self.table.setHorizontalHeaderItem(0, QTableWidgetItem("Driver"))
self.table.setRowCount(0)
def createScriptJob(self):
""" Maya script job, run callback whenever selection changes, """
jobNumber = pm.scriptJob(event=('SelectionChanged', self.callback))
return jobNumber
def mirror(self):
""" Copy the animation curves from first to second selection item, """
sel = pm.selected()
if len(sel) == 2:
pm.copyKey(
sel[0]
)
pm.pasteKey(
sel[1],
timeOffset=float(self.offset.text()),
option='replace',
)
def invert(self):
""" Invert animation curves for current selection, """
items = self.table.selectedItems()
for item in items:
self.invertCurve(item.animationCurve)
def showEvent(self, e):
""" On show create a Maya scripjob unless one already exists for some reason,"""
if self.jobNumber and pm.scriptJob(exists=self.jobNumber):
# ScriptJob already seem to exist, so let's do nothing
pass
else:
self.createScriptJob()
def closeEvent(self, e):
""" Remove scriptjob, """
if self.jobNumber and pm.scriptJob(exists=self.jobNumber):
pm.scriptJob(kill=self.jobNumber)
self.jobNumber = None
def offsetCurve(self, animationCurve, offset):
""" Move the animation curve relative in time to offset, """
pm.keyframe(
animationCurve,
edit=True,
includeUpperBound=True,
relative=True,
option='over',
timeChange=offset,
)
def invertCurve(self, animationCurve):
""" Scale the values by negative one, """
pm.scaleKey(
animationCurve,
valueScale=-1,
valuePivot=0,
)
def getAnimcurveLength(self, animationCurve):
""" Get the duration for animation curve,
max key - min key,
"""
keys = pm.keyframe(
animationCurve,
query=True,
timeChange=True
)
return keys[-1] - keys[0]
def getKeysInCurve(self, animationCurve):
""" Get a list of tuple pairs (key, value) in animationCurve, """
data = pm.keyframe(
animationCurve,
query=True,
valueChange=True,
timeChange=True,
)
return data
class AnimationCurveItem(QTableWidgetItem):
""" Subclassing Table Item to store reference to animation curves, """
def __init__(self, animationCurve, *args, **kwargs):
super(AnimationCurveItem, self).__init__(*args, **kwargs)
self.animationCurve = animationCurve
def ui():
window = Window()
window.show()
return window