Some improvements in the SVG writer
[vrm.git] / vrm.py
diff --git a/vrm.py b/vrm.py
index 00c8ed4..d85b085 100755 (executable)
--- a/vrm.py
+++ b/vrm.py
@@ -34,7 +34,7 @@ Tooltip: 'Vector Rendering Method Export Script 0.3'
 #
 # Additional credits:
 #   Thanks to Emilio Aguirre for S2flender from which I took inspirations :)
-#   Thanks to Anthony C. D'Agostino for the backface.py script   
+#   Thanks to Anthony C. D'Agostino for the original backface.py script   
 #
 # ---------------------------------------------------------------------
 
@@ -179,7 +179,7 @@ class Projector:
 
 # ---------------------------------------------------------------------
 #
-## Mesh representation class
+## Object representation class
 #
 # ---------------------------------------------------------------------
 
@@ -264,6 +264,8 @@ class SVGVectorWriter(VectorWriter):
             for face in obj.getData().faces:
                 self._printPolygon(face)
 
+            self._printWireframe(obj.getData())
+            
             self.file.write("</g>\n")
         
         self._printFooter()
@@ -276,7 +278,9 @@ class SVGVectorWriter(VectorWriter):
         """Print SVG header."""
 
         self.file.write("<?xml version=\"1.0\"?>\n")
-        self.file.write("<svg version=\"1.2\"\n")
+        self.file.write("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n")
+        self.file.write("\t\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n")
+        self.file.write("<svg version=\"1.1\"\n")
         self.file.write("\txmlns=\"http://www.w3.org/2000/svg\"\n")
         self.file.write("\twidth=\"%d\" height=\"%d\" streamable=\"true\">\n\n" %
                 self.canvasSize)
@@ -287,6 +291,31 @@ class SVGVectorWriter(VectorWriter):
         self.file.write("\n</svg>\n")
         self.file.close()
 
+    def _printWireframe(self, mesh):
+        """Print the wireframe using mesh edges... is this the correct way?
+        """
+
+        print mesh.edges
+        print
+        print mesh.verts
+        
+        stroke_width=0.5
+        stroke_col = [0, 0, 0]
+        
+        self.file.write("<g>\n")
+
+        for e in mesh.edges:
+            self.file.write("<line x1=\"%g\" y1=\"%g\" x2=\"%g\" y2=\"%g\"\n"
+                    % ( e.v1[0], e.v1[1], e.v2[0], e.v2[1] ) )
+            self.file.write(" style=\"stroke:rgb("+str(stroke_col[0])+","+str(stroke_col[1])+","+str(stroke_col[2])+");")
+            self.file.write(" stroke-width:"+str(stroke_width)+";\n")
+            self.file.write(" stroke-linecap:round;stroke-linejoin:round")
+            self.file.write("\"/>\n")
+
+        self.file.write("</g>\n")
+            
+        
+
     def _printPolygon(self, face):
         """Print our primitive, finally.
         """
@@ -407,6 +436,13 @@ class Renderer:
             transformed_mesh = NMesh.New("flat"+obj.name)
             transformed_mesh.hasVertexColours(1)
 
+            # process Edges
+            for v in obj.getData().verts:
+                transformed_mesh.verts.append(v)
+            transformed_mesh.edges = self._processEdges(obj.getData().edges)
+            print transformed_mesh.edges
+
+            
             # Store the materials
             materials = obj.getData().getMaterials()
 
@@ -558,9 +594,29 @@ class Renderer:
 
         return (d<0)
 
-    def _doClipping(face):
+
+    def _doClipping():
+        return
+
+
+    # Per object methods
+
+    def _doVisibleSurfaceDetermination(object):
+        return
+
+    def _doColorizing(object):
         return
 
+    def _doStylizingEdges(self, object, style):
+        """Process Mesh Edges. (For now copy the edge data, in next version it
+        can be a place where recognize silouhettes and/or contours).
+
+        input: an edge list
+        return: a processed edge list
+        """
+        return
+
+
 
 # ---------------------------------------------------------------------
 #
@@ -605,12 +661,22 @@ def depthSorting(scene):
         scene.link(o)
     
 def vectorize(filename):
+    """The vectorizing process is as follows:
+     
+     - Open the writer
+     - Render the scene
+     - Close the writer
+     
+     If you want to render an animation the second pass should be
+     repeated for any frame, and the frame number should be passed to the
+     renderer.
+     """
 
     print "Filename: %s" % filename
     
     scene = Scene.GetCurrent()
     renderer = Renderer()
-
+    
     flatScene = renderer.doRendering(scene)
     canvasSize = renderer.getCanvasSize()
 
@@ -624,6 +690,7 @@ def vectorize(filename):
 
 # Here the main
 if __name__ == "__main__":
+    # with this trick we can run the script in batch mode
     try:
         Blender.Window.FileSelector (vectorize, 'Save SVG', "proba.svg")
     except: