Process only object that are on visible layers
[vrm.git] / vrm.py
diff --git a/vrm.py b/vrm.py
index 67bcf3a..6a69a66 100755 (executable)
--- a/vrm.py
+++ b/vrm.py
@@ -83,6 +83,9 @@ __bpydoc__ = """\
 #     * Remove the real file opening in the abstract VectorWriter
 #     * View frustum clipping
 #     * Scene clipping done using bounding box instead of object center
+#     * Fix camera type selection for blender>2.43 (Thanks to Thomas Lachmann)
+#     * Compatibility with python 2.3
+#     * Process only object that are on visible layers.
 #
 # ---------------------------------------------------------------------
 
@@ -92,6 +95,13 @@ from Blender.Mathutils import *
 from math import *
 import sys, time
 
+def uniq(alist):
+    tmpdict = dict()
+    return [tmpdict.setdefault(e,e) for e in alist if e not in tmpdict]
+    # in python > 2.4 we ca use the following
+    #return [ u for u in alist if u not in locals()['_[1]'] ]
+
+
 # Constants
 EPS = 10e-5
 
@@ -683,9 +693,13 @@ class HSR:
                             negVertList.append(V1)
 
         
-        # uniq
-        posVertList = [ u for u in posVertList if u not in locals()['_[1]'] ]
-        negVertList = [ u for u in negVertList if u not in locals()['_[1]'] ]
+        # uniq for python > 2.4
+        #posVertList = [ u for u in posVertList if u not in locals()['_[1]'] ]
+        #negVertList = [ u for u in negVertList if u not in locals()['_[1]'] ]
+
+        # a more portable way
+        posVertList = uniq(posVertList)
+        negVertList = uniq(negVertList)
 
 
         # If vertex are all on the same half-space, return
@@ -887,13 +901,22 @@ class Projector:
 
         fovy = atan(0.5/aspect/(camera.lens/32))
         fovy = fovy * 360.0/pi
-        
+
+
+        if Blender.Get('version') < 243:
+            camPersp = 0
+            camOrtho = 1
+        else:
+            camPersp = 'persp'
+            camOrtho = 'ortho'
+            
         # What projection do we want?
-        if camera.type == 0:
+        if camera.type == camPersp:
             mP = self._calcPerspectiveMatrix(fovy, aspect, near, far) 
-        elif camera.type == 1:
-            mP = self._calcOrthoMatrix(fovy, aspect, near, far, scale) 
+        elif camera.type == camOrtho:
+            mP = self._calcOrthoMatrix(fovy, aspect, near, far, scale)
         
+
         # View transformation
         cam = Matrix(cameraObj.getInverseMatrix())
         cam.transpose() 
@@ -1863,18 +1886,7 @@ class Renderer:
         # Render from the currently active camera 
         #self.cameraObj = self._SCENE.getCurrentCamera()
 
-        # Get the list of lighting sources
-        obj_lst = self._SCENE.getChildren()
-        self.lights = [ o for o in obj_lst if o.getType() == 'Lamp']
-
-        # When there are no lights we use a default lighting source
-        # that have the same position of the camera
-        if len(self.lights) == 0:
-            l = Lamp.New('Lamp')
-            lobj = Object.New('Lamp')
-            lobj.loc = self.cameraObj.loc
-            lobj.link(l) 
-            self.lights.append(lobj)
+        self.lights = []
 
 
     ##
@@ -1963,6 +1975,10 @@ class Renderer:
         
         # global processing of the scene
 
+        self._filterHiddenObjects(workScene)
+
+        self._buildLightSetup(workScene)
+
         self._doSceneClipping(workScene)
 
         self._doConvertGeometricObjsToMesh(workScene)
@@ -1975,6 +1991,7 @@ class Renderer:
         # Per object activities
 
         Objects = workScene.getChildren()
+
         print "Total Objects: %d" % len(Objects)
         for i,obj in enumerate(Objects):
             print "\n\n-------"
@@ -2087,6 +2104,38 @@ class Renderer:
 
     # Scene methods
 
+    def _filterHiddenObjects(self, scene):
+        """Discard object that are on hidden layers in the scene.
+        """
+
+        Objects = scene.getChildren()
+
+        visible_obj_list = [ obj for obj in Objects if
+                set(obj.layers).intersection(set(scene.getLayers())) ]
+
+        for o in Objects:
+            if o not in visible_obj_list:
+                scene.unlink(o)
+
+        scene.update()
+
+
+
+    def _buildLightSetup(self, scene):
+        # Get the list of lighting sources
+        obj_lst = scene.getChildren()
+        self.lights = [ o for o in obj_lst if o.getType() == 'Lamp' ]
+
+        # When there are no lights we use a default lighting source
+        # that have the same position of the camera
+        if len(self.lights) == 0:
+            l = Lamp.New('Lamp')
+            lobj = Object.New('Lamp')
+            lobj.loc = self.cameraObj.loc
+            lobj.link(l) 
+            self.lights.append(lobj)
+
+
     def _doSceneClipping(self, scene):
         """Clip whole objects against the View Frustum.
 
@@ -2104,6 +2153,7 @@ class Renderer:
         fovy = fovy * 360.0/pi
 
         Objects = scene.getChildren()
+
         for o in Objects:
             if o.getType() != 'Mesh': continue;
 
@@ -2148,6 +2198,7 @@ class Renderer:
         #geometricObjTypes = ['Mesh', 'Surf', 'Curve']
 
         Objects = scene.getChildren()
+
         objList = [ o for o in Objects if o.getType() in geometricObjTypes ]
         for obj in objList:
             old_obj = obj
@@ -2194,6 +2245,7 @@ class Renderer:
 
         
         Objects = scene.getChildren()
+
         #Objects.sort(by_obj_center_pos)
         Objects.sort(by_nearest_bbox_point)