-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbaseSkeletonPreset.py
221 lines (160 loc) · 5.79 KB
/
baseSkeletonPreset.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
from filesystem import Path, PresetManager, Preset, savePreset, readPreset, LOCAL, GLOBAL
import skeletonBuilder
from maya.cmds import *
from baseSkeletonBuilder import SkeletonPart, setupAutoMirror, TOOL_NAME, buildSkeletonPartContainer
from names import camelCaseToNice
import maya.cmds as cmd
import apiExtensions
import filesystem
import inspect
XTN = 'skeleton'
PRESET_MANAGER = PresetManager( TOOL_NAME, XTN )
VERSION = 0
buildSkeletonPartContainer = skeletonBuilder.buildSkeletonPartContainer
SkeletonPart = skeletonBuilder.SkeletonPart
setupAutoMirror = skeletonBuilder.setupAutoMirror
eval = __builtins__[ 'eval' ] #restore python's eval...
class NoPartsError(Exception): pass
def generatePresetContents():
lines = [ 'version=%d' % VERSION ] #always store some sort of versioning variable
hasParts = False
for part in SkeletonPart.IterAllPartsInOrder():
hasParts = True
lines.append( '<part>' )
lines.append( '%s=%s' % (part.__class__.__name__, part.getBuildKwargs()) )
for item in part:
itemParent = listRelatives( item, p=True, pa=True )
if itemParent:
itemParent = itemParent[0]
else:
itemParent = ''
rad = getAttr( '%s.radius' % item )
tx, ty, tz = xform( item, q=True, ws=True, rp=True )
rx, ry, rz = xform( item, q=True, ws=True, ro=True )
#store out the line of attributes to save for the item - NOTE: attributes are currently stored in a way that makes it possible to add/modify the attributes we need serialized reasonably easily...
lines.append( '%s,%s=radius:%s;t:%s,%s,%s;r:%s,%s,%s;' % (item, itemParent, rad, tx, ty, tz, rx, ry, rz) )
lines.append( '</part>' )
if not hasParts:
raise NoPartsError( "No parts found in scene!" )
return '\n'.join( lines )
def writePreset( presetName ):
'''
deals with serializing a skeleton to disk
'''
try:
contents = generatePresetContents()
except NoPartsError:
print "No parts found in the scene!"
return None
return savePreset( LOCAL, skeletonBuilder.TOOL_NAME, presetName, XTN, contents )
def writePresetToFile( presetFilepath ):
try:
contents = generatePresetContents()
except NoPartsError:
print "No parts found in the scene!"
return
Path( presetFilepath ).write( contents )
def loadPreset( presetName ):
p = Preset( LOCAL, skeletonBuilder.TOOL_NAME, presetName, XTN )
if not p.exists():
p = Preset( GLOBAL, skeletonBuilder.TOOL_NAME, presetName, XTN )
assert p.exists(), "Cannot find a %s preset called %s" % (XTN, presetName)
return loadPresetFile( p )
def loadPresetFile( presetFilepath ):
'''
deals with unserializing a skeleton preset definition into the scene
'''
assert presetFilepath.exists(), "No preset file found! %" % presetFilepath
itemRemapDict = {}
partList = []
def cleanUp():
#removes all items built should an exception occur
for partType, partItems in partList:
if partItems:
delete( partItems[0] )
lines = presetFilepath.read()
linesIter = iter( lines )
version = linesIter.next().strip()
try:
for line in linesIter:
line = line.strip()
#blank line? skip...
if not line:
continue
if line == '<part>':
partTypeAndBuildKwargLine = linesIter.next().strip()
toks = partTypeAndBuildKwargLine.split( '=' )
numToks = len( toks )
if numToks == 1:
partType, partBuildKwargs = toks[0], {}
elif numToks == 2:
partType, partBuildKwargs = toks
partBuildKwargs = eval( partBuildKwargs )
partItems = []
partList.append( (partType, partBuildKwargs, partItems) )
while True:
line = linesIter.next().strip()
#blank line? skip...
if not line:
continue
#are we done with the part?
if line == '</part>':
break
itemAndParent, attrInfo = line.split( '=' )
item, parent = itemAndParent.split( ',' )
attrBlocks = attrInfo.split( ';' )
#construct the attr dict
attrDict = {}
for block in attrBlocks:
if not block:
continue
attrName, attrData = block.split( ':' )
attrData = [ d for d in attrData.split( ',' ) if d ]
attrDict[ attrName ] = attrData
#build the actual joint
actualItem = apiExtensions.asMObject( createNode( 'joint', n=item ) )
#insert the item and what it actually maps to in the scene into the itemRemapDict
itemRemapDict[ item ] = actualItem
#finally append to the list of items in this part
partItems.append( (actualItem, parent, attrDict) )
except StopIteration:
cleanUp()
raise IOError( "File is incomplete!" )
except:
cleanUp()
raise
parts = []
for partType, partBuildKwargs, partItems in partList:
items = []
for (actualItem, parent, attrDict) in partItems:
actualParent = itemRemapDict.get( parent, None )
#do parenting if appropriate
if actualParent is not None:
cmd.parent( actualItem, actualParent )
#set the joint size
if 'radius' in attrDict:
size = attrDict[ 'radius' ][0]
setAttr( '%s.radius' % actualItem, float( size ) )
#move to the appropriate position
if 't' in attrDict:
tx, ty, tz = map( float, attrDict[ 't' ] )
move( tx, ty, tz, actualItem, a=True, ws=True, rpr=True )
#rotate appropriately
if 'r' in attrDict:
rx, ry, rz = map( float, attrDict[ 'r' ] )
rotate( rx, ry, rz, actualItem, a=True, ws=True )
#append to the items list - so we can instantiate the part once we've finished building the items
items.append( actualItem )
#instantiate the part and append it to the list of parts created
partClass = SkeletonPart.GetNamedSubclass( partType )
partContainer = buildSkeletonPartContainer( partClass, partBuildKwargs, items )
part = partClass( partContainer )
part.convert( partBuildKwargs )
parts.append( part )
setupAutoMirror()
for part in SkeletonPart.IterAllParts():
part.visualize()
return parts
def listPresets():
return PRESET_MANAGER.listAllPresets( True )
#end