[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4163] trunk/py/scripts/addons/ render_povray/render.py: simplify povray binary checking, split it out into its own function.

Campbell Barton ideasman42 at gmail.com
Mon Jan 14 12:26:34 CET 2013


Revision: 4163
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4163
Author:   campbellbarton
Date:     2013-01-14 11:26:30 +0000 (Mon, 14 Jan 2013)
Log Message:
-----------
simplify povray binary checking, split it out into its own function. on windows check for 64bit then 32bit, assuming if 64bit is installed that the system is capable of running it.

Modified Paths:
--------------
    trunk/py/scripts/addons/render_povray/render.py

Modified: trunk/py/scripts/addons/render_povray/render.py
===================================================================
--- trunk/py/scripts/addons/render_povray/render.py	2013-01-14 10:54:57 UTC (rev 4162)
+++ trunk/py/scripts/addons/render_povray/render.py	2013-01-14 11:26:30 UTC (rev 4163)
@@ -2010,6 +2010,46 @@
     bl_label = "POV-Ray 3.7"
     DELAY = 0.5
 
+    @staticmethod
+    def locate_binary():
+        addon_prefs = bpy.context.user_preferences.addons[__package__].preferences
+
+        # Use the system preference if its set.
+        pov_binary = addon_prefs.filepath_povray
+        if pov_binary:
+            if os.path.exists(pov_binary):
+                return pov_binary
+            else:
+                print("User Preference to povray %r NOT FOUND, checking $PATH" % pov_binary)
+
+        # Windows Only
+        # assume if there is a 64bit binary that the user has a 64bit capable OS
+        if sys.platform[:3] == "win":
+            import winreg
+            win_reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\POV-Ray\\v3.7\\Windows")
+            win_home = winreg.QueryValueEx(win_reg_key, "Home")[0]
+
+            # First try 64bits
+            pov_binary = os.path.join(win_home, "bin", "pvengine64.exe")
+            if os.path.exists(pov_binary):
+                return pov_binary
+
+            # Then try 32bits
+            pov_binary = os.path.join(win_home, "bin", "pvengine.exe")
+            if os.path.exists(pov_binary):
+                return pov_binary
+
+        # search the path all os's
+        pov_binary_default = "povray"
+
+        os_path_ls = os.getenv("PATH").split(':') + [""]
+
+        for dir_name in os_path_ls:
+            pov_binary = os.path.join(dir_name, pov_binary_default)
+            if os.path.exists(pov_binary):
+                return pov_binary
+        return ""
+
     def _export(self, scene, povPath, renderImagePath):
         import tempfile
 
@@ -2042,28 +2082,20 @@
         write_pov(self._temp_file_in, scene, info_callback)
 
     def _render(self, scene):
-        addon_prefs = bpy.context.user_preferences.addons[__package__].preferences
-
         try:
             os.remove(self._temp_file_out)  # so as not to load the old file
         except OSError:
             pass
 
+        pov_binary = PovrayRender.locate_binary()
+        if not pov_binary:
+            print("POV-Ray 3.7: could not execute povray, possibly POV-Ray isn't installed")
+            return False
+
         write_pov_ini(scene, self._temp_file_ini, self._temp_file_in, self._temp_file_out)
 
         print ("***-STARTING-***")
 
-        # Use the system preference if its set.
-        pov_binary = addon_prefs.filepath_povray
-        pov_binary_default = "povray"
-        if pov_binary:
-            pov_binary_found = os.path.exists(pov_binary)
-        else:
-            pov_binary = pov_binary_default
-            pov_binary_found = False
-            
-        
-
         extra_args = []
 
         if scene.pov.command_line_switches != "":
@@ -2074,135 +2106,27 @@
         if sys.platform[:3] == "win":
             self._is_windows = True
             #extra_args.append("/EXIT")
+        else:
+            # added -d option to prevent render window popup which leads to segfault on linux
+            extra_args.append("-d")
 
