-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdagger.py
More file actions
558 lines (472 loc) · 17.3 KB
/
dagger.py
File metadata and controls
558 lines (472 loc) · 17.3 KB
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
import re
from distutils.util import strtobool
from maya import cmds
from maya.api import OpenMaya as om
from xml import etree
if not sys.version_info.major == 2:
from xml.etree import ElementTree as ET
dict_get_keys = lambda d: d.keys()
dict_get_values = lambda d: d.values()
dict_get_items = lambda d: d.items()
else:
from xml.etree import cElementTree as ET
dict_get_keys = lambda d: list(d)
dict_get_values = lambda d: list(d.values())
dict_get_items = lambda d: list(d.items())
unicode = str
basestring = str
from importlib import reload
from xml.dom import minidom
from . import utils
from . import options
class XMLmixin:
TAG_ROOT = options.TagOption.TAG_ROOT
TAG_NODE = options.TagOption.TAG_NODE
TAG_OPT = options.TagOption.TAG_OPT
@classmethod
def __create_new_element(cls, name, tag, options=None):
"""Private Method
Create New element tag data.
Args:
name (str): New TAG name attribute
tag (str): New TAG name.The constant is usually used.
options (dict, optional): can set any attributes in dictionary.
The key and value of the dictionary are set in the following attributes:
name: option's name
value: option's value.
type: option's value type.
Returns:
Element: Element data with the new TAG name created.
"""
ele = ET.Element(tag)
ele.set('name', name.split('|')[-1])
ele.set('fullpath', name)
if options:
for k, v in dict_get_items(options):
ele_opt = ET.SubElement(ele, cls.TAG_OPT)
ele_opt.set('name', k)
ele_opt.set('value', str(v))
match = re.search(r"'(.+?)'", str(type(v)))
type_name = match.group(1)
ele_opt.set('type', type_name)
return ele
@classmethod
def create_new_nodeTag(cls, name, options=None):
return cls.__create_new_element(name, cls.TAG_NODE, options)
@classmethod
def find_named_element(cls, ele, fullpathName, options=None):
"""
Find and return the Elementtag that has already been created.
Args:
ele (Element): Element data.
fullpathName (str): Full pass name to find.
options (dict, optional): can set any attributes in dictionary.
Returns:
Element: target Element data.
"""
split_node_list = fullpathName.split('|')[1:]
ele_curr = ele
target_fp = ''
for i_node in split_node_list:
target_fp += ''.join(['|', i_node])
ele_pre = ele_curr
for grp in ele_curr.findall(cls.TAG_NODE):
if grp.attrib['fullpath'] == target_fp:
ele_curr = grp
break
if ele_curr is ele_pre:
ele_new = cls.create_new_nodeTag(target_fp, options)
ele_curr.append(ele_new)
ele_curr = ele_new
return ele_curr
@staticmethod
def restore_option(options):
"""
Restore from xml options attrib.
Args:
options (list): options attrib list.
Returns:
dict: restore option dict.
"""
restore_opt = dict()
for i_opt in options:
k = i_opt.attrib['name']
v = i_opt.attrib['value']
typ = i_opt.attrib['type']
if typ == 'bool':
typ_eval = lambda x, y : x((strtobool(y)))
else:
typ_eval = lambda x, y : x(y)
if typ == 'NoneType':
restore_opt[k] = None
else:
restore_opt[k] = typ_eval(eval(typ), v)
return restore_opt
@staticmethod
def isEqual(xml1, xml2):
rootA = etree.fromstring(xml1)
rootB = etree.fromstring(xml2)
return etree.tostring(rootA) == etree.tostring(rootB)
class NTree(object):
"""
N-array Tree data class.
N-array tree is a tree data structure where each node can have at most N children.
It is a generalization of binary tree where each node can have at most 2 children.
+---+
| a |
+----+--+-+
| |
+---+ +---+
| b | | c |
+--+ + + +
| | | |
d1 d2 d3 d4
The above diagram shows a sample N-array tree where each node can have at most 3 children (N = 3).
The node "a" has 3 children "b", "c", and "d". The nodes "b" and "c" have 2 children each, while the
nodes "d1", "d2", "d3", and "d4" are leaf nodes with no children.
"""
def __init__(self, name, parent=None, opt=None):
super(NTree, self).__init__()
self.name = name
self.short_name = None
if name.startswith('|'):
self.short_name = name.split('|')[-1]
if parent is None:
parent = list()
self.childs = list()
self.parent = parent
if opt:
for k, v in dict_get_items(opt):
setattr(self, k, v)
self.opt = opt
def __str__(self):
return str(self.name)
def __call__(self, name, parent, opt=None):
return self.__class__(name, parent, opt=opt)
def has_children(self):
return True if self.childs else False
def has_parent(self):
return True if self.parent else False
def add(self, args, opt=None):
if not isinstance(args, (list, tuple)):
chs = [args]
else:
chs = args
if isinstance(chs[0], (list, tuple)):
chs = args[0]
rets = list()
for ch in chs:
tree_obj = self(ch, parent=self, opt=opt)
self.childs.append(tree_obj)
rets.append(tree_obj)
return rets
def printout(self, depth=0, long=False):
tab = "\t" * depth
if not long:
if self.short_name:
name = self.short_name
else:
name = self.name
else:
name = self.name
print(tab + name)
if len(self.childs) < 1:
return
for ch in self.childs:
ch.printout(depth + 1)
def find_add(self, par, name, opt=None):
if self.name != par:
for ch in self.childs:
ch.find_add(par, name, opt=opt)
else:
return self.add(name, opt=opt)
return 0
def find(self, par, name):
ret = None
if self.name != par:
for ch in self.childs:
ret = ch.find(par, name)
if ret:
return ret
else:
return self
def sibling(self):
if self.has_parent():
if self.parent.childs:
ch_list = self.parent.childs
sibling_list = list()
for i_ch in ch_list:
if i_ch.name == self.name:
continue
else:
sibling_list.append(i_ch)
return sibling_list
class DAGtree(NTree):
def __init__(self, name, parent=None, opt=None):
super(DAGtree, self).__init__(name, parent, opt)
@property
def fullpath(self):
return utils.getNode(self.name, long=True)
def restoreAttrs(self, attrs):
for attr in attrs:
if hasattr(self, attr):
print(self.opt)
val = self.opt[attr]
print(attr)
print(val)
if val is not None:
cmds.setAttr(self.name + '.' + attr, val)
print('Restore {}: '.format(attr) + self.name)
class DAGstructure(XMLmixin, object):
"""
DAG structure data class.
e.g.)
dag_tree = DAGstructure('Euclid')
# set root node name and construct dag hierarchy.
dag_tree.construct('all)
# print out.
dag_tree.output()
# get treeObj by name.
dag_pPlane1 = dag_tree.get_obj('pPlane1')
"""
def __init__(self, name, xml_path=None, attrib_opt=None, **opt):
super(DAGstructure, self).__init__()
self.name = name
self.opt = opt
self.root = None
if attrib_opt is None:
attrib_opt = options.attrib_opt
self.set_option_attrib(attrib_opt)
if xml_path:
self.set_xml_path(xml_path)
def __eq__(self, rhs):
if isinstance(rhs, DAGstructure):
return self.__dict__ == rhs.__dict__
else:
return False
def __ne__(self, rhs):
return not self.__eq__(rhs)
def set_option_attrib(self, opt):
self.attrib_opt = opt
def traverse(self, root='', topdown=True):
"""
Traverse all and generate DAG tree structure data.
Args:
root (str, optional): root node name. Defaults to ''.
topdown (bool, optional): Defaults to True.
"""
if root == '' or root is None:
root = self.root
childs = root.childs
childs_list = [i_child for i_child in childs] if childs else []
if topdown:
yield (root, childs_list)
for name in childs_list:
for x in self.traverse(name, topdown):
yield x
if not topdown:
yield (root, childs_list)
def construct(self, name, parent=''):
"""
Construct dag DAGtree data hierarchy from scene recursively.
Args:
name (str): top dag node name.
parent (str, optional): Use if you want to specify parent node. Defaults to ''.
"""
def _set_option_attrib(name, opt):
set_opt = dict()
for k , v_func in dict_get_items(opt):
set_opt[k] = v_func(name)
return set_opt
def _get_parent(nodes, fullpath_names):
"""
Get parent node from fullpath name list.
Args:
nodes (obj):
fullpath_names (node): fullpath
Returns:
_type_: _description_
"""
node = fullpath_names[-1]
for name in reversed(fullpath_names):
if name == node:
continue
if name in nodes:
return name
return None
opt = _set_option_attrib(name, self.attrib_opt)
self.root = DAGtree(name, opt=opt)
def __construct(dagPath, parent='', __construct=None):
fullpath_name = dagPath.fullPathName()
short_nodename = fullpath_name.split('|')[-1]
if parent != '':
_parent = _get_parent(utils.get_parentNode(fullpath_name),
fullpath_name.split('|')
)
if _parent:
if self.child_list:
if short_nodename in self.child_list:
# TODO
if not 'expandShape' in self.opt:
_fp_name = utils.getNode(short_nodename, long=True)
if not cmds.objectType(_fp_name, isa='shape'):
opt = _set_option_attrib(short_nodename,
self.attrib_opt
)
self.root.find_add(_parent,
short_nodename,
opt=opt
)
else:
return
c_num = dagPath.childCount()
if c_num > 0:
for i_num in range(c_num):
child_mObj = dagPath.child(i_num)
mfn_dag = om.MFnDagNode(child_mObj)
child_dag = mfn_dag.getPath()
ret = __construct(child_dag, parent=fullpath_name)
if not ret:
continue
else:
return
self.child_list = utils.get_all_childs(name)
dagPath = utils.get_dagPathObj(name)
__construct.__defaults__ = (__construct,)
__construct(dagPath, parent)
def set_xml_path(self, xml_path):
"""
Set XML path.
Args:
xml_path (str): xml file path
"""
self.xml_path = xml_path
def export_xml(self, root_path, assetName=None):
"""
Export to XML file as DAG tree hierarchy infomation.
Args:
root_path (str): directory root path.
assetName (str, optional): prefix name of the file name
of the XML file.The default set value is self.name. Defaults to None.
"""
if not assetName:
assetName = self.name
elem_root = ET.Element(self.TAG_ROOT)
ele_curr = elem_root
for root_obj, i_child in self.traverse():
if root_obj != '':
ele_curr = self.find_named_element(elem_root,
root_obj.fullpath,
root_obj.opt
)
for ch in i_child:
ele_curr.append(self.create_new_nodeTag(ch.fullpath, ch.opt))
output_filename = '/'.join([root_path, assetName + '_dagInfo.xml'])
output_file(elem_root, str(output_filename))
self.set_xml_path(output_filename)
@classmethod
def import_xml(cls, xml_file):
"""
Import XML data from file path.
Args:
xml_file (str): dag info xml file path.
Returns:
cls: DAGstructure class
"""
data = ET.parse(xml_file)
root = data.getroot()
node = root.find('node')
name = node.get('name')
dag_structure = cls(name, xml_path=xml_file)
def __construct(elem, parent_node, __construct=None):
node_name = elem.get('fullpath')
restore_opt = cls.restore_option(elem.findall('option'))
dag_structure.root.find_add(par=parent_node,
name=node_name,
opt=restore_opt
)
for child_elem in elem.findall('node'):
__construct(child_elem, node_name)
__construct.__defaults__ = (__construct,)
restore_opt = cls.restore_option(node.findall('option'))
dag_structure.root = DAGtree(name, opt=restore_opt)
for child_elem in node.findall('node'):
__construct(child_elem, dag_structure.name)
return dag_structure
def get_obj(self, name):
"""
Get tree dag object by string name.
"""
return self.root.find(par=name, name='')
def restore_parent(self):
"""
Restore to parent of DAG structure.
"""
for _, i_child in self.traverse():
for c in i_child:
try:
cmds.parent(c.name, c.parent.name)
except:
pass
def restore_attributes(self, root='', attrs=['visibility']):
if root == '':
root = self.root
if not isinstance(attrs, (list, tuple)):
attrs = [attrs]
root.restoreAttrs(attrs)
for root, child_elem in self.traverse():
for elem in child_elem:
elem.restoreAttrs(attrs)
def build(self, root='', maintainUUID=True, restoreAttr=True):
"""
Build the DAG hierarchy where the data is constructed.
Args:
root (Element): root Element node.
maintainUUID (bool, optional): If you want to reproduce UUID, use True.
"""
def _create_node(elem, parent_node):
if type(elem) is list:
elem = elem[0]
if elem.name.startswith('|'):
node_name = elem.name.split('|')[-1]
else:
node_name = elem.name
node_type = elem.opt['nodeType']
if maintainUUID:
uuid = elem.opt['uuid']
else:
uuid = ''
return utils.create_node(node_name, node_type, uuid, parent=parent_node)
if root == '':
root = self.root
_create_node(root, parent_node='')
for root, child_elem in self.traverse():
for elem in child_elem:
_create_node(elem, parent_node=root.name)
def printout(self, depth=0):
"""
print output root dag DAGtree hierarchy.
Args:
depth (int, optional): _description_. Defaults to 0.
"""
self.root.printout(depth)
def output_file(element, file_name):
with open(file_name, 'wb') as fp:
fp.write(minidom.parseString(ET.tostring(element)).toprettyxml(encoding="utf-8"))
print('# output file : ' + file_name)
if __name__ == "__main__":
"""
a = dagger.DAGstructure('jointTest')
a.construct('all')
a.printout()
a.export_xml('D:/')
cmds.delete('all')
a = a.import_xml('D:/nPlaneTest4_dagInfo.xml')
a = a.import_xml('D:/jointTest_dagInfo.xml')
a.build()
"""