6 Tooltip: 'Vector Rendering Method script'
9 __author__ = "Antonio Ospite"
10 __url__ = ["http://vrm.projects.blender.org"]
14 Render the scene and save the result in vector format.
17 # ---------------------------------------------------------------------
18 # Copyright (c) 2006 Antonio Ospite
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 2 of the License, or
23 # (at your option) any later version.
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 # GNU General Public License for more details.
30 # You should have received a copy of the GNU General Public License
31 # along with this program; if not, write to the Free Software
32 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 # ---------------------------------------------------------------------
37 # Thanks to Emilio Aguirre for S2flender from which I took inspirations :)
38 # Thanks to Nikola Radovanovic, the author of the original VRM script,
39 # the code you read here has been rewritten _almost_ entirely
40 # from scratch but Nikola gave me the idea, so I thank him publicly.
42 # ---------------------------------------------------------------------
44 # Things TODO for a next release:
45 # - Switch to the Mesh structure, should be considerably faster
46 # (partially done, but with Mesh we cannot sort faces, yet)
47 # - Use a better depth sorting algorithm
48 # - Review how selections are made (this script uses selection states of
49 # primitives to represent visibility infos)
50 # - Implement clipping of primitives and do handle object intersections.
51 # (for now only clipping for whole objects is supported).
52 # - Implement Edge Styles (silhouettes, contours, etc.) (partially done).
53 # - Use multiple lighting sources in color calculation
54 # - Implement Shading Styles? (for now we use Flat Shading).
55 # - Use a data structure other than Mesh to represent the 2D image?
56 # Think to a way to merge adjacent polygons that have the same color.
57 # Or a way to use paths for silhouettes and contours.
58 # - Add Vector Writers other that SVG.
59 # - Consider SMIL for animation handling instead of ECMA Script? (Firefox do
60 # not support SMIL for animations)
61 # - FIX the issue with negative scales in object tranformations!
63 # ---------------------------------------------------------------------
67 # vrm-0.3.py - 2006-05-19
68 # * First release after code restucturing.
69 # Now the script offers a useful set of functionalities
70 # and it can render animations, too.
72 # ---------------------------------------------------------------------
75 from Blender import Scene, Object, Mesh, NMesh, Material, Lamp, Camera
76 from Blender.Mathutils import *
80 # Some global settings
84 polygons['SHOW'] = True
85 polygons['SHADING'] = 'TOON'
86 # Hidden to the user for now
87 polygons['EXPANSION_TRICK'] = True
91 edges['SHOW_HIDDEN'] = False
92 edges['STYLE'] = 'SILHOUETTE'
94 edges['COLOR'] = [0, 0, 0]
97 output['FORMAT'] = 'SVG'
98 output['ANIMATION'] = False
99 output['JOIN_OBJECTS'] = True
103 # ---------------------------------------------------------------------
105 ## Utility Mesh class
107 # ---------------------------------------------------------------------
110 def getEdgeAdjacentFaces(edge, mesh):
111 """Get the faces adjacent to a given edge.
113 There can be 0, 1 or more (usually 2) faces adjacent to an edge.
118 if (edge.v1 in f.v) and (edge.v2 in f.v):
119 adjface_list.append(f)
123 def isMeshEdge(e, mesh):
126 A mesh edge is visible if _any_ of its adjacent faces is selected.
127 Note: if the edge has no adjacent faces we want to show it as well,
128 useful for "edge only" portion of objects.
131 adjacent_faces = MeshUtils.getEdgeAdjacentFaces(e, mesh)
133 if len(adjacent_faces) == 0:
136 selected_faces = [f for f in adjacent_faces if f.sel]
138 if len(selected_faces) != 0:
143 def isSilhouetteEdge(e, mesh):
144 """Silhuette selection rule.
146 An edge is a silhuette edge if it is shared by two faces with
147 different selection status or if it is a boundary edge of a selected
151 adjacent_faces = MeshUtils.getEdgeAdjacentFaces(e, mesh)
153 if ((len(adjacent_faces) == 1 and adjacent_faces[0].sel == 1) or
154 (len(adjacent_faces) == 2 and
155 adjacent_faces[0].sel != adjacent_faces[1].sel)
164 texels = 2*levels - 1
165 map = [0.0] + [(i)/float(texels-1) for i in range(1, texels-1) ] + [1.0]
168 for i in range(0, len(map)-1):
169 pivot = (map[i]+map[i+1])/2.0
180 getEdgeAdjacentFaces = staticmethod(getEdgeAdjacentFaces)
181 isMeshEdge = staticmethod(isMeshEdge)
182 isSilhouetteEdge = staticmethod(isSilhouetteEdge)
183 toonShading = staticmethod(toonShading)
187 # ---------------------------------------------------------------------
189 ## Projections classes
191 # ---------------------------------------------------------------------
194 """Calculate the projection of an object given the camera.
196 A projector is useful to so some per-object transformation to obtain the
197 projection of an object given the camera.
199 The main method is #doProjection# see the method description for the
203 def __init__(self, cameraObj, canvasRatio):
204 """Calculate the projection matrix.
206 The projection matrix depends, in this case, on the camera settings.
207 TAKE CARE: This projector expects vertices in World Coordinates!
210 camera = cameraObj.getData()
212 aspect = float(canvasRatio[0])/float(canvasRatio[1])
213 near = camera.clipStart
216 scale = float(camera.scale)
218 fovy = atan(0.5/aspect/(camera.lens/32))
219 fovy = fovy * 360.0/pi
221 # What projection do we want?
223 #mP = self._calcOrthoMatrix(fovy, aspect, near, far, 17) #camera.scale)
224 mP = self._calcOrthoMatrix(fovy, aspect, near, far, scale)
226 mP = self._calcPerspectiveMatrix(fovy, aspect, near, far)
228 # View transformation
229 cam = Matrix(cameraObj.getInverseMatrix())
234 self.projectionMatrix = mP
240 def doProjection(self, v):
241 """Project the point on the view plane.
243 Given a vertex calculate the projection using the current projection
247 # Note that we have to work on the vertex using homogeneous coordinates
248 p = self.projectionMatrix * Vector(v).resize4D()
264 def _calcPerspectiveMatrix(self, fovy, aspect, near, far):
265 """Return a perspective projection matrix.
268 top = near * tan(fovy * pi / 360.0)
272 x = (2.0 * near) / (right-left)
273 y = (2.0 * near) / (top-bottom)
274 a = (right+left) / (right-left)
275 b = (top+bottom) / (top - bottom)
276 c = - ((far+near) / (far-near))
277 d = - ((2*far*near)/(far-near))
283 [0.0, 0.0, -1.0, 0.0])
287 def _calcOrthoMatrix(self, fovy, aspect , near, far, scale):
288 """Return an orthogonal projection matrix.
291 # The 11 in the formula was found emiprically
292 top = near * tan(fovy * pi / 360.0) * (scale * 11)
294 left = bottom * aspect
299 tx = -((right+left)/rl)
300 ty = -((top+bottom)/tb)
304 [2.0/rl, 0.0, 0.0, tx],
305 [0.0, 2.0/tb, 0.0, ty],
306 [0.0, 0.0, 2.0/fn, tz],
307 [0.0, 0.0, 0.0, 1.0])
313 # ---------------------------------------------------------------------
315 ## 2D Object representation class
317 # ---------------------------------------------------------------------
319 # TODO: a class to represent the needed properties of a 2D vector image
320 # For now just using a [N]Mesh structure.
323 # ---------------------------------------------------------------------
325 ## Vector Drawing Classes
327 # ---------------------------------------------------------------------
333 A class for printing output in a vectorial format.
335 Given a 2D representation of the 3D scene the class is responsible to
336 write it is a vector format.
338 Every subclasses of VectorWriter must have at last the following public
342 - printCanvas(self, scene,
343 doPrintPolygons=True, doPrintEdges=False, showHiddenEdges=False):
346 def __init__(self, fileName):
347 """Set the output file name and other properties"""
349 self.outputFileName = fileName
352 context = Scene.GetCurrent().getRenderingContext()
353 self.canvasSize = ( context.imageSizeX(), context.imageSizeY() )
357 self.animation = False
364 def open(self, startFrame=1, endFrame=1):
365 if startFrame != endFrame:
366 self.startFrame = startFrame
367 self.endFrame = endFrame
368 self.animation = True
370 self.file = open(self.outputFileName, "w")
371 print "Outputting to: ", self.outputFileName
379 def printCanvas(self, scene, doPrintPolygons=True, doPrintEdges=False,
380 showHiddenEdges=False):
381 """This is the interface for the needed printing routine.
388 class SVGVectorWriter(VectorWriter):
389 """A concrete class for writing SVG output.
392 def __init__(self, fileName):
393 """Simply call the parent Contructor.
395 VectorWriter.__init__(self, fileName)
402 def open(self, startFrame=1, endFrame=1):
403 """Do some initialization operations.
405 VectorWriter.open(self, startFrame, endFrame)
409 """Do some finalization operation.
413 # remember to call the close method of the parent
414 VectorWriter.close(self)
417 def printCanvas(self, scene, doPrintPolygons=True, doPrintEdges=False,
418 showHiddenEdges=False):
419 """Convert the scene representation to SVG.
422 Objects = scene.getChildren()
424 context = scene.getRenderingContext()
425 framenumber = context.currentFrame()
428 framestyle = "display:none"
430 framestyle = "display:block"
432 # Assign an id to this group so we can set properties on it using DOM
433 self.file.write("<g id=\"frame%d\" style=\"%s\">\n" %
434 (framenumber, framestyle) )
439 if(obj.getType() != 'Mesh'):
442 self.file.write("<g id=\"%s\">\n" % obj.getName())
444 mesh = obj.getData(mesh=1)
447 self._printPolygons(mesh)
450 self._printEdges(mesh, showHiddenEdges)
452 self.file.write("</g>\n")
454 self.file.write("</g>\n")
461 def _calcCanvasCoord(self, v):
462 """Convert vertex in scene coordinates to canvas coordinates.
465 pt = Vector([0, 0, 0])
467 mW = float(self.canvasSize[0])/2.0
468 mH = float(self.canvasSize[1])/2.0
470 # rescale to canvas size
471 pt[0] = v.co[0]*mW + mW
472 pt[1] = v.co[1]*mH + mH
475 # For now we want (0,0) in the top-left corner of the canvas.
476 # Mirror and translate along y
478 pt[1] += self.canvasSize[1]
482 def _printHeader(self):
483 """Print SVG header."""
485 self.file.write("<?xml version=\"1.0\"?>\n")
486 self.file.write("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n")
487 self.file.write("\t\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n")
488 self.file.write("<svg version=\"1.1\"\n")
489 self.file.write("\txmlns=\"http://www.w3.org/2000/svg\"\n")
490 self.file.write("\twidth=\"%d\" height=\"%d\" streamable=\"true\">\n\n" %
495 self.file.write("""\n<script><![CDATA[
499 /* FIXME: Use 1000 as interval as lower values gives problems */
500 timerID = setInterval("NextFrame()", 1000);
501 globalFrameCounter=%d;
505 currentElement = document.getElementById('frame'+globalFrameCounter)
506 previousElement = document.getElementById('frame'+(globalFrameCounter-1))
513 if (globalFrameCounter > globalEndFrame)
515 clearInterval(timerID)
521 previousElement.style.display="none";
523 currentElement.style.display="block";
524 globalFrameCounter++;
528 \n""" % (self.startFrame, self.endFrame, self.startFrame) )
530 def _printFooter(self):
531 """Print the SVG footer."""
533 self.file.write("\n</svg>\n")
535 def _printPolygons(self, mesh):
536 """Print the selected (visible) polygons.
539 if len(mesh.faces) == 0:
542 self.file.write("<g>\n")
544 for face in mesh.faces:
548 self.file.write("<polygon points=\"")
551 p = self._calcCanvasCoord(v)
552 self.file.write("%g,%g " % (p[0], p[1]))
554 # get rid of the last blank space, just cosmetics here.
555 self.file.seek(-1, 1)
556 self.file.write("\"\n")
558 # take as face color the first vertex color
559 # TODO: the average of vetrex colors?
562 color = [fcol.r, fcol.g, fcol.b, fcol.a]
564 color = [255, 255, 255, 255]
566 # Convert the color to the #RRGGBB form
567 str_col = "#%02X%02X%02X" % (color[0], color[1], color[2])
569 # use the stroke property to alleviate the "adjacent edges" problem,
570 # we simulate polygon expansion using borders,
571 # see http://www.antigrain.com/svg/index.html for more info
574 # Handle transparent polygons
577 opacity = float(color[3])/255.0
578 opacity_string = " fill-opacity: %g; stroke-opacity: %g; opacity: 1;" % (opacity, opacity)
580 self.file.write("\tstyle=\"fill:" + str_col + ";")
581 self.file.write(opacity_string)
582 if config.polygons['EXPANSION_TRICK']:
583 self.file.write(" stroke:" + str_col + ";")
584 self.file.write(" stroke-width:" + str(stroke_width) + ";\n")
585 self.file.write(" stroke-linecap:round;stroke-linejoin:round")
586 self.file.write("\"/>\n")
588 self.file.write("</g>\n")
590 def _printEdges(self, mesh, showHiddenEdges=False):
591 """Print the wireframe using mesh edges.
594 stroke_width = config.edges['WIDTH']
595 stroke_col = config.edges['COLOR']
597 self.file.write("<g>\n")
601 hidden_stroke_style = ""
604 if showHiddenEdges == False:
607 hidden_stroke_style = ";\n stroke-dasharray:3, 3"
609 p1 = self._calcCanvasCoord(e.v1)
610 p2 = self._calcCanvasCoord(e.v2)
612 self.file.write("<line x1=\"%g\" y1=\"%g\" x2=\"%g\" y2=\"%g\"\n"
613 % ( p1[0], p1[1], p2[0], p2[1] ) )
614 self.file.write(" style=\"stroke:rgb("+str(stroke_col[0])+","+str(stroke_col[1])+","+str(stroke_col[2])+");")
615 self.file.write(" stroke-width:"+str(stroke_width)+";\n")
616 self.file.write(" stroke-linecap:round;stroke-linejoin:round")
617 self.file.write(hidden_stroke_style)
618 self.file.write("\"/>\n")
620 self.file.write("</g>\n")
624 # ---------------------------------------------------------------------
628 # ---------------------------------------------------------------------
630 # A dictionary to collect different shading style methods
631 shadingStyles = dict()
632 shadingStyles['FLAT'] = None
633 shadingStyles['TOON'] = None
635 # A dictionary to collect different edge style methods
637 edgeStyles['MESH'] = MeshUtils.isMeshEdge
638 edgeStyles['SILHOUETTE'] = MeshUtils.isSilhouetteEdge
640 # A dictionary to collect the supported output formats
641 outputWriters = dict()
642 outputWriters['SVG'] = SVGVectorWriter
646 """Render a scene viewed from a given camera.
648 This class is responsible of the rendering process, transformation and
649 projection of the objects in the scene are invoked by the renderer.
651 The rendering is done using the active camera for the current scene.
655 """Make the rendering process only for the current scene by default.
657 We will work on a copy of the scene, be sure that the current scene do
658 not get modified in any way.
661 # Render the current Scene, this should be a READ-ONLY property
662 self._SCENE = Scene.GetCurrent()
664 # Use the aspect ratio of the scene rendering context
665 context = self._SCENE.getRenderingContext()
667 aspect_ratio = float(context.imageSizeX())/float(context.imageSizeY())
668 self.canvasRatio = (float(context.aspectRatioX())*aspect_ratio,
669 float(context.aspectRatioY())
672 # Render from the currently active camera
673 self.cameraObj = self._SCENE.getCurrentCamera()
675 # Get a projector for this camera.
676 # NOTE: the projector wants object in world coordinates,
677 # so we should remember to apply modelview transformations
678 # _before_ we do projection transformations.
679 self.proj = Projector(self.cameraObj, self.canvasRatio)
681 # Get the list of lighting sources
682 obj_lst = self._SCENE.getChildren()
683 self.lights = [ o for o in obj_lst if o.getType() == 'Lamp']
685 # When there are no lights we use a default lighting source
686 # that have the same position of the camera
687 if len(self.lights) == 0:
689 lobj = Object.New('Lamp')
690 lobj.loc = self.cameraObj.loc
692 self.lights.append(lobj)
699 def doRendering(self, outputWriter, animation=False):
700 """Render picture or animation and write it out.
703 - a Vector writer object that will be used to output the result.
704 - a flag to tell if we want to render an animation or only the
708 context = self._SCENE.getRenderingContext()
709 origCurrentFrame = context.currentFrame()
711 # Handle the animation case
713 startFrame = origCurrentFrame
714 endFrame = startFrame
717 startFrame = context.startFrame()
718 endFrame = context.endFrame()
719 outputWriter.open(startFrame, endFrame)
721 # Do the rendering process frame by frame
722 print "Start Rendering!"
723 for f in range(startFrame, endFrame+1):
724 context.currentFrame(f)
726 # Use some temporary workspace, a full copy of the scene
727 inputScene = self._SCENE.copy(2)
730 renderedScene = self.doRenderScene(inputScene)
732 print "There was an error! Aborting."
734 print traceback.print_exc()
736 self._SCENE.makeCurrent()
737 Scene.unlink(inputScene)
741 outputWriter.printCanvas(renderedScene,
742 doPrintPolygons = config.polygons['SHOW'],
743 doPrintEdges = config.edges['SHOW'],
744 showHiddenEdges = config.edges['SHOW_HIDDEN'])
746 # clear the rendered scene
747 self._SCENE.makeCurrent()
748 Scene.unlink(renderedScene)
753 context.currentFrame(origCurrentFrame)
756 def doRenderScene(self, workScene):
757 """Control the rendering process.
759 Here we control the entire rendering process invoking the operation
760 needed to transform and project the 3D scene in two dimensions.
763 # global processing of the scene
765 self._doConvertGeometricObjToMesh(workScene)
767 self._doSceneClipping(workScene)
769 if config.output['JOIN_OBJECTS']:
770 self._joinMeshObjectsInScene(workScene)
772 self._doSceneDepthSorting(workScene)
774 # Per object activities
776 Objects = workScene.getChildren()
779 if obj.getType() != 'Mesh':
780 print "Only Mesh supported! - Skipping type:", obj.getType()
783 print "Rendering: ", obj.getName()
785 mesh = obj.getData(mesh=1)
787 self._doModelToWorldCoordinates(mesh, obj.matrix)
789 self._doObjectDepthSorting(mesh)
791 self._doBackFaceCulling(mesh)
793 self._doColorAndLighting(mesh)
795 self._doEdgesStyle(mesh, edgeStyles[config.edges['STYLE']])
797 self._doProjection(mesh, self.proj)
799 # Update the object data, important! :)
811 def _getObjPosition(self, obj):
812 """Return the obj position in World coordinates.
814 return obj.matrix.translationPart()
816 def _cameraViewDirection(self):
817 """Get the View Direction form the camera matrix.
819 return Vector(self.cameraObj.matrix[2]).resize3D()
824 def _isFaceVisible(self, face):
825 """Determine if a face of an object is visible from the current camera.
827 The view vector is calculated from the camera location and one of the
828 vertices of the face (expressed in World coordinates, after applying
829 modelview transformations).
831 After those transformations we determine if a face is visible by
832 computing the angle between the face normal and the view vector, this
833 angle has to be between -90 and 90 degrees for the face to be visible.
834 This corresponds somehow to the dot product between the two, if it
835 results > 0 then the face is visible.
837 There is no need to normalize those vectors since we are only interested in
838 the sign of the cross product and not in the product value.
840 NOTE: here we assume the face vertices are in WorldCoordinates, so
841 please transform the object _before_ doing the test.
844 normal = Vector(face.no)
845 camPos = self._getObjPosition(self.cameraObj)
848 # View Vector in orthographics projections is the view Direction of
850 if self.cameraObj.data.getType() == 1:
851 view_vect = self._cameraViewDirection()
853 # View vector in perspective projections can be considered as
854 # the difference between the camera position and one point of
855 # the face, we choose the farthest point from the camera.
856 if self.cameraObj.data.getType() == 0:
857 vv = max( [ ((camPos - Vector(v.co)).length, (camPos - Vector(v.co))) for v in face] )
860 # if d > 0 the face is visible from the camera
861 d = view_vect * normal
871 def _doConvertGeometricObjToMesh(self, scene):
872 """Convert all "geometric" objects to mesh ones.
874 geometricObjTypes = ['Mesh', 'Surf', 'Curve', 'Text']
876 Objects = scene.getChildren()
877 objList = [ o for o in Objects if o.getType() in geometricObjTypes ]
880 obj = self._convertToRawMeshObj(obj)
882 scene.unlink(old_obj)
885 # XXX Workaround for Text and Curve which have some normals
886 # inverted when they are converted to Mesh, REMOVE that when
887 # blender will fix that!!
888 if old_obj.getType() in ['Curve', 'Text']:
889 me = obj.getData(mesh=1)
890 for f in me.faces: f.sel = 1;
891 for v in me.verts: v.sel = 1;
897 def _doSceneClipping(self, scene):
898 """Clip objects against the View Frustum.
900 For now clip away only objects according to their center position.
903 cpos = self._getObjPosition(self.cameraObj)
904 view_vect = self._cameraViewDirection()
906 near = self.cameraObj.data.clipStart
907 far = self.cameraObj.data.clipEnd
909 aspect = float(self.canvasRatio[0])/float(self.canvasRatio[1])
910 fovy = atan(0.5/aspect/(self.cameraObj.data.lens/32))
911 fovy = fovy * 360.0/pi
913 Objects = scene.getChildren()
915 if o.getType() != 'Mesh': continue;
917 obj_vect = Vector(cpos) - self._getObjPosition(o)
919 d = obj_vect*view_vect
920 theta = AngleBetweenVecs(obj_vect, view_vect)
922 # if the object is outside the view frustum, clip it away
923 if (d < near) or (d > far) or (theta > fovy):
926 def _doSceneDepthSorting(self, scene):
927 """Sort objects in the scene.
929 The object sorting is done accordingly to the object centers.
932 c = self._getObjPosition(self.cameraObj)
934 by_center_pos = (lambda o1, o2:
935 (o1.getType() == 'Mesh' and o2.getType() == 'Mesh') and
936 cmp((self._getObjPosition(o1) - Vector(c)).length,
937 (self._getObjPosition(o2) - Vector(c)).length)
940 # TODO: implement sorting by bounding box, if obj1.bb is inside obj2.bb,
941 # then ob1 goes farther than obj2, useful when obj2 has holes
944 Objects = scene.getChildren()
945 Objects.sort(by_center_pos)
952 def _joinMeshObjectsInScene(self, scene):
953 """Merge all the Mesh Objects in a scene into a single Mesh Object.
956 if Blender.mode == 'background':
957 print "\nWARNING! Joining objects not supported in background mode!\n"
960 oList = [o for o in scene.getChildren() if o.getType()=='Mesh']
962 # FIXME: Object.join() do not work if the list contains 1 object
966 mesh = Mesh.New('BigOne')
967 bigObj = Object.New('Mesh', 'BigOne')
975 print "Can't Join Objects"
979 print "Objects Type error?"
989 def _convertToRawMeshObj(self, object):
990 """Convert geometry based object to a mesh object.
992 me = Mesh.New('RawMesh_'+object.name)
993 me.getFromObject(object.name)
995 newObject = Object.New('Mesh', 'RawMesh_'+object.name)
998 # If the object has no materials set a default material
1000 me.materials = [Material.New()]
1001 #for f in me.faces: f.mat = 0
1003 newObject.setMatrix(object.getMatrix())
1007 def _doModelToWorldCoordinates(self, mesh, matrix):
1008 """Transform object coordinates to world coordinates.
1010 This step is done simply applying to the object its tranformation
1011 matrix and recalculating its normals.
1013 # XXX FIXME: blender do not transform normals in the right way when
1014 # there are negative scale values
1015 if matrix[0][0] < 0 or matrix[1][1] < 0 or matrix[2][2] < 0:
1016 print "WARNING: Negative scales, expect incorrect results!"
1018 mesh.transform(matrix, True)
1020 def _doObjectDepthSorting(self, mesh):
1021 """Sort faces in an object.
1023 The faces in the object are sorted following the distance of the
1024 vertices from the camera position.
1026 if len(mesh.faces) == 0:
1029 c = self._getObjPosition(self.cameraObj)
1031 # hackish sorting of faces
1033 # Sort faces according to the max distance from the camera
1034 by_max_vert_dist = (lambda f1, f2:
1035 cmp(max([(Vector(v.co)-Vector(c)).length for v in f1]),
1036 max([(Vector(v.co)-Vector(c)).length for v in f2])))
1038 # Sort faces according to the min distance from the camera
1039 by_min_vert_dist = (lambda f1, f2:
1040 cmp(min([(Vector(v.co)-Vector(c)).length for v in f1]),
1041 min([(Vector(v.co)-Vector(c)).length for v in f2])))
1043 # Sort faces according to the avg distance from the camera
1044 by_avg_vert_dist = (lambda f1, f2:
1045 cmp(sum([(Vector(v.co)-Vector(c)).length for v in f1])/len(f1),
1046 sum([(Vector(v.co)-Vector(c)).length for v in f2])/len(f2)))
1049 # FIXME: using NMesh to sort faces. We should avoid that!
1050 nmesh = NMesh.GetRaw(mesh.name)
1051 nmesh.faces.sort(by_max_vert_dist)
1052 nmesh.faces.reverse()
1054 mesh.faces.delete(1, range(0, len(mesh.faces)))
1056 for i,f in enumerate(nmesh.faces):
1057 fv = [v.index for v in f.v]
1058 mesh.faces.extend(fv)
1059 mesh.faces[i].mat = f.mat
1062 def _doBackFaceCulling(self, mesh):
1063 """Simple Backface Culling routine.
1065 At this level we simply do a visibility test face by face and then
1066 select the vertices belonging to visible faces.
1069 # Select all vertices, so edges can be displayed even if there are no
1071 for v in mesh.verts:
1074 Mesh.Mode(Mesh.SelectModes['FACE'])
1076 for f in mesh.faces:
1078 if self._isFaceVisible(f):
1081 def _doColorAndLighting(self, mesh):
1082 """Apply an Illumination ans shading model to the object.
1084 The model used is the Phong one, it may be inefficient,
1085 but I'm just learning about rendering and starting from Phong seemed
1086 the most natural way.
1089 # If the mesh has vertex colors already, use them,
1090 # otherwise turn them on and do some calculations
1091 if mesh.vertexColors:
1093 mesh.vertexColors = 1
1095 materials = mesh.materials
1097 # TODO: use multiple lighting sources
1098 light_obj = self.lights[0]
1099 light_pos = self._getObjPosition(light_obj)
1100 light = light_obj.data
1102 camPos = self._getObjPosition(self.cameraObj)
1104 # We do per-face color calculation (FLAT Shading), we can easily turn
1105 # to a per-vertex calculation if we want to implement some shading
1106 # technique. For an example see:
1107 # http://www.miralab.unige.ch/papers/368.pdf
1108 for f in mesh.faces:
1114 mat = materials[f.mat]
1116 # A new default material
1118 mat = Material.New('defMat')
1120 L = Vector(light_pos).normalize()
1122 V = (Vector(camPos) - Vector(f.cent)).normalize()
1124 N = Vector(f.no).normalize()
1126 R = 2 * (N*L) * N - L
1128 # TODO: Attenuation factor (not used for now)
1129 a0 = 1.0; a1 = 0.0; a2 = 1.0
1130 d = (Vector(f.v[0].co) - Vector(light_pos)).length
1131 fd = min(1, 1.0/(a0 + a1*d + a2*(d*d)))
1135 ka = mat.getAmb() * Vector([0.1, 0.1, 0.1])
1138 # Diffuse component (add light.col for kd)
1139 kd = mat.getRef() * Vector(mat.getRGBCol())
1140 Ip = light.getEnergy()
1142 if config.polygons['SHADING'] == 'FLAT':
1143 Idiff = Ip * kd * (N*L)
1144 elif config.polygons['SHADING'] == 'TOON':
1145 Idiff = Ip * kd * MeshUtils.toonShading(N*L)
1147 # Specular component
1148 ks = mat.getSpec() * Vector(mat.getSpecCol())
1149 ns = mat.getHardness()
1150 Ispec = Ip * ks * pow((V*R), ns)
1152 # Emissive component
1153 ki = Vector([mat.getEmit()]*3)
1155 I = ki + Iamb + (Idiff + Ispec)
1158 # Set Alpha component
1160 I.append(mat.getAlpha())
1162 # Clamp I values between 0 and 1
1163 I = [ min(c, 1) for c in I]
1164 I = [ max(0, c) for c in I]
1166 # Convert to a value between 0 and 255
1167 tmp_col = [ int(c * 255.0) for c in I]
1175 def _doEdgesStyle(self, mesh, edgestyleSelect):
1176 """Process Mesh Edges accroding to a given selection style.
1178 Examples of algorithms:
1181 given an edge if its adjacent faces have the same normal (that is
1182 they are complanar), than deselect it.
1185 given an edge if one its adjacent faces is frontfacing and the
1186 other is backfacing, than select it, else deselect.
1189 Mesh.Mode(Mesh.SelectModes['EDGE'])
1191 for e in mesh.edges:
1194 if edgestyleSelect(e, mesh):
1197 def _doProjection(self, mesh, projector):
1198 """Calculate the Projection for the object.
1200 # TODO: maybe using the object.transform() can be faster?
1202 for v in mesh.verts:
1203 p = projector.doProjection(v.co)
1210 # ---------------------------------------------------------------------
1212 ## GUI Class and Main Program
1214 # ---------------------------------------------------------------------
1217 from Blender import BGL, Draw
1218 from Blender.BGL import *
1224 # Output Format menu
1225 output_format = config.output['FORMAT']
1226 default_value = outputWriters.keys().index(output_format)+1
1227 GUI.outFormatMenu = Draw.Create(default_value)
1228 GUI.evtOutFormatMenu = 0
1230 # Animation toggle button
1231 GUI.animToggle = Draw.Create(config.output['ANIMATION'])
1232 GUI.evtAnimToggle = 1
1234 # Join Objects toggle button
1235 GUI.joinObjsToggle = Draw.Create(config.output['JOIN_OBJECTS'])
1236 GUI.evtJoinObjsToggle = 2
1238 # Render filled polygons
1239 GUI.polygonsToggle = Draw.Create(config.polygons['SHOW'])
1241 # Shading Style menu
1242 shading_style = config.polygons['SHADING']
1243 default_value = shadingStyles.keys().index(shading_style)+1
1244 GUI.shadingStyleMenu = Draw.Create(default_value)
1245 GUI.evtShadingStyleMenu = 21
1247 GUI.evtPolygonsToggle = 3
1248 # We hide the config.polygons['EXPANSION_TRICK'], for now
1250 # Render polygon edges
1251 GUI.showEdgesToggle = Draw.Create(config.edges['SHOW'])
1252 GUI.evtShowEdgesToggle = 4
1254 # Render hidden edges
1255 GUI.showHiddenEdgesToggle = Draw.Create(config.edges['SHOW_HIDDEN'])
1256 GUI.evtShowHiddenEdgesToggle = 5
1259 edge_style = config.edges['STYLE']
1260 default_value = edgeStyles.keys().index(edge_style)+1
1261 GUI.edgeStyleMenu = Draw.Create(default_value)
1262 GUI.evtEdgeStyleMenu = 6
1265 GUI.edgeWidthSlider = Draw.Create(config.edges['WIDTH'])
1266 GUI.evtEdgeWidthSlider = 7
1269 c = config.edges['COLOR']
1270 GUI.edgeColorPicker = Draw.Create(c[0]/255.0, c[1]/255.0, c[2]/255.0)
1271 GUI.evtEdgeColorPicker = 71
1274 GUI.evtRenderButton = 8
1277 GUI.evtExitButton = 9
1281 # initialize static members
1284 glClear(GL_COLOR_BUFFER_BIT)
1285 glColor3f(0.0, 0.0, 0.0)
1286 glRasterPos2i(10, 350)
1287 Draw.Text("VRM: Vector Rendering Method script.")
1288 glRasterPos2i(10, 335)
1289 Draw.Text("Press Q or ESC to quit.")
1291 # Build the output format menu
1292 glRasterPos2i(10, 310)
1293 Draw.Text("Select the output Format:")
1294 outMenuStruct = "Output Format %t"
1295 for t in outputWriters.keys():
1296 outMenuStruct = outMenuStruct + "|%s" % t
1297 GUI.outFormatMenu = Draw.Menu(outMenuStruct, GUI.evtOutFormatMenu,
1298 10, 285, 160, 18, GUI.outFormatMenu.val, "Choose the Output Format")
1301 GUI.animToggle = Draw.Toggle("Animation", GUI.evtAnimToggle,
1302 10, 260, 160, 18, GUI.animToggle.val,
1303 "Toggle rendering of animations")
1305 # Join Objects toggle
1306 GUI.joinObjsToggle = Draw.Toggle("Join objects", GUI.evtJoinObjsToggle,
1307 10, 235, 160, 18, GUI.joinObjsToggle.val,
1308 "Join objects in the rendered file")
1311 Draw.Button("Render", GUI.evtRenderButton, 10, 210-25, 75, 25+18,
1313 Draw.Button("Exit", GUI.evtExitButton, 95, 210-25, 75, 25+18, "Exit!")
1316 glRasterPos2i(200, 310)
1317 Draw.Text("Rendering Style:")
1320 GUI.polygonsToggle = Draw.Toggle("Filled Polygons", GUI.evtPolygonsToggle,
1321 200, 285, 160, 18, GUI.polygonsToggle.val,
1322 "Render filled polygons")
1324 if GUI.polygonsToggle.val == 1:
1326 # Polygon Shading Style
1327 shadingStyleMenuStruct = "Shading Style %t"
1328 for t in shadingStyles.keys():
1329 shadingStyleMenuStruct = shadingStyleMenuStruct + "|%s" % t.lower()
1330 GUI.shadingStyleMenu = Draw.Menu(shadingStyleMenuStruct, GUI.evtShadingStyleMenu,
1331 200, 260, 160, 18, GUI.shadingStyleMenu.val,
1332 "Choose the shading style")
1336 GUI.showEdgesToggle = Draw.Toggle("Show Edges", GUI.evtShowEdgesToggle,
1337 200, 235, 160, 18, GUI.showEdgesToggle.val,
1338 "Render polygon edges")
1340 if GUI.showEdgesToggle.val == 1:
1343 edgeStyleMenuStruct = "Edge Style %t"
1344 for t in edgeStyles.keys():
1345 edgeStyleMenuStruct = edgeStyleMenuStruct + "|%s" % t.lower()
1346 GUI.edgeStyleMenu = Draw.Menu(edgeStyleMenuStruct, GUI.evtEdgeStyleMenu,
1347 200, 210, 160, 18, GUI.edgeStyleMenu.val,
1348 "Choose the edge style")
1351 GUI.edgeWidthSlider = Draw.Slider("Width: ", GUI.evtEdgeWidthSlider,
1352 200, 185, 140, 18, GUI.edgeWidthSlider.val,
1353 0.0, 10.0, 0, "Change Edge Width")
1356 GUI.edgeColorPicker = Draw.ColorPicker(GUI.evtEdgeColorPicker,
1357 342, 185, 18, 18, GUI.edgeColorPicker.val, "Choose Edge Color")
1360 GUI.showHiddenEdgesToggle = Draw.Toggle("Show Hidden Edges",
1361 GUI.evtShowHiddenEdgesToggle,
1362 200, 160, 160, 18, GUI.showHiddenEdgesToggle.val,
1363 "Render hidden edges as dashed lines")
1365 glRasterPos2i(10, 160)
1366 Draw.Text("Antonio Ospite (c) 2006")
1368 def event(evt, val):
1370 if evt == Draw.ESCKEY or evt == Draw.QKEY:
1377 def button_event(evt):
1379 if evt == GUI.evtExitButton:
1382 elif evt == GUI.evtOutFormatMenu:
1383 i = GUI.outFormatMenu.val - 1
1384 config.output['FORMAT']= outputWriters.keys()[i]
1386 elif evt == GUI.evtAnimToggle:
1387 config.outpur['ANIMATION'] = bool(GUI.animToggle.val)
1389 elif evt == GUI.evtJoinObjsToggle:
1390 config.output['JOIN_OBJECTS'] = bool(GUI.joinObjsToggle.val)
1392 elif evt == GUI.evtPolygonsToggle:
1393 config.polygons['SHOW'] = bool(GUI.polygonsToggle.val)
1395 elif evt == GUI.evtShadingStyleMenu:
1396 i = GUI.shadingStyleMenu.val - 1
1397 config.polygons['SHADING'] = shadingStyles.keys()[i]
1399 elif evt == GUI.evtShowEdgesToggle:
1400 config.edges['SHOW'] = bool(GUI.showEdgesToggle.val)
1402 elif evt == GUI.evtShowHiddenEdgesToggle:
1403 config.edges['SHOW_HIDDEN'] = bool(GUI.showHiddenEdgesToggle.val)
1405 elif evt == GUI.evtEdgeStyleMenu:
1406 i = GUI.edgeStyleMenu.val - 1
1407 config.edges['STYLE'] = edgeStyles.keys()[i]
1409 elif evt == GUI.evtEdgeWidthSlider:
1410 config.edges['WIDTH'] = float(GUI.edgeWidthSlider.val)
1412 elif evt == GUI.evtEdgeColorPicker:
1413 config.edges['COLOR'] = [int(c*255.0) for c in GUI.edgeColorPicker.val]
1415 elif evt == GUI.evtRenderButton:
1416 label = "Save %s" % config.output['FORMAT']
1417 # Show the File Selector
1419 Blender.Window.FileSelector(vectorize, label, outputfile)
1422 print "Event: %d not handled!" % evt
1429 from pprint import pprint
1431 pprint(config.output)
1432 pprint(config.polygons)
1433 pprint(config.edges)
1435 _init = staticmethod(_init)
1436 draw = staticmethod(draw)
1437 event = staticmethod(event)
1438 button_event = staticmethod(button_event)
1439 conf_debug = staticmethod(conf_debug)
1441 # A wrapper function for the vectorizing process
1442 def vectorize(filename):
1443 """The vectorizing process is as follows:
1445 - Instanciate the writer and the renderer
1450 print "\nERROR: invalid file name!"
1453 from Blender import Window
1454 editmode = Window.EditMode()
1455 if editmode: Window.EditMode(0)
1457 actualWriter = outputWriters[config.output['FORMAT']]
1458 writer = actualWriter(filename)
1460 renderer = Renderer()
1461 renderer.doRendering(writer, config.output['ANIMATION'])
1463 if editmode: Window.EditMode(1)
1467 if __name__ == "__main__":
1470 basename = Blender.sys.basename(Blender.Get('filename'))
1472 outputfile = Blender.sys.splitext(basename)[0] + "." + str(config.output['FORMAT']).lower()
1474 if Blender.mode == 'background':
1475 vectorize(outputfile)
1477 Draw.Register(GUI.draw, GUI.event, GUI.button_event)