-            import winreg
-            import platform as pltfrm
-            if pltfrm.architecture()[0] == "64bit":
-                bitness = 64
-            else:
-                bitness = 32
-
-            regKey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\POV-Ray\\v3.7\\Windows")
-
+        # Start Rendering!
+        try:
+            self._process = subprocess.Popen([pov_binary, self._temp_file_ini] + extra_args,
+                                             stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+        except OSError:
             # TODO, report api
+            print("POV-Ray 3.7: could not execute '%s'" % pov_binary)
+            import traceback
+            traceback.print_exc()
+            print ("***-DONE-***")
+            return False
 
-            # 64 bits blender
-            if bitness == 64:
-                try:
-                    if not pov_binary_found:
-                        pov_binary = winreg.QueryValueEx(regKey, "Home")[0] + "\\bin\\pvengine64"
-                    self._process = subprocess.Popen(
-                            [pov_binary, self._temp_file_ini] + extra_args,
-                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-                    # This would work too but means we have to wait until its done:
-                    # os.system("%s %s" % (pov_binary, self._temp_file_ini))
-
-                except OSError:
-                    # someone might run povray 32 bits on a 64 bits blender machine
-                    try:
-                        if not pov_binary_found:
-                            pov_binary = winreg.QueryValueEx(regKey, "Home")[0] + "\\bin\\pvengine"
-                        self._process = subprocess.Popen(
-                                [pov_binary, self._temp_file_ini] + extra_args,
-                                stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-
-                    except OSError:
-                        # TODO, report api
-                        print("POV-Ray 3.7: could not execute '%s', possibly POV-Ray isn't " \
-                              "installed" % pov_binary)
-                        import traceback
-                        traceback.print_exc()
-                        print ("***-DONE-***")
-                        return False
-
-                    else:
-                        print("POV-Ray 3.7 64 bits could not execute, running 32 bits instead")
-                        print("Command line arguments passed: " + str(extra_args))
-                        return True
-
-                else:
-                    print("POV-Ray 3.7 64 bits found")
-                    print("Command line arguments passed: " + str(extra_args))
-                    return True
-
-            #32 bits blender
-            else:
-                try:
-                    if not pov_binary_found:
-                        pov_binary = winreg.QueryValueEx(regKey, "Home")[0] + "\\bin\\pvengine"
-                    self._process = subprocess.Popen(
-                            [pov_binary, self._temp_file_ini] + extra_args,
-                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-
-                # someone might also run povray 64 bits with a 32 bits build of blender.
-                except OSError:
-                    try:
-                        if not pov_binary_found:
-                            pov_binary = winreg.QueryValueEx(regKey, "Home")[0] + "\\bin\\pvengine64"
-                        self._process = subprocess.Popen(
-                                [pov_binary, self._temp_file_ini] + extra_args,
-                                stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-
-                    except OSError:
-                        # TODO, report api
-                        print("POV-Ray 3.7: could not execute '%s', possibly POV-Ray isn't " \
-                              "installed" % pov_binary)
-                        import traceback
-                        traceback.print_exc()
-                        print ("***-DONE-***")
-                        return False
-
-                    else:
-                        print("Running POV-Ray 3.7 64 bits build with 32 bits Blender,\n" \
-                              "You might want to run Blender 64 bits as well.")
-                        print("Command line arguments passed: " + str(extra_args))
-                        return True
-
-                else:
-                    print("POV-Ray 3.7 32 bits found")
-                    print("Command line arguments passed: " + str(extra_args))
-                    return True
-
         else:
-            # DH - added -d option to prevent render window popup which leads to segfault on linux
-            extra_args.append("-d")
+            print("POV-Ray 3.7 found")
+            print("Command line arguments passed: " + str(extra_args))
+            return True
 
-            if not pov_binary_found:
-                pov_binary_found = False
-                sysPathList = os.getenv("PATH").split(':')
-                sysPathList.append("")
-
-                for dirName in sysPathList:
-                    fn = os.path.join(dirName, pov_binary_default)
-                    if os.path.exists(fn):
-                        pov_binary = fn
-                        pov_binary_found = True
-                        break
-
-                if not pov_binary_found:
-                    print("POV-Ray 3.7: could not found execute '%s' - not if PATH" % pov_binary)
-                    import traceback
-                    traceback.print_exc()
-                    print ("***-DONE-***")
-                    return False
-
-            try:
-                self._process = subprocess.Popen([pov_binary, self._temp_file_ini] + extra_args,
-                                                 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-
-            except OSError:
-                # TODO, report api
-                print("POV-Ray 3.7: could not execute '%s'" % pov_binary)
-                import traceback
-                traceback.print_exc()
-                print ("***-DONE-***")
-                return False
-
-            else:
-                print("POV-Ray 3.7 found")
-                print("Command line arguments passed: " + str(extra_args))
-                return True
-
         # Now that we have a valid process
 
     def _cleanup(self):



More information about the Bf-extensions-cvs mailing list