Skip to content
Newer
Older
100644 375 lines (307 sloc) 16.6 KB
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
1 # Copyright (C) 2006 Khronos Group
2 # Available only to Khronos members.
3 # Distribution of this file or its content is strictly prohibited.
4
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
5 import os.path
6 import shutil
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
7 import Core.Common.FUtils as FUtils
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
8
9 from stat import *
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
10 from Core.Logic.FSettingEntry import *
11 from Scripts.FApplication import *
12
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
13 class FMaya_UIRender (FApplication):
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
14 """The class which represents Maya 7.0 to the testing framework.
15
16 Note that this has only been tested on Maya 7.0 and will probably not work
17 on other versions.
18
19 """
20
21 __PLUGIN = ("COLLADA")
22
23 __MEL_SCRIPT_EXTENSION = ".mel"
aaaf419 Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
24 __SCRIPT_EXTENSION = ".py"
25
26 __IMPORT_OPTIONS = [
27 ("Import document up-axis", "importUpAxis", "0"),
28 ("Import document units", "importUnits", "0")]
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
29
30 __EXPORT_OPTIONS = [
31 ("Bake transforms", "bakeTransforms", "0"),
32 ("Relative paths", "relativePaths", "0"),
33 ("Bake lighting", "bakeLighting", "0"),
34 ("Export camera as lookat", "exportCameraAsLookat", "0"),
35 ("Export polygons as triangles", "exportTriangles", "0"),
36 ("Sampling", "isSampling", "0"),
37 ("Curve-Contrain", "curveConstrainSampling", "0"),
aaaf419 Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
38 ("Sampling Function", "samplingFunction", ""),
39 ("Static curve removal", "removeStaticCurves", "1"),
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
40 ("Export polygon meshes", "exportPolygonMeshes", "1"),
41 ("Export lights", "exportLights", "1"),
aaaf419 Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
42 ("Export cameras", "exportCameras", "1"),
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
43 ("Export joints and skin", "exportJointsAndSkin", "1"),
44 ("Export animations", "exportAnimations", "1"),
45 ("Export invisible nodes", "exportInvisibleNodes", "0"),
aaaf419 Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
46 ("Export default cameras", "exportDefaultCameras", "0"),
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
47 ("Export normals", "exportNormals", "1"),
48 ("Export texture coordinates", "exportTexCoords", "1"),
aaaf419 Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
49 ("Export per-vertex colors", "exportVertexColors", "1"),
50 ("Export per-vertex color animations", "exportVertexColorAnimations", "1"),
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
51 ("Export geometric tangents", "exportTangents", "0"),
aaaf419 Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
52 ("Export texture tangents", "exportTexTangents", "1"),
53 ("Export materials only", "exportMaterialsOnly", "0"),
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
54 ("Export constraints", "exportConstraints", "1"),
55 ("Export physics", "exportPhysics", "1"),
56 ("Exclusion set mode", "exclusionSetMode", "0"),
57 ("Exclusion set", "exclusionSets", ""),
aaaf419 Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
58 ("Export external references", "exportXRefs", "1"),
59 ("De-Reference external references", "dereferenceXRefs", "0"),
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
60 ("XFov", "cameraXFov", "0"),
aaaf419 Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
61 ("YFov", "cameraYFov", "1")]
62
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
63 __RENDER_CAMERA = "Camera"
64 __RENDER_RENDERER = "Renderer"
65 __RENDER_ANIMATION_START = "Animation Start Frame"
66 __RENDER_ANIMATION_END = "Animation End Frame"
67 __RENDER_ANIMATION_STEP = "Animation Step Interval"
68 __RENDER_STILL_START = "Non-Animation Start Frame"
69 __RENDER_STILL_END = "Non-Animation End Frame"
70 __RENDER_STILL_STEP = "Non-Animation Step Interval"
71 __RENDER_WIDTH = "X resolution"
72 __RENDER_HEIGHT = "Y resolution"
73 __RENDER_ARD = "Device Aspect Ratio (empty to ignore)"
74 __RENDER_FORMAT = "Output Filetype"
75
76 __RENDER_OPTIONS = [
77 (__RENDER_CAMERA, "- NOT USED -", "|testCamera"),
78 (__RENDER_WIDTH, "- NOT USED -", "300"),
79 (__RENDER_HEIGHT, "- NOT USED -", "300"),
80 (__RENDER_ANIMATION_START, "setAttr defaultRenderGlobals.startFrame ", "1"),
81 (__RENDER_ANIMATION_END, "setAttr defaultRenderGlobals.endFrame ", "45"),
82 (__RENDER_ANIMATION_STEP, "setAttr defaultRenderGlobals.byFrameStep ", "3"),
83 (__RENDER_STILL_START, "setAttr defaultRenderGlobals.startFrame ", "1"),
84 (__RENDER_STILL_END, "setAttr defaultRenderGlobals.endFrame ", "1"),
85 (__RENDER_STILL_STEP, "setAttr defaultRenderGlobals.byFrameStep ", "1")]
86
87 def __init__(self, configDict):
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
88 """__init__() -> FMaya_UIRender"""
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
89 FApplication.__init__(self, configDict)
90 self.__melScript = None
91 self.__currentFilename = None
92 self.__currentImportProperName = None
93 self.__testImportCount = 0
94 self.__workingDir = None
95
96 def GetPrettyName(self):
97 """GetPrettyName() -> str
98
99 Implements FApplication.GetPrettyName()
100
101 """
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
102 return "Maya - UIRender"
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
103
104 def GetSettingsForOperation(self, operation):
105 """GetSettingsForOperation(operation) -> list_of_FSettingEntry
106
107 Implements FApplication.GetSettingsForOperation()
108
109 """
aaaf419 Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
110 options = []
111
112 # Retrieve the list of options for this operation.
113 optionList = None
114 if operation == IMPORT: optionList = FMaya_UIRender.__IMPORT_OPTIONS
115 elif operation == EXPORT: optionList = FMaya_UIRender.__EXPORT_OPTIONS
116 elif operation == RENDER: optionList = FMaya_UIRender.__RENDER_OPTIONS
117
118 # Return a correctly-processed list of FSettingEntry's.
119 if optionList != None:
120 for entry in optionList:
121 options.append(FSettingEntry(*entry))
122 return options
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
123
124 def BeginScript(self, workingDir):
125 """BeginScript(workingDir) -> None
126
127 Implements FApplication.BeginScript()
128
129 """
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
130 melFilename = ("script" + str(self.applicationIndex) + FMaya_UIRender.__MEL_SCRIPT_EXTENSION)
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
131 self.__melScript = open(os.path.join(workingDir, melFilename) , "w")
132
133 self.__melScript.write(
134 "int $descriptor;\n" +
135 "catch(`loadPlugin \"" +
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
136 FMaya_UIRender.__PLUGIN.replace("\\", "\\\\") + "\"`);\n" +
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
137 "catch(`file -f -new`);\n\n" +
138 "proc fixNewlines(string $filename) {\n" +
139 " $tempFilename = $filename + \".temp\";\n" +
140 "\n" +
141 " $file=`fopen $filename \"r\"`;\n" +
142 " $tempFile=`fopen $tempFilename \"w\"`;\n" +
143 "\n" +
144 " string $nextLine = `fgetline $file`;\n" +
145 " while (size($nextLine) > 0) { \n" +
146 " fprint $tempFile `substitute \"\\n\" " +
147 "$nextLine \"\\r\\n\"`;\n" +
148 " $nextLine = `fgetline $file`;\n" +
149 " }\n" +
150 " fclose $tempFile;\n" +
151 " fclose $file;\n" +
152 "\n" +
153 " sysFile -delete $filename;\n" +
154 " sysFile -rename $filename $tempFilename;\n" +
155 "}\n\n")
156
157 self.__testImportCount = 0
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
158 self.__workingDir = workingDir
159 self.__renderFolders = []
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
160
161 def EndScript(self):
162 """EndScript() -> None
163
164 Implements FApplication.EndScript()
165
166 """
167 self.__melScript.close()
168
169 def RunScript(self):
170 """RunScript() -> None
171
172 Implements FApplication.RunScript()
173
174 """
175 if (not os.path.isfile(self.configDict["mayaPath"])):
176 print "Maya does not exist"
177 return True
178
179 command = ("\"" + self.configDict["mayaPath"] +
180 "\" -batch -script \"" + self.__melScript.name + "\"")
181
182 # quotes around command is awkward, but seems like the only way works
183 print ("start running " + os.path.basename(self.__melScript.name))
184 returnValueImport = self.RunApplication(command, self.__workingDir)
185 if (returnValueImport == 0):
186 print "finished running " + os.path.basename(self.__melScript.name)
187 else:
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
188 print "crashed running " + os.path.basename(self.__melScript.name)
189
190 # Maya has a tendency to dump images where I don't want them to be.
191 # Look for images in the sub-folders of the output folder and move them to the output folder.
192 for renderFolder in self.__renderFolders:
193 subFolders = [renderFolder]
194 while (len(subFolders) > 0):
195 subFolder = subFolders[-1]
196 subFolders.pop()
197
198 for dirEntry in os.listdir(subFolder):
199 pathname = os.path.join(subFolder, dirEntry)
200 mode = os.stat(pathname)[ST_MODE]
201 if S_ISDIR(mode):
202 # Add this sub-folder to our queue.
203 subFolders.append(pathname)
204 elif S_ISREG(mode):
205 # Process all python script files, except for the __init__.py ones.
206 if FUtils.GetExtension(pathname).lower() == "png":
207 shutil.move(pathname, os.path.join(renderFolder, dirEntry))
208 self.__renderFolders = []
209
210 return (returnValueImport == 0)
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
211
a769bd5 Alan: added a cameraRig and a lightingRig index to tests
giles authored Mar 3, 2010
212 def WriteImport(self, filename, logname, outputDir, settings, isAnimated, cameraRig, lightingRig):
213 """WriteImport(filename, logname, outputDir, settings, isAnimated, cameraRig, lightingRig) -> list_of_str
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
214
215 Implements FApplication.WriteImport(). Assumes a COLLADA, maya binary,
216 or maya ascii file is being imported.
217
218 """
219 baseName = FUtils.GetProperFilename(filename)
220 self.__currentImportProperName = baseName
221 output = (os.path.join(outputDir, baseName)).replace("\\", "/")
222 filename = filename.replace("\\", "/")
223 self.__currentFilename = output + ".mb"
aaaf419 Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
224
225 # Generate the import options string.
226 options = ""
227 for setting in settings:
228 value = setting.GetValue().strip()
229 if len(value) == 0:
230 value = self.FindDefault(FMaya_UIRender.__IMPORT_OPTIONS, setting.GetPrettyName())
231 options = (options + setting.GetCommand() + "=" + value + ";")
232
233 # Generate the import MEL command.
234 extension = FUtils.GetExtension(filename).lower()
235 if (extension == "mb"):
236 command = ("catch(`file -type \"mayaBinary\" -o \"" + filename + "\"`);\n")
237 elif (extension == "ma"):
238 command = ("catch(`file -type \"mayaAscii\" -o \"" + filename + "\"`);\n")
239 else:
240 command = ("catch(`file -type \"COLLADA importer\" -op \"" + options + "\" -o \"" + filename + "\"`);\n")
241
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
242 self.__melScript.write(
243 "$logname = \"" + logname.replace("\\", "/") + "\";\n" +
244 "$descriptor = `cmdFileOutput -o $logname`;\n" +
245 "catch(`file -f -new`);\n" +
246 command +
247 "catch(`file -rename \"" + output + "\"`);\n" +
248 "catch(`file -save -type \"mayaBinary\"`);\n" +
249 "cmdFileOutput -c $descriptor;\n" +
250 "fixNewlines $logname;\n\n")
251
252 self.__testImportCount = self.__testImportCount + 1
253
254 return [os.path.normpath(baseName + ".mb"),]
255
a769bd5 Alan: added a cameraRig and a lightingRig index to tests
giles authored Mar 3, 2010
256 def WriteRender(self, logname, outputDir, settings, isAnimated, cameraRig, lightingRig):
257 """WriteRender(logname, outputDir, settings, isAnimated, cameraRig, lightingRig) -> list_of_str
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
258
259 Implements FApplication.WriteRender()
260
261 """
262 baseName = self.__currentImportProperName
263 output = os.path.normpath(os.path.join(outputDir, baseName))
264 outputDir = os.path.dirname(output)
265
266 self.__melScript.write(
267 "$logname = \"" + logname.replace("\\", "/") + "\";\n" +
68f2f55 Khronos 1587
Brendan Rehon authored Aug 2, 2007
268 "$descriptor = `cmdFileOutput -o $logname`;\n")
269
270 # Unlock all setAttr attributes (for reimported older Maya files)
271 self.__melScript.write("setAttr -lock off defaultRenderGlobals.startFrame;\n")
272 self.__melScript.write("setAttr -lock off defaultRenderGlobals.endFrame;\n")
273 self.__melScript.write("setAttr -lock off defaultRenderGlobals.byFrameStep;\n")
274 self.__melScript.write("setAttr -lock off defaultRenderGlobals.imageFormat;\n")
275 self.__melScript.write("setAttr -lock off defaultRenderGlobals.imageFilePrefix;\n")
276 self.__melScript.write("setAttr -lock off defaultRenderGlobals.animation;\n")
277 self.__melScript.write("setAttr -lock off defaultRenderGlobals.putFrameBeforeExt;\n")
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
278
279 # Set render globals example:
280 # Maya node types: renderGlobals, hardwareRenderGlobals,
281 # setAttr hardwareRenderGlobals.frameBufferFormat 0
282
283 for setting in settings:
284
285 # Start by parsing the value.
286 value = setting.GetValue().strip()
287 if (len(value) == 0):
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
288 value = self.FindDefault(FMaya_UIRender.__RENDER_OPTIONS, setting.GetPrettyName())
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
289
290 prettyName = setting.GetPrettyName()
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
291 if (prettyName == FMaya_UIRender.__RENDER_ANIMATION_START):
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
292 if not isAnimated: continue
293 start = int(value)
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
294 elif (prettyName == FMaya_UIRender.__RENDER_ANIMATION_END):
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
295 if not isAnimated: continue
296 end = int(value)
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
297 elif (prettyName == FMaya_UIRender.__RENDER_ANIMATION_STEP):
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
298 if not isAnimated: continue
299 step = int(value)
300
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
301 elif (prettyName == FMaya_UIRender.__RENDER_STILL_START):
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
302 if isAnimated: continue
303 start = int(value)
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
304 elif (prettyName == FMaya_UIRender.__RENDER_STILL_END):
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
305 if isAnimated: continue
306 end = int(value)
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
307 elif (prettyName == FMaya_UIRender.__RENDER_STILL_STEP):
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
308 if isAnimated: continue
309 step = int(value)
310
311 # Record these settings for later.
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
312 elif (prettyName == FMaya_UIRender.__RENDER_WIDTH):
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
313 width = value
314 continue
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
315 elif (prettyName == FMaya_UIRender.__RENDER_HEIGHT):
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
316 height = value
317 continue
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
318 elif (prettyName == FMaya_UIRender.__RENDER_CAMERA):
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
319 camera = value
320 continue
321
322 self.__melScript.write(setting.GetCommand() + " " + value + ";\n")
323
324 self.__melScript.write("setAttr defaultRenderGlobals.imageFormat 32;\n") # where 32 is PNG.
325 self.__melScript.write("setAttr -type \"string\" defaultRenderGlobals.imageFilePrefix \"" + str(baseName) + "\";\n")
a769bd5 Alan: added a cameraRig and a lightingRig index to tests
giles authored Mar 3, 2010
326 self.__melScript.write("setAttr defaultRenderGlobals.animation " + str(isAnimated, cameraRig, lightingRig).lower() + ";\n")
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
327 self.__melScript.write("setAttr defaultRenderGlobals.putFrameBeforeExt true;\n")
328 self.__melScript.write("workspace -renderType \"images\" \"" + outputDir.replace("\\", "/") + "\";\n")
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
329 self.__melScript.write("catch(`hwRender -camera \"" + camera + "\" -width " + width + " -height " + height + "`);\n\n")
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
330 self.__melScript.write("cmdFileOutput -c $descriptor;\nfixNewlines $logname;\n\n")
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
331
332 # Record this folder for image look-ups, because Maya spreads images in unexpected ways.
333 self.__renderFolders.append(outputDir)
334
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
335 outputList = []
336 if not isAnimated:
337 outputList.append(os.path.normpath(output + ".png"))
338 else:
339 numDigit = len(str(end))
340 for i in range(start, end + 1, step):
341 outputList.append(os.path.normpath(output + "." + str(i) + ".png"))
342
343 return outputList
344
a769bd5 Alan: added a cameraRig and a lightingRig index to tests
giles authored Mar 3, 2010
345 def WriteExport(self, logname, outputDir, settings, isAnimated, cameraRig, lightingRig):
346 """WriteImport(logname, outputDir, settings, isAnimated, cameraRig, lightingRig) -> list_of_str
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
347
348 Implements FApplication.WriteExport()
349
350 """
351 basename = self.__currentImportProperName + ".dae"
352 output = os.path.join(outputDir, self.__currentImportProperName)
353 output = output.replace("\\", "/")
354
355 options = ""
356 for setting in settings:
357 value = setting.GetValue().strip()
358 if (value == ""):
093ca1a Bugzilla Bug 1587.
glaforte authored Jun 17, 2007
359 value = self.FindDefault(FMaya_UIRender.__EXPORT_OPTIONS,
821e7bd Bugzilla Bug 1587.
glaforte authored Jun 16, 2007
360 setting.GetPrettyName())
361
362 options = (options + setting.GetCommand() + "=" +
363 value + ";")
364
365 self.__melScript.write(
366 "$logname = \"" + logname.replace("\\", "/") + "\";\n" +
367 "$descriptor = `cmdFileOutput -o $logname`;\n" +
368 "catch(`file -op \"" + options +
369 "\" -typ \"COLLADA exporter\" -pr -ea \"" + output +
370 "\"`);\n" +
371 "cmdFileOutput -c $descriptor;\n" +
372 "fixNewlines $logname;\n\n")
373
374 return [os.path.normpath(basename),]
375
Something went wrong with that request. Please try again.