Blender V2.61 - r43446

FBlender.py

Go to the documentation of this file.
00001 # Written by Nathan Letwory: Letwory Interactive | Studio Lumikuu
00002 # http://www.letworyinteractive.com/b | http://www.lumikuu.com
00003 # for Blender Foundation
00004 
00005 import os.path
00006 
00007 import Core.Common.FUtils as FUtils
00008 from Core.Logic.FSettingEntry import *
00009 from Scripts.FApplication import *
00010 
00011 class FBlender(FApplication):
00012     """Presents Blender to the testing framework"""
00013     
00014     __SCRIPT_EXTENSION = ".py"
00015     
00016     def __init__(self, configDict):
00017         """__init__() -> FBlender"""
00018         FApplication.__init__(self, configDict)
00019         self.__script = None
00020         self.__blenderScript = None
00021         self.__currentFilename = None
00022         self.__currentImageName = None
00023         self.__currentImportProperName = None
00024         self.__testImportCount = 0
00025         self.__testRenderCount = 0
00026         self.__blenderCommandLine = None
00027         self.__workingDir = None
00028     
00029     def GetPrettyName(self):
00030         """GetPrettyName() -> str
00031         
00032         Implements FApplication.GetPrettyName()
00033         
00034         """
00035         return "Blender 2.59"
00036     
00037     def GetSettingsForOperation(self, operation):
00038         """GetSettingsForOperation(operation) -> list_of_FSettingEntry
00039         
00040         Implements FApplication.GetSettingsForOperation()
00041 
00042     TODO: Figure out how we can/should use these, esp. for animation tests
00043         
00044         """
00045         if (operation == IMPORT):
00046             return []
00047         elif (operation == EXPORT):
00048             return []
00049         elif (operation == RENDER): 
00050             return []
00051         else:
00052             return []
00053     
00054     def BeginScript(self, workingDir):
00055         """BeginScript(workingDir) -> None'
00056         
00057         Implements FApplication.BeginScript()
00058         
00059         """
00060         pyFilename = ("script" + str(self.applicationIndex) + 
00061                 FBlender.__SCRIPT_EXTENSION)
00062         blenderPyFilename = ("blenderScript" + str(self.applicationIndex) + 
00063                 FBlender.__SCRIPT_EXTENSION)
00064         self.__script = open(os.path.join(workingDir, pyFilename), "w")
00065         self.__blenderScript = open(os.path.join(workingDir, blenderPyFilename), "w")
00066         self.WriteCrashDetectBegin(self.__script)
00067 
00068         self.__blenderScript.write(
00069             """import bpy
00070 import bpy.ops
00071 import sys
00072 
00073 import_dae = sys.argv[-1]
00074 export_dae = sys.argv[-3]
00075 default_dae = sys.argv[-4]
00076 
00077 print("default .dea for testing: {}\\n".format(default_dae))
00078 
00079 print("importing: {}\\n".format(import_dae))
00080 img=sys.argv[-2]
00081 img=img.replace("\\\\", "\\\\\\\\")
00082 bpy.ops.wm.collada_import(filepath=import_dae)
00083 for o in bpy.data.objects:
00084     print("\\t{}\\n".format(o.name))
00085 
00086 if len(bpy.data.cameras)==0:
00087     print("no camera found, importing {}".format(default_dae))
00088     bpy.ops.wm.collada_import(filepath=default_dae)
00089     for o in bpy.data.objects:
00090         o.select = True if o.name == 'delete_me' else False
00091     print("cleaning after {} import".format(default_dae))
00092     bpy.ops.object.delete()
00093 
00094 print("making sure we have an active camera... ")
00095 c = None
00096 for o in bpy.data.objects:
00097     if o.type=='CAMERA' and o.name=='testCamera':
00098         c = o
00099         print("...camera set")
00100 if not c:
00101     print("... ERROR: no camera found!")
00102 
00103 bpy.data.scenes[0].camera = c
00104 
00105 bpy.data.scenes[0].render.resolution_x = 512
00106 bpy.data.scenes[0].render.resolution_y = 512
00107 bpy.data.scenes[0].render.resolution_percentage = 100
00108 bpy.data.scenes[0].render.use_antialiasing = False
00109 bpy.data.scenes[0].render.alpha_mode = 'STRAIGHT'
00110 
00111 bpy.ops.render.render(animation=False, write_still=True)
00112 
00113 bpy.ops.wm.collada_export(filepath=export_dae)
00114 
00115 print("\\n\\ndone testing.\\n\\n")"""
00116         )
00117         
00118         self.__testImportCount = 0
00119         self.__testRenderCount = 0
00120         self.__workingDir = workingDir
00121     
00122     def EndScript(self):
00123         """EndScript() -> None
00124         
00125         Implements FApplication.EndScript()
00126         
00127         """
00128         self.__blenderScript.close()
00129         self.__script.close()
00130     
00131     def RunScript(self):
00132         """RunScript() -> None
00133         
00134         Implements FApplication.RunScript()
00135         
00136         """
00137         if (not os.path.isfile(self.configDict["blenderPath"])):
00138             print "Blender does not exist"
00139             return True
00140         
00141         print ("start running " + os.path.basename(self.__script.name))
00142         command = ("\"" + self.configDict["pythonExecutable"] + "\" " +
00143                    "\"" + self.__script.name + "\"")
00144         
00145         returnValue = subprocess.call(command)
00146         
00147         if (returnValue == 0):
00148             print "finished running " + os.path.basename(self.__script.name)
00149         else:
00150             print "crashed running " + os.path.basename(self.__script.name)
00151         
00152         return (returnValue == 0)
00153     
00154     def WriteImport(self, filename, logname, outputDir, settings, isAnimated, cameraRig, lightingRig):
00155         """WriteImport(filename, logname, outputDir, settings, isAnimated, cameraRig, lightingRig) -> list_of_str
00156                 
00157         """
00158         outputFormat = ".png"
00159         
00160         command = ("\"" + self.configDict["blenderPath"] + "\" --background -noaudio \"" + self.configDict["blenderEmpty"] + "\" -o ")
00161         
00162         baseName = FUtils.GetProperFilename(filename)
00163         self.__currentImportProperName = baseName
00164         outputFilename = os.path.join(outputDir, baseName + "_out" + ".dae")
00165         self.__currentFilename = outputFilename
00166         imageFilename = os.path.join(outputDir, "result" + outputFormat)
00167         self.__currentImageName = imageFilename
00168         command = (command + "\"" + imageFilename + "\" --python \"" + self.__blenderScript.name + "\" -- \""+ self.configDict["blenderDefaultDae"] +"\" \"" + outputFilename + "\" \"" + imageFilename + "\" \"" + filename+"\"")
00169         
00170         print "***Importing: %s" % (filename)
00171         print "   Command %s" % (command)        
00172 
00173         self.__blenderCommandLine = command
00174         
00175         self.WriteCrashDetect(self.__script, command, logname)
00176         
00177         self.__testImportCount = self.__testImportCount + 1
00178         
00179         return [os.path.normpath(outputFilename)]
00180         
00181     
00182     def WriteRender(self, logname, outputDir, settings, isAnimated, cameraRig, lightingRig):
00183         """WriteRender(logname, outputDir, settings, isAnimated, cameraRig, lightingRig) -> list_of_str
00184         
00185         Implements FApplication.WriteRender()
00186         
00187         """
00188         print "***Render outputDir: %s" % (outputDir)
00189 
00190         command = self.__blenderCommandLine
00191         
00192         print "***Rendering: %s" % (self.__currentImageName)
00193         print "   Command %s" % (command)        
00194 
00195         self.WriteCrashDetect(self.__script, command, logname)
00196         
00197         self.__testRenderCount = self.__testRenderCount + 1        
00198         return [os.path.normpath(self.__currentImageName),]
00199 
00200     
00201     def WriteExport(self, logname, outputDir, settings, isAnimated, cameraRig, lightingRig):
00202         """WriteImport(logname, outputDir, settings, isAnimated, cameraRig, lightingRig) -> list_of_str
00203         
00204         Implements FApplication.WriteExport()
00205         
00206         """
00207         print "***Export outputDir: %s" % (outputDir)
00208         command = self.__blenderCommandLine
00209         print "   Command %s" % (command)        
00210         self.WriteCrashDetect(self.__script, command, logname)
00211         
00212         return [os.path.normpath(self.__currentFilename)]