1
2
3
4
5
6
7
8
9
10
11from pyfbsdk import FBApplication, FBSystem, FBFindModelByLabelName, FBBodyNodeId, FBTime, FBTake
12import math
13import sys
14import os
15import tempfile
16
17
18
19
20
21
22
23def loadScene(sceneFile):
25 if (not application.FileOpen(sceneFile, False)):
26 return False
27 return True
28
29def getFullPrefix(name):
30 if len( name.split(":") )>1:
31 lastPref = name.split(":")[-2]
32 firstPref = name.split(lastPref)[0]
33 fullPrefix = firstPref+lastPref
34 return fullPrefix
35 else:
36 return None
37
38def addAllGroupMembers(group, members):
39 for member in group.Items:
40 if member.ClassName() ==
str(
"FBGroup"):
41 addAllGroupMembers(member, members)
42 else:
43 members.append(member)
44
45def addAllSetMembers(set, members):
46 for member in set.Items:
47 if member.ClassName() ==
str(
"FBGroup"):
48 addAllGroupMembers(member, members)
49 elif member.ClassName() ==
str(
"FBSet"):
50 addAllSetMembers(member, members)
51 else:
52 members.append(member)
53
54def getGroupMembers(groupName, members):
55 for group
in FBSystem().Scene.Groups:
56 if group.Name == groupName.split(":")[-1]:
57 for item in group.Items:
58 if not getFullPrefix(item.LongName):
59 continue
60 elif getFullPrefix(item.LongName) == groupName.split(":")[-2]:
61 addAllGroupMembers(group, members)
62 return
63 else:
64 members.append(item)
65 return
66
67def getSetMembers(setName, members):
69 if set.Name == setName.split(":")[-1]:
70 for item in set.Items:
71 if not getFullPrefix(item.LongName):
72 continue
73 elif getFullPrefix(item.LongName) == setName.split(":")[-2]:
74 addAllSetMembers(set, members)
75 return
76 return
77
78def getHierarchy (modelName, rotationModels, translationModels):
80
81 if not model == None:
82 if not model.ClassName() ==
str(
"FBModel"):
83
84 rotationModels.append(model)
85 for child in model.Children:
86 getHierarchy(child.LongName, rotationModels, None)
87
88 if not translationModels == None:
89 translationModels.append(model)
90
91
92
93def getCharacterModels(character, models, transModels):
95 count = 0
96 countTrans = 0
97 for char in chars:
98 if char.Name == character:
99 for nodeID in FBBodyNodeId.values.values():
100 mod = char.GetModel(nodeID)
101 if mod != None:
102 count = count + 1
103 models.append(mod)
104 if nodeID == FBBodyNodeId.kFBHipsNodeId:
105 transModels.append(mod)
106 countTrans = countTrans + 1
107
108
109 return
110
111def getModel(modelName, models):
113 if not mod == None:
114 models.append(mod)
115
116def mineCurve(start, end, fcurve, modifier):
117 curveData = dict()
118 counter = start
119 while counter <= end:
120 frameTime =
FBTime(0,0,0,counter)
121 value = fcurve.Evaluate(frameTime) * modifier
122 curveData[frameTime.GetFrame()] = value
123 counter = counter + 1
124 return curveData
125
126def exportAnimData(animNodes, prefix, start, end, outData, modifier = 1.0):
127 if animNodes == None:
128 return
129
130 for animNode in animNodes:
131 curveName = prefix + animNode.Name
132 curveData = mineCurve(start, end, animNode.FCurve, modifier)
133 outData[curveName] = curveData
134
135 exportAnimData(animNode.Nodes, prefix, start, end, outData)
136
137
138def PlotModels(models, start, end):
139
140
141 print("1.Plot the models!")
142 print("1.1. Select the models:")
143 for mod in models:
144 mod.Selected = True
145
147
148
149
150 take.SetCurrentLayer(0)
151
152
153 print("1.2. Plot models from frame", start, "to frame", end)
155
156 take.PlotTakeOnSelected(period)
157
158
159 print("1.3. Unselect all ploted models!")
160 for mod in models:
161 mod.Selected = False
162
163
164def GetRotationAnimation(models, start, end, allData):
165 print("2. Rotation models has: ", len( rotationModels), "models.")
166 count = 0
167 for mod in models:
168 if mod != None:
169 nodeAnim = mod.AnimationNode
170 for nodeAnimNode in nodeAnim.Nodes:
171 if "Lcl Rotation" in nodeAnimNode.Name:
172 if len(nodeAnimNode.Nodes) > 0:
173 exportAnimData(nodeAnimNode.Nodes, mod.FullName + "__Rotation", start, end, allData, math.pi/180.0)
174 count = count + 1
175
176 print("2. Get valid rotation animation from: ", count, " models.")
177
178def GetTranslationAnimation(models, start, end, allData):
179 print("2. Translation models has: ", len( translationModels), "models.")
180 count = 0
181 for mod in models:
182 if mod != None:
183
184 nodeAnim = mod.AnimationNode
185 for nodeAnimNode in nodeAnim.Nodes:
186 if "Lcl Translation" in nodeAnimNode.Name:
187 if len(nodeAnimNode.Nodes) > 0:
188 exportAnimData(nodeAnimNode.Nodes, mod.FullName + "__Translation", start, end, allData)
189 count = count + 1
190
191 print("2. Get valid translation animation from: ", count, " models.")
192
193def ExportAnimationData(outFile, allData):
194 print("3. Output animation data into file: ", outFile, "\n")
195
196
197 output = open (outFile, 'w')
198 output.write('########### Animation Data ###########\n')
199 for keyModelName,curveDict in allData.items():
200 output.write('%s\n'%(keyModelName))
201
202 keylist = curveDict.keys()
203 keylist.sort()
204 for keyFrame in keylist:
205 output.write('%s: %s\n'%(keyFrame, curveDict[keyFrame]))
206 output.write('\n')
207 output.close()
208
209def ExportModelsAnimation(outFile, rotationModels, translationModels, start, end):
210 PlotModels( rotationModels, start, end )
211 PlotModels( translationModels, start, end )
212
213 allData = dict()
214 GetRotationAnimation( rotationModels, start, end, allData )
215 GetTranslationAnimation( translationModels, start, end, allData )
216
217 ExportAnimationData( outFile, allData )
218
219
220
221
222
223
224
225
226
227
228
229
230start = 120
231end = 130
232inFile = os.path.realpath(os.path.abspath(os.path.join(
FBSystem().ApplicationPath,
"../../OpenRealitySDK/Scenes/StoryExample.fbx" )))
233outFile = os.path.realpath(os.path.abspath(os.path.join( tempfile.gettempdir(), "StoryExample_output.txt" )))
234
235
236if not loadScene(inFile):
237 sys.stderr.write('Could not load file %s' % inFile)
238 raise Exception('quitting')
239
240
241
242
243
244rotationModels =
list()
245translationModels =
list()
246
247
248
249
250
251getCharacterModels("Skeleton_Model", rotationModels, translationModels)
252
253
254
255getHierarchy ("Skeleton_Model:Hips", rotationModels, translationModels)
256getHierarchy ("Character_Ctrl:HipsEffector", rotationModels, translationModels)
257
258
259
260getGroupMembers("KNIGHT:RightThighArmorLowBeltBuckle", rotationModels)
261
262
263
264
265'''
266getSetMembers("MyNamespace:MySet", models)
267'''
268
269# Finally, add any additional models which should have translations
270# and rotations exported for them.
271getModel("KNIGHT:Hips", rotationModels)
272getModel("KNIGHT:Hips", translationModels)
273getModel("Character_Ctrl:HipsEffector", translationModels)
274
275
276print("0. Totally get", len(rotationModels), "models to export their rotation.")
277#for model in rotationModels:
278# print model.FullName
279print("0. Totally get", len(translationModels), "models to export their translations.")
280
281
282
283
284if len(rotationModels) == 0 & len(translationModels) == 0:
285 sys.stderr.write('No models were found.')
286 raise Exception('quitting')
287
288
289ExportModelsAnimation(outFile, rotationModels, translationModels, start, end)
290
291print("The animation data for from frame ", start, " to frame ", end, " have been exported to file: \n", outFile)
FBApplication is used mainly to manage files.
Definition: pyfbsdk_generated.h:801
Provides access to the underlying system, and the MotionBuilder scene.
Definition: pyfbsdk_generated.h:18771
Time data structure.
Definition: pyfbsdk_generated.h:19596
Python built-in list container class.
Definition: pyfbsdk.h:65
Python built-in string class.
Definition: pyfbsdk.h:77
FBModel FBFindModelByLabelName(str pModelLabelName)
Find a model in the scene by its label name.