Skip to content

Commit 32b6a74

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

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

dwpicker/scenedata_dag.py

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

0 commit comments

Comments
 (0)