Skip to content

Commit 3b3a986

Browse files
committed
add storage class for dag hierarchy (wip)
1 parent d62d4bd commit 3b3a986

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

dwpicker/scenedata_dag.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import imp
2+
import json
3+
import time
4+
5+
import dwpicker
6+
imp.reload(dwpicker)
7+
import dwpicker.scenedata
8+
from maya import cmds
9+
10+
11+
def shortname(fullname):
12+
return fullname.split('|')[-1].split(':')[-1]
13+
14+
15+
def timeit(func):
16+
def wrapped(*args, **kwargs):
17+
started = time.perf_counter()
18+
value = func(*args, **kwargs)
19+
print(
20+
'Time for', func.__qualname__, ':', time.perf_counter() - started)
21+
return value
22+
return wrapped
23+
24+
25+
def ensure_holder(parent, type, identifier, default_name):
26+
if parent:
27+
if identifier:
28+
for node in cmds.listRelatives(
29+
parent, children=True, fullPath=True):
30+
if shortname(node) == shortname(identifier):
31+
identifier = node
32+
break
33+
else:
34+
default_name = identifier
35+
identifier = None
36+
if not identifier:
37+
identifier = cmds.createNode(
38+
type, name=default_name, parent=parent)
39+
if not cmds.objExists(identifier):
40+
identifier = cmds.createNode(type, name=identifier)
41+
if parent:
42+
identifier = parent + '|' + identifier.split('|')[-1]
43+
nodes = cmds.ls(identifier, long=True)
44+
assert len(nodes) == 1, (
45+
'Unexpected nodes found: {}, for {}'.format(
46+
nodes, identifier))
47+
return nodes[0]
48+
49+
50+
def ensure_data(node, attribute, value):
51+
if not cmds.attributeQuery(attribute, node=node, exists=True):
52+
cmds.addAttr(node, longName=attribute, dataType='string')
53+
value = json.dumps(value)
54+
attribute = node + '.' + attribute
55+
if cmds.getAttr(attribute) != value:
56+
cmds.setAttr(attribute, value, type='string')
57+
58+
59+
class SceneDagStorage:
60+
PICKER_HOLDER_NODE = 'DwPicker'
61+
62+
def load(self):
63+
data = []
64+
for holder in cmds.ls('::' + self.PICKER_HOLDER_NODE):
65+
for picker_node in cmds.listRelatives(
66+
holder, children=True, fullPath=True):
67+
picker = {
68+
'general': {
69+
# add corresponding scene node identifier, so storing
70+
# picker data then will be into same node
71+
'id': picker_node,
72+
}, 'shapes': []}
73+
for attribute in cmds.listAttr(
74+
picker_node, userDefined=True):
75+
value = json.loads(cmds.getAttr(
76+
picker_node + '.' + attribute))
77+
if attribute == 'version':
78+
picker[attribute] = value
79+
else:
80+
picker['general'][attribute] = value
81+
for shape in cmds.listRelatives(
82+
picker_node, shapes=True, fullPath=True):
83+
shape_data = {'id': shape}
84+
for attribute in cmds.listAttr(
85+
shape, userDefined=True):
86+
value = json.loads(cmds.getAttr(
87+
shape + '.' + attribute))
88+
shape_data[attribute.replace('_', '.')] = value
89+
picker['shapes'].append(shape_data)
90+
# some weird missing version in picker general section
91+
picker['general']['version'] = picker['version']
92+
data.append(picker)
93+
return data
94+
95+
def store(self, pickers):
96+
for picker in pickers:
97+
picker_holder = picker.get('general', {}).get('id')
98+
if not picker_holder or not cmds.objExists(picker_holder):
99+
if not cmds.objExists(self.PICKER_HOLDER_NODE):
100+
cmds.createNode(
101+
'dagContainer', name=self.PICKER_HOLDER_NODE)
102+
picker_holder = ensure_holder(
103+
self.PICKER_HOLDER_NODE, 'transform', picker_holder,
104+
picker['general']['name'])
105+
ensure_data(picker_holder, 'version', picker['version'])
106+
for key, value in picker['general'].items():
107+
if key == 'id':
108+
# scene identifier makes sense only in current scene
109+
# context and should not be serialized
110+
continue
111+
ensure_data(picker_holder, key, value)
112+
for shape in picker['shapes']:
113+
shape_holder = ensure_holder(
114+
picker_holder, 'nurbsSurface', shape.get('id'), 'shape')
115+
for key, value in shape.items():
116+
if key == 'id':
117+
continue
118+
assert '_' not in key
119+
ensure_data(shape_holder, key.replace('.', '_'), value)
120+
121+
def cleanup(self):
122+
pass
123+
124+
#data = timeit(dwpicker.scenedata.DefaultSceneStorage().load)()
125+
#data = timeit(SceneDagStorage().load)()
126+
#SceneDagStorage().store(data)
127+
#dwpicker.scenedata.DefaultSceneStorage().store(data)
128+
#pprint.pprint(data)
129+
#dwpicker.show(storage_class=SceneDagStorage)
130+
#dwpicker.show(storage_class=dwpicker.scenedata.DefaultSceneStorage)

0 commit comments

Comments
 (0)