-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
198 lines (152 loc) · 5.71 KB
/
utils.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
import pymel.core as pmc
import maya.cmds as cmds
import maya.OpenMayaUI as omui
from shiboken2 import wrapInstance
from PySide2 import QtGui, QtWidgets
import time
def getMayaWindow():
winptr = omui.MQtUtil.mainWindow()
if winptr is None:
raise RuntimeError('No maya window found.')
window = wrapInstance(long(winptr), QtWidgets.QWidget)
return window
def firtsOrDefault(seq, predicate=None, default=None):
for each in seq:
if predicate is None or predicate(each):
return each
return default
def head(seq, count):
lst = []
for each in seq:
if len(lst) == count:
break
else:
lst.append(each)
return lst
def tail(seq, count):
lst = list(seq)
return lst[-count:]
def isExactType(node, typename):
"""node.type() == typename"""
return node.type() == typename
def isType(node, typename):
"""Return True if node.type() is typename or
any subclass of typename."""
return typename in node.nodeType(inherited=True)
def ancestors(node):
"""Return a list of ancestors, starting with the direct parent
and ending with the top-level (root) parent."""
result = []
parent = node.getParent()
while parent is not None:
result.append(parent)
parent = parent.getParent()
return result
def uniqueroots(nodes): #(1)
"""Returns a list of the nodes in `nodes` that are not
children of any node in `nodes`."""
result = []
def handle_node(n): #(2)
"""If any of the ancestors of n are in realroots,
just return, otherwise, append n to realroots.
"""
for ancestor in ancestors(n):
if ancestor in nodes: #(4)
return
result.append(n) #(5)
for node in nodes: #(3)
handle_node(node)
return result
# context managers and decorators.
class undoChunk(object):
'''Context manager'''
def __enter__(self):
pmc.undoInfo(openChunk=True)
def __exit__(self, *exc_info):
pmc.undoInfo(closeChunk=True)
def chunkUndo(func):
def inner(*args, **kwargs):
pmc.undoInfo(openChunk=True)
try:
return func(*args, **kwargs)
except RuntimeError as ex:
print(ex)
finally:
pmc.undoInfo(closeChunk=True)
return inner
class undoOnError(object):
def __enter__(self):
pmc.undoInfo(openChunk=True)
def __exit__(self, exc_type, exc_val, exc_tb):
pmc.undoInfo(closeChunk=True)
if exc_val is not None:
pmc.undo()
class atTime(object):
def __init__(self, t):
self.t = t
self.oldt = None
def __enter__(self):
self.oldt = pmc.getCurrentTime()
pmc.setCurrentTime(self.t)
def __exit__(self, *_):
if self.oldt is not None:
pmc.setCurrentTime(self.oldt)
class setFilePrompt(object):
def __init__(self, value):
self.value = value
self.buffer = None
def __enter__(self):
self.buffer = cmds.file(query=True, prompt=True)
cmds.file(prompt=self.value)
def __exit__(self, *_):
cmds.file(prompt=self.buffer)
class AnimationLayers(object):
def __init__(self, *layers):
self.layers = layers
self.bufferLayers = None
def __enter__(self):
self.bufferLayers = self._getActiveLayers()
for each in self._getAnimLayers():
if each.name() not in self.layers:
pmc.mel.eval('animLayerMuteCallBack "{0}" "1";'.format(each.name()))
pmc.animLayer(each, edit=True, mute=True, lock=True)
else:
pmc.mel.eval('animLayerMuteCallBack "{0}" "1";'.format(each.name()))
pmc.animLayer(each, edit=True, mute=False, lock=False)
pmc.mel.eval('updateEditorFeedbackAnimLayers("AnimLayerTab")')
def __exit__(self, *_):
if self.bufferLayers:
for each in self._getAnimLayers():
if each in self.bufferLayers:
pmc.mel.eval('animLayerMuteCallBack "{0}" "1";'.format(each.name()))
pmc.animLayer(each.name(), edit=True, lock=True)
pmc.animLayer(each.name(), edit=True, mute=True)
else:
pmc.mel.eval('animLayerMuteCallBack "{0}" "1";'.format(each.name()))
pmc.animLayer(each.name(), edit=True, lock=False)
pmc.animLayer(each.name(), edit=True, mute=False)
pmc.mel.eval('updateEditorFeedbackAnimLayers("AnimLayerTab")')
print(_)
def _getActiveLayers(self):
animLayers = self._getAnimLayers()
return [layer for layer in animLayers if not self._isMuted(layer)]
def _getAnimLayers(self, predicate=None):
baseAnimLayer = self._getBaseAnimLayer()
animLayers = []
if baseAnimLayer:
animLayers.append(baseAnimLayer)
childrenLayers = pmc.animLayer(baseAnimLayer, query=True, children=True)
animLayers.extend(childrenLayers)
return animLayers
def _getBaseAnimLayer(self):
return pmc.animLayer(query=True, root=True)
def _isMuted(self, layer):
return pmc.animLayer(layer, query=True, mute=True)
def readDuration(func):
def inner(*args, **kwargs):
startTime = time.clock()
result = func(*args, **kwargs)
endTime = time.clock()
print('{0} took {1} in execution'.format(func.__name__, (endTime-startTime)))
return result
return inner