-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpluginfo.py
489 lines (396 loc) · 13.8 KB
/
pluginfo.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
"""
Internal. Contains routines to get information on
:class:`~maya.OpenMaya.MPlug` instances and administer abstract Paya plug
classes.
"""
import re
import maya.OpenMaya as om
import maya.cmds as m
import pymel.core as p
from paya.util import uncap
from paya.apiutil import enumIndexToKey
#------------------------------------------------------------|
#------------------------------------------------------------| Constants
#------------------------------------------------------------|
geoTypes = [
'NurbsCurve',
'BezierCurve',
'NurbsSurface',
'Mesh',
'Subdiv',
'Lattice',
'PluginGeometry',
'SubdSurface',
'DynSweptGeometry'
]
#------------------------------------------------------------|
#------------------------------------------------------------| Parse MFn enums
#------------------------------------------------------------|
def parseMFnEnums():
"""
Runs on startup.
:return: A mapping of ``{enum name: paya class name}`` for every enum on
:class:`maya.OpenMaya.MFn`
:rtype: :class:`dict`
"""
# Include all enums which may be accessed as modified keys
# from the tree, even if they have no meaningful classification
# (i.e. they'll be put directly under Attribute)
out = {} # MFn enum key: tree key
enums = filter(lambda x: x.startswith('k'), om.MFn.__dict__)
for enum in enums:
#---------------| kAttribute3Float, kData3Float...
mt = re.match(
r"^k(?:Attribute|Data)([2-4])(.*)$",
enum
)
if mt:
dim, key = mt.groups()
key += dim
out[enum] = key
continue
#---------------| kNurbsCurveData, kMessageAttribute...
if 'Array' not in enum:
mt = re.match(
r"k(.+)(Attribute|Data)$",
enum
)
if mt:
key = mt.groups()[0]
out[enum] = key
return out
def parseMFnDataEnums():
"""
Runs on startup.
:return: A mapping of ``{enum name: paya class name}`` for every enum on
:class:`maya.OpenMaya.MFnData`
:rtype: :class:`dict`
"""
enums = filter(lambda x: x.startswith('k'), om.MFnData.__dict__)
enums = list(filter(lambda x: x != 'kLast' and 'Array' not in x, enums))
keys = list(map(lambda x: x[1:], enums))
return dict(zip(enums, keys))
def parseMFnNumericDataEnums():
"""
Runs on startup.
:return: A mapping of ``{enum name: paya class name}`` for every enum on
:class:`maya.OpenMaya.MFnNumericData`
:rtype: :class:`dict`
"""
out = {}
enums = filter(lambda x: x.startswith('k'), om.MFnNumericData.__dict__)
for enum in enums:
mt = re.match(
r"k([2-4])(.*)$",
enum
)
if mt:
dim, key = mt.groups()
key += dim
out[enum] = key
continue
key = enum[1:]
if key != 'Last':
out[enum] = enum[1:]
return out
def parseMFnUnitAttributeEnums():
"""
Runs on startup.
:return: A mapping of ``{enum name: paya class name}`` for every enum on
:class:`maya.OpenMaya.MFnUnitAttribute`
:rtype: :class:`dict`
"""
out = {}
enums = filter(lambda x: x.startswith('k'), om.MFnUnitAttribute.__dict__)
for enum in enums:
if enum != 'kLast':
out[enum] = enum[1:]
return out
mFnEnumsToTreeKeys = parseMFnEnums()
mFnDataEnumsToTreeKeys = parseMFnDataEnums()
mFnNumericDataEnumsToTreeKeys = parseMFnNumericDataEnums()
mFnUnitAttributeEnumsToTreeKeys = parseMFnUnitAttributeEnums()
tree = {
'Attribute': {
'Math': {
'Math1D': {
'Unit': {
'Angle': {},
'Time': {},
'Distance': {}
},
},
'Math2D': {},
'Vector': {
'EulerRotation': {}, # if 3 compound children are all angles
'Point': {} # if 3 compound children are all linear
},
'Quaternion': {},
'Matrix': {}
},
'Geometry': {
'NurbsCurve': {'BezierCurve': {}},
'NurbsSurface': {},
'DynSweptGeometry': {},
'Lattice': {},
'Mesh': {},
'PluginGeometry': {},
'Sphere': {},
'Box': {},
'Subdiv': {}
},
'Array': {}
}
}
def _getPath(key, invent=True):
global tree
buffer = {'result': None}
def parse(history):
dct = tree
for _key in history:
dct = dct[_key]
for _key in dct:
possiblePath = history + [_key]
if _key == key:
buffer['result'] = possiblePath
else:
parse(possiblePath)
parse([])
result = buffer['result']
if result:
return result
if invent:
return ['Attribute', key]
raise KeyError(
"Key '{}' is not in the plug tree.".format(key)
)
def parsePerKeyInfo():
"""
Runs on startup.
:return: A ``{paya class name: info dict}`` mapping, where ``info``
comprises:
::
{
'mathDimension': int, # e.g. 3; may be omitted
'type': list, # the class path,
'mathUnitType': str # one of 'angle', 'distance', 'time'; may be omitted
}
:rtype: :class:`dict`
"""
global mFnEnumsToTreeKeys
global mFnDataEnumsToTreeKeys
global mFnNumericDataEnumsToTreeKeys
global mFnUnitAttributeEnumsToTreeKeys
keys = list(mFnEnumsToTreeKeys.values())
keys += list(mFnDataEnumsToTreeKeys.values())
keys += list(mFnNumericDataEnumsToTreeKeys.values())
keys += list(mFnUnitAttributeEnumsToTreeKeys.values())
keys = list(set(keys))
out = {}
for key in keys:
parent = mathDimension = mathUnitType = None
# DoubleAngle, FloatLinear
mt = re.match(
r"^.+(Linear|Angle)$",
key
)
if mt:
typ = mt.groups()[0]
parent = {'Linear':'Distance'}.get(typ, typ)
mathUnitType = uncap(parent)
mathDimension = 1
else:
# Double3...
mt = re.match(r"^[^0-9]+([0-9])$", key)
if mt:
mathDimension = int(mt.groups()[0])
parent = {
2: 'Math2D',
3: 'Vector',
4: 'Quaternion'
}[mathDimension]
else:
if key in ['Addr', 'Boolean', 'Byte', 'Char',
'Double', 'Enum', 'Float', 'Int',
'Int64', 'Long', 'Numeric', 'Short']:
mathDimension = 1
parent = 'Math1D'
elif key == 'Matrix':
parent = 'Math'
mathDimension = 16
elif key in ['Matrix', 'FloatMatrix', 'MatrixFloat']:
parent = 'Matrix'
mathDimension = 16
else:
if key in geoTypes:
parent = 'Geometry'
if key == 'BezierCurve':
parent = 'NurbsCurve'
else:
if re.match(r"^.+Array$", key):
parent = 'Array'
else:
if key in ['Time', 'Distance', 'Angle']:
mathDimension = 1
mathUnitType = uncap(key)
parent = 'Unit'
# Construct path
if parent is None:
path = _getPath(key, invent=True)
else:
path = _getPath(parent, invent=False) + [key]
# path = []
# if parent is None:
# path.append('Attribute')
# else:
# path += _getPath(parent, invent=False)
#
# path.append(key)
inf = {'type': path}
if mathDimension is not None:
inf['mathDimension'] = mathDimension
if mathUnitType is not None:
inf['mathUnitType'] = mathUnitType
out[key] = inf
return out
perKeyInfo = parsePerKeyInfo()
def getPath(key, invent=True):
"""
:param str key: the name of a Paya class for which to retrieve
an inheritance path
:param bool invent: if the class is not classified, invent a basic
classification; defaults to ``True``
:return: The inheritance path.
:rtype: :class:`list` [:class:`str`]
"""
global perKeyInfo
try:
return perKeyInfo[key]['type']
except KeyError as exc:
return _getPath(key, invent=invent)
#------------------------------------------------------------|
#------------------------------------------------------------| Instance analysis
#------------------------------------------------------------|
def getInfoFromMPlug(mplug):
if mplug.isArray():
mplug = mplug.elementByLogicalIndex(0)
if mplug.isCompound():
numChildren = mplug.numChildren()
if numChildren in (2, 3, 4):
children = [mplug.child(x) for x in range(mplug.numChildren())]
childInfos = [getInfoFromMPlug(child) for child in children]
mathDimensions = [childInfo.get(
'mathDimension') for childInfo in childInfos]
if all([mathDimension is 1 for mathDimension in mathDimensions]):
if numChildren is 4:
return {
'type':getPath('Quaternion', invent=False),
'mathDimension': 4
}
if numChildren is 3:
mathUnitTypes = [childInfo.get(
'mathUnitType', '') for childInfo in childInfos]
if all([mathUnitType == 'distance' for mathUnitType in mathUnitTypes]):
return {
'type':getPath('Point', invent=False),
'mathDimension': 3
}
elif all([mathUnitType == 'angle' for mathUnitType in mathUnitTypes]):
return {
'type':getPath('EulerRotation', invent=False),
'mathDimension': 3
}
else:
return {
'type':getPath('Vector', invent=False),
'mathDimension': 3
}
return {
'type':getPath('Math{}D'.format(numChildren), invent=False),
'mathDimension': numChildren
}
return {'type':getPath('Compound')}
mobj = mplug.attribute()
if mobj.hasFn(om.MFn.kNumericAttribute):
mfn = om.MFnNumericAttribute(mobj)
subtype = mfn.unitType()
subtypeStr = enumIndexToKey(subtype, om.MFnNumericData)
key = mFnNumericDataEnumsToTreeKeys[subtypeStr]
return perKeyInfo[key]
if mobj.hasFn(om.MFn.kUnitAttribute):
mfn = om.MFnUnitAttribute(mobj)
subtypeStr = enumIndexToKey(mfn.unitType(), om.MFnUnitAttribute)
key = mFnUnitAttributeEnumsToTreeKeys[subtypeStr]
return perKeyInfo[key]
if mobj.hasFn(om.MFn.kTypedAttribute):
try:
dataObj = mplug.asMObject()
dataTypeStr = dataObj.apiTypeStr()
key = mFnEnumsToTreeKeys[dataTypeStr]
return perKeyInfo[key]
except RuntimeError:
mfn = om.MFnTypedAttribute(mobj)
attrType = mfn.attrType()
key = mFnDataEnumsToTreeKeys[enumIndexToKey(attrType, om.MFnData)]
return perKeyInfo[key]
if mobj.hasFn(om.MFn.kGenericAttribute):
try:
dataObj = mplug.asMObject()
dataTypeStr = dataObj.apiTypeStr()
key = mFnEnumsToTreeKeys[dataTypeStr]
return perKeyInfo[key]
except RuntimeError:
mhandle = mplug.asMDataHandle()
typ = mhandle.type()
typeStr = enumIndexToKey(typ, om.MFnData)
key = mFnDataEnumsToTreeKeys[typeStr]
return perKeyInfo[key]
if mobj.hasFn(om.MFn.kMatrixAttribute):
return perKeyInfo['Matrix']
elif mobj.hasFn(om.MFn.kEnumAttribute):
return {'type':getPath('Enum'), 'mathDimension': 1}
elif mobj.hasFn(om.MFn.kMessageAttribute):
return {'type':getPath('Message')}
raise RuntimeError(
"Could not parse plug: {}".format(mplug.name())
)
def getInfoFromAttr(pmattr):
return getInfoFromMPlug(pmattr.__apimplug__())
#------------------------------------------------------------|
#------------------------------------------------------------| Unit testing
#------------------------------------------------------------|
def unitTest(goto=True):
if goto:
path = 'C:/Users/user/Desktop/unittest.ma'
p.openFile(path, f=1)
plugs = [
# 'persp.t',
# 'persp.r',
# 'persp.s',
# 'persp.tx',
# 'persp.rx',
# 'persp.sx',
# 'polyCube1.output',
# 'pCubeShape1.worldMesh',
# 'pCubeShape1.worldMesh[0]',
# 'curve1.local',
# 'unitConversion1.input',
# 'unitConversion1.output',
# 'locator1.notes',
# 'choice1.selector',
# 'choice1.input[0]',
# 'choice1.output',
# 'persp.matrix',
# 'quatAdd1.outputQuat',
# 'locator1.customMatrix',
# 'bezier1.local',
'persp.worldMatrix',
'persp.worldMatrix[0]',
'persp.rotateOrder'
]
for plug in plugs:
node, plug = plug.split('.')
node = p.PyNode(node)
plug = node.attr(plug)
print('\nPlug is ', plug)
print('Result is: {}'.format(getInfoFromAttr(plug)))