-
Notifications
You must be signed in to change notification settings - Fork 0
/
channelbox.py
413 lines (332 loc) · 12.8 KB
/
channelbox.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
import pymel.core as pm
# Functions #########################################################################
def setDefault(nodes, ignores=tuple()):
"""Set the node(s) channelBox attributes(s) to there default value if there is.
Log a warning message if a RuntimeError occur during the set value process.
:param nodes:
OR Maya node(s) name.
OR PyNode instance(s) of Maya node(s).
:type nodes:
OR iterable of basestring
OR iterable of pymel.core.PyNode
:param ignores: Attribute(s) name to ignore during the process,
defaults to tuple()
:type ignores: Iterable of basestring, optional
:Example:
import maya.cmds as mc
import rigIO.channelBox
rigIO.channelBox.setDefault(mc.ls(sl=True), ['v'])
"""
for node in nodes:
channelBox = ChannelBox(node, *ignores)
channelBox.setDefault()
def connect(source, destinations, ignores=tuple()):
"""Connect current channelBox attribute(s) to the destination(s)
channelBox attribute(s). Log a warning message if a RuntimeError occurs
during the connection process.
:param source:
OR Maya node name.
OR PyNode instance of Maya node.
:type source:
OR basestring
OR pymel.core.PyNode
:param destinations:
OR Maya node(s) name.
OR PyNode instance(s) of Maya node(s).
:type destinations:
OR iterable of basestring
OR iterable of pymel.core.PyNode
:param ignores: Attribute(s) name to ignore during the process,
defaults to tuple()
:type ignores: Iterable of basestring, optional
:Example:
import maya.cmds as mc
import rigIO.channelBox
sl = mc.ls(sl=True)
rigIO.channelBox.connect(sl[0], sl[1:], ['v'])
"""
sourceChannelBox = ChannelBox(source, *ignores)
sourceChannelBox.connect(*destinations)
def disconnect(nodes, ignores=tuple()):
"""Disconnect the node(s) channelBox attribute(s).
Log a warning error if there is no incoming connection(s).
:param nodes:
OR Maya node(s) name.
OR PyNode instance(s) of Maya node(s).
:type nodes:
OR iterable of basestring
OR iterable of pymel.core.PyNode
:param ignores: Attribute(s) name to ignore during the process,
defaults to tuple()
:type ignores: Iterable of basestring, optional
:Example:
import maya.cmds as mc
import rigIO.channelBox
rigIO.channelBox.disconnect(mc.ls(sl=True), ['v'])
"""
for node in nodes:
channelBox = ChannelBox(node, *ignores)
channelBox.disconnect()
def setLocked(nodes, value, ignores=tuple()):
"""Delete the animation key(s) on the node chennelBox attribute(s).
:param nodes:
OR Maya node(s) name.
OR PyNode instance(s) of Maya node(s).
:type nodes:
OR iterable of basestring
OR iterable of pymel.core.PyNode
:param value:
True - Lock the channelBox attribute(s).
False - Unlock the channelBox attribute(s).
:type value: bool
:param ignores: Attribute(s) name to ignore during the process,
defaults to tuple()
:type ignores: Iterable of basestring, optional
:Example:
import maya.cmds as mc
import rigIO.channelBox
rigIO.channelBox.setLocked(mc.ls(sl=True), True, ['v'])
"""
for node in nodes:
channelBox = ChannelBox(node, *ignores)
channelBox.setLocked(value)
def clearKeys(nodes, ignores=tuple()):
"""Delete the animation key(s) on the node chennelBox attribute(s).
:param nodes:
OR Maya node(s) name.
OR PyNode instance(s) of Maya node(s).
:type nodes:
OR iterable of basestring
OR iterable of pymel.core.PyNode
:param ignores: Attribute(s) name to ignore during the process,
defaults to tuple()
:type ignores: Iterable of basestring, optional
:Example:
import maya.cmds as mc
import rigIO.channelBox
rigIO.channelBox.clearKeys(mc.ls(sl=True), ['v'])
"""
for node in nodes:
channelBox = ChannelBox(node, *ignores)
channelBox.clearKeys()
# Class #############################################################################
class ChannelBox(tuple):
"""
Class to manage a node channelBox attributes.
"""
def __new__(cls, node, *ignores):
"""__new__(node, *ignores) method of builtins.type instance
Create and return a new object.
:param node:
OR Maya node name.
OR PyNode instance.
:type node:
OR str
OR pymel.core.PyNode
:param *ignores: Attribute(s) name to ignore during the process.
:type *ignores: str
"""
cls.node = pm.PyNode(node) if isinstance(node, basestring) else node
cls._ignores = ignores
content = tuple()
for attr in cls.node.listAttr():
if attr.isKeyable() or attr.isInChannelBox():
content += attr,
return tuple.__new__(cls, content)
def __repr__(self):
"""repr(x) <-> x.__repr__()
"""
return repr(tuple(i.longName() for i in self._filter()))
def __getitem__(self, index):
"""x.[0] <-> x.__getitem__(0)
:param index:
OR Index number of the wanted attribute.
OR Name of the wanted attribute.
OR Pymel attribute.
:type index:
OR int
OR basestring
OR pymel.core.PyNode
:returns: Attribute at the given index.
:rtype: pymel.core.PyNode
:raises: KeyError, TypeError
"""
if isinstance(index, int):
return super(ChannelBox, self).__getitem__(index)
elif isinstance(index, pm.general.Attribute):
for attr in self:
if index.shortName() == attr.shortName():
return attr
raise KeyError(index)
elif isinstance(index, basestring):
for attr in self:
if index in (attr.longName(), attr.shortName()):
return attr
raise KeyError(index)
else:
errorMessage = '%s indices must be integers ' % self.__class__.__name__
errorMessage += 'string or pymel.core.general.Attribute. '
errorMessage += 'Gets %s' % type(index)
raise TypeError(errorMessage)
def __contains__(self, key):
"""y in x <-> x.__contains__(y)
"""
if not isinstance(key, pm.general.Attribute):
return super(ChannelBox, self).__contains__(key)
else:
for attr in self:
if key.longName() == attr.longName():
return True
return False
def _filter(self):
"""Gets the node channelBox attribute(s) after the ignores filter.
:returns: Pymel attributes.
:rtype: generator of pymel.core.PyNode
"""
for attr in self:
short_name_in_ignores = attr.shortName() in self.ignores
long_name_in_ignores = attr.longName() in self.ignores
if not (short_name_in_ignores or long_name_in_ignores):
yield attr
@property
def ignores(self):
"""x.ignores
Gets the attribute(s) name to ignore during the process.
:returns: Attribute(s) name.
:rtype: Iterable of basestring
"""
return self._ignores
@ignores.setter
def ignores(self, value):
"""x.ignore = value
Sets the attribute(s) name to ignore during the process.
:param value:
OR Attribute(s) name.
OR None
:type value:
OR Iterable of basestring
OR None
"""
if value is None:
value = tuple()
self._ignores = value
def get(self, key, default=None):
"""x.get(key, default) -> x[key] if key in x, else default.
:param key:
OR Index number of the wanted attribute.
OR Name of the wanted attribute.
OR Pymel attribute.
:type key:
OR int
OR basestring
OR pymel.core.PyNode
:param default: Object to return if key isn't in self.
:type default: optional
:returns:
OR Pymel attribute at the given key.
OR Default parameter value.
:rtype:
OR pymel.core.general.Attribute
OR type(param default)
:Example:
from rigIO.channelBox import ChannelBox
channelBox = ChannelBox('nodeName')
channelBox.get('attributeName')
"""
return self[key] if key in self else default
def setDefault(self):
"""Set the node channelBox attributes(s) to there default value if there is.
Log a warning message if a RuntimeError occur during the set value process.
:Example:
from rigIO.channelBox import ChannelBox
channelBox = ChannelBox('nodeName')
channelBox.setDefault()
"""
for attr in self._filter():
if attr.attrName() in ('tx', 'ty', 'tz', 'rx', 'ry', 'rz'):
defaultValue = 0
elif attr.attrName() in ('sx', 'sy', 'sz', 'v'):
defaultValue = 1
else:
defaultValue = pm.addAttr(attr, query=True, defaultValue=True)
if not (defaultValue is None):
try:
attr.set(defaultValue)
except RuntimeError as message:
pm.warning(message.message[:-1])
def connect(self, *destinations):
"""Connect current channelBox attribute(s) to the destination(s)
channelBox attribute(s). Log a warning message if a RuntimeError occurs
during the connection process.
:param *destinations:
OR Maya node name of the wanted destination(s).
OR PyNode instance of the wanted destination(s).
OR ChannelBox instance of the wanted destination(s).
:type *destinations:
OR str
OR pymel.core.PyNode
OR ChannelBox
:Example:
import pymel.core as pm
from rigIO.channelBox import ChannelBox
channelBox = ChannelBox('nodeName')
# With str param type :
sourceChannelBox.connect('destination_a', 'destination_b')
# With pymel.core.PyNode param type :
destinationPyNodeA = pm.PyNode('destination_a')
destinationPyNodeB = pm.PyNode('destination_b')
sourceChannelBox.connect(destinationPyNodeA, destinationPyNodeB)
# With ChannelBox param type :
destinationChannelBoxA = ChannelBox('destination_a')
destinationChannelBoxB = ChannelBox('destination_b')
sourceChannelBox.connect(destinationChannelBoxA, destinationChannelBoxB)
"""
for channelBox in destinations:
if not isinstance(channelBox, ChannelBox):
channelBox = ChannelBox(channelBox)
for attr in channelBox._filter():
sourceAttr = self.get(attr)
if sourceAttr:
try:
sourceAttr >> attr
except RuntimeError as error:
pm.warning(error.message[:-1])
def disconnect(self):
"""Disconnect the node channelBox attribute(s).
Log a warning error if there is no incoming connection(s).
:Example:
from rigIO.channelBox import ChannelBox
channelBox = ChannelBox('nodeName')
channelBox.disconnect()
"""
for attr in self._filter():
sourceAttr = attr.listConnections(source=True, plugs=True)
sourceAttr = (sourceAttr or [None])[0]
if sourceAttr and sourceAttr.isSource():
sourceAttr // attr
else:
message = '%s has no incoming connections.' % attr.name()
pm.warning(message)
def setLocked(self, value):
"""Set the lock state of the node channelBox attribute(s).
:param value:
True - Lock the channelBox attribute(s).
False - Unlock the channelBox attribute(s).
:type value: bool
:Example:
from rigIO.channelBox import ChannelBox
channelBox = ChannelBox('nodeName')
channelBox.setLocked(True)
channelBox.setLocked(False)
"""
for attr in self._filter():
attr.setLocked(value)
def clearKeys(self):
"""Delete the animation key(s) on the node chennelBox attribute(s).
:Example:
from rigIO.channelBox import ChannelBox
channelBox = ChannelBox('nodeName')
channelBox.clearKeys()
"""
for attr in self._filter():
pm.cutKey(attr)