6 Tooltip: 'Vector Rendering Method script'
9 __author__ = "Antonio Ospite"
10 __url__ = ["http://projects.blender.org/projects/vrm"]
11 __version__ = "0.3.beta"
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 # - FIX the issue with negative scales in object tranformations!
46 # - Use a better depth sorting algorithm
47 # - Implement clipping of primitives and do handle object intersections.
48 # (for now only clipping away whole objects is supported).
49 # - Review how selections are made (this script uses selection states of
50 # primitives to represent visibility infos)
51 # - Use a data structure other than Mesh to represent the 2D image?
52 # Think to a way to merge (adjacent) polygons that have the same color.
53 # Or a way to use paths for silhouettes and contours.
54 # - Consider SMIL for animation handling instead of ECMA Script? (Firefox do
55 # not support SMIL for animations)
56 # - Switch to the Mesh structure, should be considerably faster
57 # (partially done, but with Mesh we cannot sort faces, yet)
58 # - Implement Edge Styles (silhouettes, contours, etc.) (partially done).
59 # - Implement Shading Styles? (partially done, to make more flexible).
60 # - Add Vector Writers other than SVG.
61 # - set the background color!
62 # - Check memory use!!
64 # ---------------------------------------------------------------------
69 # * First release after code restucturing.
70 # Now the script offers a useful set of functionalities
71 # and it can render animations, too.
72 # * Optimization in Renderer.doEdgeStyle(), build a topology cache
73 # so to speed up the lookup of adjacent faces of an edge.
75 # * The SVG output is now SVG 1.0 valid.
76 # Checked with: http://jiggles.w3.org/svgvalidator/ValidatorURI.html
77 # * Progress indicator during HSR.
78 # * Initial SWF output support
79 # * Fixed a bug in the animation code, now the projection matrix is
80 # recalculated at each frame!
82 # ---------------------------------------------------------------------
85 from Blender import Scene, Object, Mesh, NMesh, Material, Lamp, Camera, Window
86 from Blender.Mathutils import *
93 # We use a global progress Indicator Object
97 # Some global settings
101 polygons['SHOW'] = True
102 polygons['SHADING'] = 'FLAT' # FLAT or TOON
103 polygons['HSR'] = 'PAINTER' # PAINTER or NEWELL
104 # Hidden to the user for now
105 polygons['EXPANSION_TRICK'] = True
107 polygons['TOON_LEVELS'] = 2
110 edges['SHOW'] = False
111 edges['SHOW_HIDDEN'] = False
112 edges['STYLE'] = 'MESH' # MESH or SILHOUETTE
114 edges['COLOR'] = [0, 0, 0]
117 output['FORMAT'] = 'SVG'
118 output['ANIMATION'] = False
119 output['JOIN_OBJECTS'] = True
125 def dumpfaces(flist, filename):
126 """Dump a single face to a file.
137 writerobj = SVGVectorWriter(filename)
140 writerobj._printPolygons(m)
146 sys.stderr.write(msg)
149 return (abs(v1[0]-v2[0]) < EPS and
150 abs(v1[1]-v2[1]) < EPS )
151 by_furthest_z = (lambda f1, f2:
152 cmp(max([v.co[2] for v in f1]), max([v.co[2] for v in f2])+EPS)
167 # ---------------------------------------------------------------------
171 # ---------------------------------------------------------------------
177 """A utility class for HSR processing.
180 def is_nonplanar_quad(face):
181 """Determine if a quad is non-planar.
183 From: http://mathworld.wolfram.com/Coplanar.html
185 Geometric objects lying in a common plane are said to be coplanar.
186 Three noncollinear points determine a plane and so are trivially coplanar.
187 Four points are coplanar iff the volume of the tetrahedron defined by them is
193 | x_4 y_4 z_4 1 | == 0
195 Coplanarity is equivalent to the statement that the pair of lines
196 determined by the four points are not skew, and can be equivalently stated
197 in vector form as (x_3-x_1).[(x_2-x_1)x(x_4-x_3)]==0.
199 An arbitrary number of n points x_1, ..., x_n can be tested for
200 coplanarity by finding the point-plane distances of the points
201 x_4, ..., x_n from the plane determined by (x_1,x_2,x_3)
202 and checking if they are all zero.
203 If so, the points are all coplanar.
205 We here check only for 4-point complanarity.
211 print "ERROR a mesh in Blender can't have more than 4 vertices or less than 3"
215 # three points must be complanar
218 x1 = Vector(face[0].co)
219 x2 = Vector(face[1].co)
220 x3 = Vector(face[2].co)
221 x4 = Vector(face[3].co)
223 v = (x3-x1) * CrossVecs((x2-x1), (x4-x3))
229 is_nonplanar_quad = staticmethod(is_nonplanar_quad)
231 def pointInPolygon(poly, v):
234 pointInPolygon = staticmethod(pointInPolygon)
236 def edgeIntersection(s1, s2, do_perturbate=False):
238 (x1, y1) = s1[0].co[0], s1[0].co[1]
239 (x2, y2) = s1[1].co[0], s1[1].co[1]
241 (x3, y3) = s2[0].co[0], s2[0].co[1]
242 (x4, y4) = s2[1].co[0], s2[1].co[1]
250 # calculate delta values (vector components)
259 C = dy2 * dx1 - dx2 * dy1 # /* cross product */
260 if C == 0: #/* parallel */
263 dx3 = x1 - x3 # /* combined origin offset vector */
266 a1 = (dy3 * dx2 - dx3 * dy2) / C;
267 a2 = (dy3 * dx1 - dx3 * dy1) / C;
269 # check for degeneracies
271 #print_debug(str(a1)+"\n")
272 #print_debug(str(a2)+"\n\n")
274 if (a1 == 0 or a1 == 1 or a2 == 0 or a2 == 1):
275 # Intersection on boundaries, we consider the point external?
278 elif (a1>0.0 and a1<1.0 and a2>0.0 and a2<1.0): # /* lines cross */
284 return (NMesh.Vert(x, y, z), a1, a2)
287 # lines have intersections but not those segments
290 edgeIntersection = staticmethod(edgeIntersection)
292 def isVertInside(self, v):
296 # Create point at infinity
297 point_at_infinity = NMesh.Vert(-INF, v.co[1], -INF)
299 for i in range(len(self.v)):
300 s1 = (point_at_infinity, v)
301 s2 = (self.v[i-1], self.v[i])
303 if EQ(v.co, s2[0].co) or EQ(v.co, s2[1].co):
306 if HSR.edgeIntersection(s1, s2, do_perturbate=False):
310 if winding_number % 2 == 0 :
317 isVertInside = staticmethod(isVertInside)
321 return ((b[0] - a[0]) * (c[1] - a[1]) -
322 (b[1] - a[1]) * (c[0] - a[0]) )
324 det = staticmethod(det)
326 def pointInPolygon(q, P):
329 point_at_infinity = NMesh.Vert(-INF, q.co[1], -INF)
333 for i in range(len(P.v)):
336 if (det(q.co, point_at_infinity.co, p0.co)<0) != (det(q.co, point_at_infinity.co, p1.co)<0):
337 if det(p0.co, p1.co, q.co) == 0 :
340 elif (det(p0.co, p1.co, q.co)<0) != (det(p0.co, p1.co, point_at_infinity.co)<0):
345 pointInPolygon = staticmethod(pointInPolygon)
347 def projectionsOverlap(f1, f2):
348 """ If you have nonconvex, but still simple polygons, an acceptable method
349 is to iterate over all vertices and perform the Point-in-polygon test[1].
350 The advantage of this method is that you can compute the exact
351 intersection point and collision normal that you will need to simulate
352 collision. When you have the point that lies inside the other polygon, you
353 just iterate over all edges of the second polygon again and look for edge
354 intersections. Note that this method detects collsion when it already
355 happens. This algorithm is fast enough to perform it hundreds of times per
358 for i in range(len(f1.v)):
361 # If a point of f1 in inside f2, there is an overlap!
363 #if HSR.isVertInside(f2, v1):
364 if HSR.pointInPolygon(v1, f2):
367 # If not the polygon can be ovelap as well, so we check for
368 # intersection between an edge of f1 and all the edges of f2
372 for j in range(len(f2.v)):
379 intrs = HSR.edgeIntersection(e1, e2)
381 #print_debug(str(v0.co) + " " + str(v1.co) + " " +
382 # str(v2.co) + " " + str(v3.co) )
383 #print_debug("\nIntersection\n")
389 projectionsOverlap = staticmethod(projectionsOverlap)
391 def midpoint(p1, p2):
392 """Return the midpoint of two vertices.
394 m = MidpointVecs(Vector(p1), Vector(p2))
395 mv = NMesh.Vert(m[0], m[1], m[2])
399 midpoint = staticmethod(midpoint)
401 def facesplit(P, Q, facelist, nmesh):
402 """Split P or Q according to the strategy illustrated in the Newell's
406 by_furthest_z = (lambda f1, f2:
407 cmp(max([v.co[2] for v in f1]), max([v.co[2] for v in f2])+EPS)
410 # Choose if split P on Q plane or vice-versa
414 d = HSR.Distance(Vector(Pi), Q)
417 pIntersectQ = (n != len(P))
421 d = HSR.Distance(Vector(Qi), P)
424 qIntersectP = (n != len(Q))
428 # 1. If parts of P lie in both half-spaces of Q
429 # then splice P in two with the plane of Q
435 newfaces = HSR.splitOn(plane, f)
437 # 2. Else if parts of Q lie in both half-space of P
438 # then splice Q in two with the plane of P
439 if qIntersectP and newfaces == None:
444 newfaces = HSR.splitOn(plane, f)
447 # 3. Else slice P in half through the mid-point of
448 # the longest pair of opposite sides
451 print "We ignore P..."
458 # v1 = midpoint(f[0], f[1])
459 # v2 = midpoint(f[1], f[2])
461 # v1 = midpoint(f[0], f[1])
462 # v2 = midpoint(f[2], f[3])
463 #vec3 = (Vector(v2)+10*Vector(f.normal))
465 #v3 = NMesh.Vert(vec3[0], vec3[1], vec3[2])
467 #plane = NMesh.Face([v1, v2, v3])
469 #newfaces = splitOn(plane, f)
473 print "Big FAT problem, we weren't able to split POLYGONS!"
479 # if v not in plane and v in nmesh.verts:
480 # nmesh.verts.remove(v)
485 nf.col = [f.col[0]] * len(nf.v)
490 nmesh.verts.append(v)
491 # insert pieces in the list
496 # and resort the faces
497 facelist.sort(by_furthest_z)
498 facelist.sort(lambda f1, f2: cmp(f1.smooth, f2.smooth))
501 #print [ f.smooth for f in facelist ]
505 facesplit = staticmethod(facesplit)
507 def isOnSegment(v1, v2, p, extremes_internal=False):
508 """Check if point p is in segment v1v2.
514 # Should we consider extreme points as internal ?
516 # if p == v1 or p == v2:
517 if l1 < EPS or l2 < EPS:
518 return extremes_internal
522 # if the sum of l1 and l2 is circa l, then the point is on segment,
523 if abs(l - (l1+l2)) < EPS:
528 isOnSegment = staticmethod(isOnSegment)
530 def Distance(point, face):
531 """ Calculate the distance between a point and a face.
533 An alternative but more expensive method can be:
535 ip = Intersect(Vector(face[0]), Vector(face[1]), Vector(face[2]),
536 Vector(face.no), Vector(point), 0)
538 d = Vector(ip - point).length
540 See: http://mathworld.wolfram.com/Point-PlaneDistance.html
544 plNormal = Vector(face.no)
545 plVert0 = Vector(face.v[0])
547 d = (plVert0 * plNormal) - (p * plNormal)
549 #d = plNormal * (plVert0 - p)
551 #print "\nd: %.10f - sel: %d, %s\n" % (d, face.sel, str(point))
555 Distance = staticmethod(Distance)
559 # make one or two new faces based on a list of vertex-indices
588 makeFaces = staticmethod(makeFaces)
591 """Split P using the plane of Q.
592 Logic taken from the knife.py python script
595 # Check if P and Q are parallel
596 u = CrossVecs(Vector(Q.no),Vector(P.no))
602 print "PARALLEL planes!!"
606 # The final aim is to find the intersection line between P
607 # and the plane of Q, and split P along this line
611 # Calculate point-plane Distance between vertices of P and plane Q
613 for i in range(0, nP):
614 d.append(HSR.Distance(P.v[i], Q))
627 #print "d0:", d0, "d1:", d1
629 # if the vertex lies in the cutplane
631 #print "d1 On cutplane"
632 posVertList.append(V1)
633 negVertList.append(V1)
635 # if the previous vertex lies in cutplane
637 #print "d0 on Cutplane"
639 #print "d1 on positive Halfspace"
640 posVertList.append(V1)
642 #print "d1 on negative Halfspace"
643 negVertList.append(V1)
645 # if they are on the same side of the plane
647 #print "On the same half-space"
649 #print "d1 on positive Halfspace"
650 posVertList.append(V1)
652 #print "d1 on negative Halfspace"
653 negVertList.append(V1)
655 # the vertices are not on the same side of the plane, so we have an intersection
657 #print "Intersection"
659 e = Vector(V0), Vector(V1)
660 tri = Vector(Q[0]), Vector(Q[1]), Vector(Q[2])
662 inters = Intersect(tri[0], tri[1], tri[2], e[1]-e[0], e[0], 0)
667 #print "Intersection", inters
669 nv = NMesh.Vert(inters[0], inters[1], inters[2])
670 newVertList.append(nv)
672 posVertList.append(nv)
673 negVertList.append(nv)
676 posVertList.append(V1)
678 negVertList.append(V1)
682 posVertList = [ u for u in posVertList if u not in locals()['_[1]'] ]
683 negVertList = [ u for u in negVertList if u not in locals()['_[1]'] ]
686 # If vertex are all on the same half-space, return
687 #if len(posVertList) < 3:
688 # print "Problem, we created a face with less that 3 verteices??"
690 #if len(negVertList) < 3:
691 # print "Problem, we created a face with less that 3 verteices??"
694 if len(posVertList) < 3 or len(negVertList) < 3:
695 print "RETURN NONE, SURE???"
699 newfaces = HSR.addNewFaces(posVertList, negVertList)
703 splitOn = staticmethod(splitOn)
705 def addNewFaces(posVertList, negVertList):
706 # Create new faces resulting from the split
708 if len(posVertList) or len(negVertList):
710 #newfaces = [posVertList] + [negVertList]
711 newfaces = ( [[ NMesh.Vert(v[0], v[1], v[2]) for v in posVertList]] +
712 [[ NMesh.Vert(v[0], v[1], v[2]) for v in negVertList]] )
716 outfaces += HSR.makeFaces(nf)
721 addNewFaces = staticmethod(addNewFaces)
724 # ---------------------------------------------------------------------
726 ## Mesh Utility class
728 # ---------------------------------------------------------------------
732 def buildEdgeFaceUsersCache(me):
734 Takes a mesh and returns a list aligned with the meshes edges.
735 Each item is a list of the faces that use the edge
736 would be the equiv for having ed.face_users as a property
738 Taken from .blender/scripts/bpymodules/BPyMesh.py,
739 thanks to ideasman_42.
742 def sorted_edge_indicies(ed):
750 face_edges_dict= dict([(sorted_edge_indicies(ed), (ed.index, [])) for ed in me.edges])
752 fvi= [v.index for v in f.v]# face vert idx's
753 for i in xrange(len(f)):
760 face_edges_dict[i1,i2][1].append(f)
762 face_edges= [None] * len(me.edges)
763 for ed_index, ed_faces in face_edges_dict.itervalues():
764 face_edges[ed_index]= ed_faces
768 def isMeshEdge(adjacent_faces):
771 A mesh edge is visible if _at_least_one_ of its adjacent faces is selected.
772 Note: if the edge has no adjacent faces we want to show it as well,
773 useful for "edge only" portion of objects.
776 if len(adjacent_faces) == 0:
779 selected_faces = [f for f in adjacent_faces if f.sel]
781 if len(selected_faces) != 0:
786 def isSilhouetteEdge(adjacent_faces):
787 """Silhuette selection rule.
789 An edge is a silhuette edge if it is shared by two faces with
790 different selection status or if it is a boundary edge of a selected
794 if ((len(adjacent_faces) == 1 and adjacent_faces[0].sel == 1) or
795 (len(adjacent_faces) == 2 and
796 adjacent_faces[0].sel != adjacent_faces[1].sel)
802 buildEdgeFaceUsersCache = staticmethod(buildEdgeFaceUsersCache)
803 isMeshEdge = staticmethod(isMeshEdge)
804 isSilhouetteEdge = staticmethod(isSilhouetteEdge)
807 # ---------------------------------------------------------------------
809 ## Shading Utility class
811 # ---------------------------------------------------------------------
817 def toonShadingMapSetup():
818 levels = config.polygons['TOON_LEVELS']
820 texels = 2*levels - 1
821 tmp_shademap = [0.0] + [(i)/float(texels-1) for i in xrange(1, texels-1) ] + [1.0]
827 shademap = ShadingUtils.shademap
830 shademap = ShadingUtils.toonShadingMapSetup()
833 for i in xrange(0, len(shademap)-1):
834 pivot = (shademap[i]+shademap[i+1])/2.0
839 if v < shademap[i+1]:
844 toonShadingMapSetup = staticmethod(toonShadingMapSetup)
845 toonShading = staticmethod(toonShading)
848 # ---------------------------------------------------------------------
850 ## Projections classes
852 # ---------------------------------------------------------------------
855 """Calculate the projection of an object given the camera.
857 A projector is useful to so some per-object transformation to obtain the
858 projection of an object given the camera.
860 The main method is #doProjection# see the method description for the
864 def __init__(self, cameraObj, canvasRatio):
865 """Calculate the projection matrix.
867 The projection matrix depends, in this case, on the camera settings.
868 TAKE CARE: This projector expects vertices in World Coordinates!
871 camera = cameraObj.getData()
873 aspect = float(canvasRatio[0])/float(canvasRatio[1])
874 near = camera.clipStart
877 scale = float(camera.scale)
879 fovy = atan(0.5/aspect/(camera.lens/32))
880 fovy = fovy * 360.0/pi
882 # What projection do we want?
884 mP = self._calcPerspectiveMatrix(fovy, aspect, near, far)
885 elif camera.type == 1:
886 mP = self._calcOrthoMatrix(fovy, aspect, near, far, scale)
888 # View transformation
889 cam = Matrix(cameraObj.getInverseMatrix())
894 self.projectionMatrix = mP
900 def doProjection(self, v):
901 """Project the point on the view plane.
903 Given a vertex calculate the projection using the current projection
907 # Note that we have to work on the vertex using homogeneous coordinates
908 # From blender 2.42+ we don't need to resize the vector to be 4d
909 # when applying a 4x4 matrix, but we do that anyway since we need the
910 # 4th coordinate later
911 p = self.projectionMatrix * Vector(v).resize4D()
913 # Perspective division
930 def _calcPerspectiveMatrix(self, fovy, aspect, near, far):
931 """Return a perspective projection matrix.
934 top = near * tan(fovy * pi / 360.0)
938 x = (2.0 * near) / (right-left)
939 y = (2.0 * near) / (top-bottom)
940 a = (right+left) / (right-left)
941 b = (top+bottom) / (top - bottom)
942 c = - ((far+near) / (far-near))
943 d = - ((2*far*near)/(far-near))
949 [0.0, 0.0, -1.0, 0.0])
953 def _calcOrthoMatrix(self, fovy, aspect , near, far, scale):
954 """Return an orthogonal projection matrix.
957 # The 11 in the formula was found emiprically
958 top = near * tan(fovy * pi / 360.0) * (scale * 11)
960 left = bottom * aspect
965 tx = -((right+left)/rl)
966 ty = -((top+bottom)/tb)
970 [2.0/rl, 0.0, 0.0, tx],
971 [0.0, 2.0/tb, 0.0, ty],
972 [0.0, 0.0, 2.0/fn, tz],
973 [0.0, 0.0, 0.0, 1.0])
978 # ---------------------------------------------------------------------
980 ## Progress Indicator
982 # ---------------------------------------------------------------------
985 """A model for a progress indicator.
987 Do the progress calculation calculation and
988 the view independent stuff of a progress indicator.
990 def __init__(self, steps=0):
996 def setSteps(self, steps):
997 """Set the number of steps of the activity wich we want to track.
1004 def setName(self, name):
1005 """Set the name of the activity wich we want to track.
1012 def getProgress(self):
1013 return self.progress
1020 """Update the model, call this method when one step is completed.
1022 if self.progress == 100:
1026 self.progress = ( float(self.completed) / float(self.steps) ) * 100
1027 self.progress = int(self.progress)
1032 class ProgressIndicator:
1033 """An abstraction of a View for the Progress Model
1037 # Use a refresh rate so we do not show the progress at
1038 # every update, but every 'self.refresh_rate' times.
1039 self.refresh_rate = 10
1040 self.shows_counter = 0
1044 self.progressModel = None
1046 def setQuiet(self, value):
1049 def setActivity(self, name, steps):
1050 """Initialize the Model.
1052 In a future version (with subactivities-progress support) this method
1053 could only set the current activity.
1055 self.progressModel = Progress()
1056 self.progressModel.setName(name)
1057 self.progressModel.setSteps(steps)
1059 def getActivity(self):
1060 return self.progressModel
1063 """Update the model and show the actual progress.
1065 assert(self.progressModel)
1067 if self.progressModel.update():
1071 self.show(self.progressModel.getProgress(),
1072 self.progressModel.getName())
1074 # We return always True here so we can call the update() method also
1075 # from lambda funcs (putting the call in logical AND with other ops)
1078 def show(self, progress, name=""):
1079 self.shows_counter = (self.shows_counter + 1) % self.refresh_rate
1080 if self.shows_counter != 0:
1084 self.shows_counter = -1
1087 class ConsoleProgressIndicator(ProgressIndicator):
1088 """Show a progress bar on stderr, a la wget.
1091 ProgressIndicator.__init__(self)
1093 self.swirl_chars = ["-", "\\", "|", "/"]
1094 self.swirl_count = -1
1096 def show(self, progress, name):
1097 ProgressIndicator.show(self, progress, name)
1100 bar_progress = int( (progress/100.0) * bar_length )
1101 bar = ("=" * bar_progress).ljust(bar_length)
1103 self.swirl_count = (self.swirl_count+1)%len(self.swirl_chars)
1104 swirl_char = self.swirl_chars[self.swirl_count]
1106 progress_bar = "%s |%s| %c %3d%%" % (name, bar, swirl_char, progress)
1108 sys.stderr.write(progress_bar+"\r")
1110 sys.stderr.write("\n")
1113 class GraphicalProgressIndicator(ProgressIndicator):
1114 """Interface to the Blender.Window.DrawProgressBar() method.
1117 ProgressIndicator.__init__(self)
1119 #self.swirl_chars = ["-", "\\", "|", "/"]
1120 # We have to use letters with the same width, for now!
1121 # Blender progress bar considers the font widths when
1122 # calculating the progress bar width.
1123 self.swirl_chars = ["\\", "/"]
1124 self.swirl_count = -1
1126 def show(self, progress, name):
1127 ProgressIndicator.show(self, progress)
1129 self.swirl_count = (self.swirl_count+1)%len(self.swirl_chars)
1130 swirl_char = self.swirl_chars[self.swirl_count]
1132 progress_text = "%s - %c %3d%%" % (name, swirl_char, progress)
1134 # Finally draw the Progress Bar
1135 Window.WaitCursor(1) # Maybe we can move that call in the constructor?
1136 Window.DrawProgressBar(progress/100.0, progress_text)
1139 Window.DrawProgressBar(1, progress_text)
1140 Window.WaitCursor(0)
1144 # ---------------------------------------------------------------------
1146 ## 2D Object representation class
1148 # ---------------------------------------------------------------------
1150 # TODO: a class to represent the needed properties of a 2D vector image
1151 # For now just using a [N]Mesh structure.
1154 # ---------------------------------------------------------------------
1156 ## Vector Drawing Classes
1158 # ---------------------------------------------------------------------
1164 A class for printing output in a vectorial format.
1166 Given a 2D representation of the 3D scene the class is responsible to
1167 write it is a vector format.
1169 Every subclasses of VectorWriter must have at last the following public
1173 - printCanvas(self, scene,
1174 doPrintPolygons=True, doPrintEdges=False, showHiddenEdges=False):
1177 def __init__(self, fileName):
1178 """Set the output file name and other properties"""
1180 self.outputFileName = fileName
1183 context = Scene.GetCurrent().getRenderingContext()
1184 self.canvasSize = ( context.imageSizeX(), context.imageSizeY() )
1188 self.animation = False
1195 def open(self, startFrame=1, endFrame=1):
1196 if startFrame != endFrame:
1197 self.startFrame = startFrame
1198 self.endFrame = endFrame
1199 self.animation = True
1201 self.file = open(self.outputFileName, "w")
1202 print "Outputting to: ", self.outputFileName
1211 def printCanvas(self, scene, doPrintPolygons=True, doPrintEdges=False,
1212 showHiddenEdges=False):
1213 """This is the interface for the needed printing routine.
1220 class SVGVectorWriter(VectorWriter):
1221 """A concrete class for writing SVG output.
1224 def __init__(self, fileName):
1225 """Simply call the parent Contructor.
1227 VectorWriter.__init__(self, fileName)
1234 def open(self, startFrame=1, endFrame=1):
1235 """Do some initialization operations.
1237 VectorWriter.open(self, startFrame, endFrame)
1241 """Do some finalization operation.
1245 # remember to call the close method of the parent
1246 VectorWriter.close(self)
1249 def printCanvas(self, scene, doPrintPolygons=True, doPrintEdges=False,
1250 showHiddenEdges=False):
1251 """Convert the scene representation to SVG.
1254 Objects = scene.getChildren()
1256 context = scene.getRenderingContext()
1257 framenumber = context.currentFrame()
1260 framestyle = "display:none"
1262 framestyle = "display:block"
1264 # Assign an id to this group so we can set properties on it using DOM
1265 self.file.write("<g id=\"frame%d\" style=\"%s\">\n" %
1266 (framenumber, framestyle) )
1271 if(obj.getType() != 'Mesh'):
1274 self.file.write("<g id=\"%s\">\n" % obj.getName())
1276 mesh = obj.getData(mesh=1)
1279 self._printPolygons(mesh)
1282 self._printEdges(mesh, showHiddenEdges)
1284 self.file.write("</g>\n")
1286 self.file.write("</g>\n")
1293 def _calcCanvasCoord(self, v):
1294 """Convert vertex in scene coordinates to canvas coordinates.
1297 pt = Vector([0, 0, 0])
1299 mW = float(self.canvasSize[0])/2.0
1300 mH = float(self.canvasSize[1])/2.0
1302 # rescale to canvas size
1303 pt[0] = v.co[0]*mW + mW
1304 pt[1] = v.co[1]*mH + mH
1307 # For now we want (0,0) in the top-left corner of the canvas.
1308 # Mirror and translate along y
1310 pt[1] += self.canvasSize[1]
1314 def _printHeader(self):
1315 """Print SVG header."""
1317 self.file.write("<?xml version=\"1.0\"?>\n")
1318 self.file.write("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\"\n")
1319 self.file.write("\t\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n")
1320 self.file.write("<svg version=\"1.0\"\n")
1321 self.file.write("\txmlns=\"http://www.w3.org/2000/svg\"\n")
1322 self.file.write("\twidth=\"%d\" height=\"%d\">\n\n" %
1327 self.file.write("""\n<script type="text/javascript"><![CDATA[
1328 globalStartFrame=%d;
1331 /* FIXME: Use 1000 as interval as lower values gives problems */
1332 timerID = setInterval("NextFrame()", 1000);
1333 globalFrameCounter=%d;
1335 function NextFrame()
1337 currentElement = document.getElementById('frame'+globalFrameCounter)
1338 previousElement = document.getElementById('frame'+(globalFrameCounter-1))
1340 if (!currentElement)
1345 if (globalFrameCounter > globalEndFrame)
1347 clearInterval(timerID)
1353 previousElement.style.display="none";
1355 currentElement.style.display="block";
1356 globalFrameCounter++;
1360 \n""" % (self.startFrame, self.endFrame, self.startFrame) )
1362 def _printFooter(self):
1363 """Print the SVG footer."""
1365 self.file.write("\n</svg>\n")
1367 def _printPolygons(self, mesh):
1368 """Print the selected (visible) polygons.
1371 if len(mesh.faces) == 0:
1374 self.file.write("<g>\n")
1376 for face in mesh.faces:
1380 self.file.write("<path d=\"")
1382 #p = self._calcCanvasCoord(face.verts[0])
1383 p = self._calcCanvasCoord(face.v[0])
1384 self.file.write("M %g,%g L " % (p[0], p[1]))
1386 for v in face.v[1:]:
1387 p = self._calcCanvasCoord(v)
1388 self.file.write("%g,%g " % (p[0], p[1]))
1390 # get rid of the last blank space, just cosmetics here.
1391 self.file.seek(-1, 1)
1392 self.file.write(" z\"\n")
1394 # take as face color the first vertex color
1397 color = [fcol.r, fcol.g, fcol.b, fcol.a]
1399 color = [255, 255, 255, 255]
1401 # Convert the color to the #RRGGBB form
1402 str_col = "#%02X%02X%02X" % (color[0], color[1], color[2])
1404 # Handle transparent polygons
1407 opacity = float(color[3])/255.0
1408 opacity_string = " fill-opacity: %g; stroke-opacity: %g; opacity: 1;" % (opacity, opacity)
1409 #opacity_string = "opacity: %g;" % (opacity)
1411 self.file.write("\tstyle=\"fill:" + str_col + ";")
1412 self.file.write(opacity_string)
1414 # use the stroke property to alleviate the "adjacent edges" problem,
1415 # we simulate polygon expansion using borders,
1416 # see http://www.antigrain.com/svg/index.html for more info
1419 # EXPANSION TRICK is not that useful where there is transparency
1420 if config.polygons['EXPANSION_TRICK'] and color[3] == 255:
1421 # str_col = "#000000" # For debug
1422 self.file.write(" stroke:%s;\n" % str_col)
1423 self.file.write(" stroke-width:" + str(stroke_width) + ";\n")
1424 self.file.write(" stroke-linecap:round;stroke-linejoin:round")
1426 self.file.write("\"/>\n")
1428 self.file.write("</g>\n")
1430 def _printEdges(self, mesh, showHiddenEdges=False):
1431 """Print the wireframe using mesh edges.
1434 stroke_width = config.edges['WIDTH']
1435 stroke_col = config.edges['COLOR']
1437 self.file.write("<g>\n")
1439 for e in mesh.edges:
1441 hidden_stroke_style = ""
1444 if showHiddenEdges == False:
1447 hidden_stroke_style = ";\n stroke-dasharray:3, 3"
1449 p1 = self._calcCanvasCoord(e.v1)
1450 p2 = self._calcCanvasCoord(e.v2)
1452 self.file.write("<line x1=\"%g\" y1=\"%g\" x2=\"%g\" y2=\"%g\"\n"
1453 % ( p1[0], p1[1], p2[0], p2[1] ) )
1454 self.file.write(" style=\"stroke:rgb("+str(stroke_col[0])+","+str(stroke_col[1])+","+str(stroke_col[2])+");")
1455 self.file.write(" stroke-width:"+str(stroke_width)+";\n")
1456 self.file.write(" stroke-linecap:round;stroke-linejoin:round")
1457 self.file.write(hidden_stroke_style)
1458 self.file.write("\"/>\n")
1460 self.file.write("</g>\n")
1469 SWFSupported = False
1471 class SWFVectorWriter(VectorWriter):
1472 """A concrete class for writing SWF output.
1475 def __init__(self, fileName):
1476 """Simply call the parent Contructor.
1478 VectorWriter.__init__(self, fileName)
1488 def open(self, startFrame=1, endFrame=1):
1489 """Do some initialization operations.
1491 VectorWriter.open(self, startFrame, endFrame)
1492 self.movie = SWFMovie()
1493 self.movie.setDimension(self.canvasSize[0], self.canvasSize[1])
1495 self.movie.setRate(25)
1496 numframes = endFrame - startFrame + 1
1497 self.movie.setFrames(numframes)
1500 """Do some finalization operation.
1502 self.movie.save(self.outputFileName)
1504 # remember to call the close method of the parent
1505 VectorWriter.close(self)
1507 def printCanvas(self, scene, doPrintPolygons=True, doPrintEdges=False,
1508 showHiddenEdges=False):
1509 """Convert the scene representation to SVG.
1511 context = scene.getRenderingContext()
1512 framenumber = context.currentFrame()
1514 Objects = scene.getChildren()
1517 self.movie.remove(self.sprite)
1519 sprite = SWFSprite()
1523 if(obj.getType() != 'Mesh'):
1526 mesh = obj.getData(mesh=1)
1529 self._printPolygons(mesh, sprite)
1532 self._printEdges(mesh, sprite, showHiddenEdges)
1535 i = self.movie.add(sprite)
1536 # Remove the instance the next time
1539 self.movie.nextFrame()
1546 def _calcCanvasCoord(self, v):
1547 """Convert vertex in scene coordinates to canvas coordinates.
1550 pt = Vector([0, 0, 0])
1552 mW = float(self.canvasSize[0])/2.0
1553 mH = float(self.canvasSize[1])/2.0
1555 # rescale to canvas size
1556 pt[0] = v.co[0]*mW + mW
1557 pt[1] = v.co[1]*mH + mH
1560 # For now we want (0,0) in the top-left corner of the canvas.
1561 # Mirror and translate along y
1563 pt[1] += self.canvasSize[1]
1567 def _printPolygons(self, mesh, sprite):
1568 """Print the selected (visible) polygons.
1571 if len(mesh.faces) == 0:
1574 for face in mesh.faces:
1580 color = [fcol.r, fcol.g, fcol.b, fcol.a]
1582 color = [255, 255, 255, 255]
1585 f = s.addFill(color[0], color[1], color[2], color[3])
1588 # The starting point of the shape
1589 p0 = self._calcCanvasCoord(face.verts[0])
1590 s.movePenTo(p0[0], p0[1])
1593 for v in face.verts[1:]:
1594 p = self._calcCanvasCoord(v)
1595 s.drawLineTo(p[0], p[1])
1598 s.drawLineTo(p0[0], p0[1])
1604 # use the stroke property to alleviate the "adjacent edges" problem,
1605 # we simulate polygon expansion using borders,
1606 # see http://www.antigrain.com/svg/index.html for more info
1609 # EXPANSION TRICK is not that useful where there is transparency
1610 if config.polygons['EXPANSION_TRICK'] and color[3] == 255:
1611 # str_col = "#000000" # For debug
1612 self.file.write(" stroke:%s;\n" % str_col)
1613 self.file.write(" stroke-width:" + str(stroke_width) + ";\n")
1614 self.file.write(" stroke-linecap:round;stroke-linejoin:round")
1618 def _printEdges(self, mesh, sprite, showHiddenEdges=False):
1619 """Print the wireframe using mesh edges.
1622 stroke_width = config.edges['WIDTH']
1623 stroke_col = config.edges['COLOR']
1627 for e in mesh.edges:
1629 #Next, we set the line width and color for our shape.
1630 s.setLine(stroke_width, stroke_col[0], stroke_col[1], stroke_col[2],
1634 if showHiddenEdges == False:
1637 # SWF does not support dashed lines natively, so -for now-
1638 # draw hidden lines thinner and half-trasparent
1639 s.setLine(stroke_width/2, stroke_col[0], stroke_col[1],
1642 p1 = self._calcCanvasCoord(e.v1)
1643 p2 = self._calcCanvasCoord(e.v2)
1645 # FIXME: this is just a qorkaround, remove that after the
1646 # implementation of propoer Viewport clipping
1647 if abs(p1[0]) < 3000 and abs(p2[0]) < 3000 and abs(p1[1]) < 3000 and abs(p1[2]) < 3000:
1648 s.movePenTo(p1[0], p1[1])
1649 s.drawLineTo(p2[0], p2[1])
1657 # ---------------------------------------------------------------------
1659 ## Rendering Classes
1661 # ---------------------------------------------------------------------
1663 # A dictionary to collect different shading style methods
1664 shadingStyles = dict()
1665 shadingStyles['FLAT'] = None
1666 shadingStyles['TOON'] = None
1668 # A dictionary to collect different edge style methods
1670 edgeStyles['MESH'] = MeshUtils.isMeshEdge
1671 edgeStyles['SILHOUETTE'] = MeshUtils.isSilhouetteEdge
1673 # A dictionary to collect the supported output formats
1674 outputWriters = dict()
1675 outputWriters['SVG'] = SVGVectorWriter
1677 outputWriters['SWF'] = SWFVectorWriter
1681 """Render a scene viewed from the active camera.
1683 This class is responsible of the rendering process, transformation and
1684 projection of the objects in the scene are invoked by the renderer.
1686 The rendering is done using the active camera for the current scene.
1690 """Make the rendering process only for the current scene by default.
1692 We will work on a copy of the scene, to be sure that the current scene do
1693 not get modified in any way.
1696 # Render the current Scene, this should be a READ-ONLY property
1697 self._SCENE = Scene.GetCurrent()
1699 # Use the aspect ratio of the scene rendering context
1700 context = self._SCENE.getRenderingContext()
1702 aspect_ratio = float(context.imageSizeX())/float(context.imageSizeY())
1703 self.canvasRatio = (float(context.aspectRatioX())*aspect_ratio,
1704 float(context.aspectRatioY())
1707 # Render from the currently active camera
1708 self.cameraObj = self._SCENE.getCurrentCamera()
1710 # Get the list of lighting sources
1711 obj_lst = self._SCENE.getChildren()
1712 self.lights = [ o for o in obj_lst if o.getType() == 'Lamp']
1714 # When there are no lights we use a default lighting source
1715 # that have the same position of the camera
1716 if len(self.lights) == 0:
1717 l = Lamp.New('Lamp')
1718 lobj = Object.New('Lamp')
1719 lobj.loc = self.cameraObj.loc
1721 self.lights.append(lobj)
1728 def doRendering(self, outputWriter, animation=False):
1729 """Render picture or animation and write it out.
1732 - a Vector writer object that will be used to output the result.
1733 - a flag to tell if we want to render an animation or only the
1737 context = self._SCENE.getRenderingContext()
1738 origCurrentFrame = context.currentFrame()
1740 # Handle the animation case
1742 startFrame = origCurrentFrame
1743 endFrame = startFrame
1746 startFrame = context.startFrame()
1747 endFrame = context.endFrame()
1748 outputWriter.open(startFrame, endFrame)
1750 # Do the rendering process frame by frame
1751 print "Start Rendering of %d frames" % (endFrame-startFrame+1)
1752 for f in xrange(startFrame, endFrame+1):
1753 print "\n\nFrame: %d" % f
1754 context.currentFrame(f)
1756 # Use some temporary workspace, a full copy of the scene
1757 inputScene = self._SCENE.copy(2)
1758 # And Set our camera accordingly
1759 self.cameraObj = inputScene.getCurrentCamera()
1761 # Get a projector for this camera.
1762 # NOTE: the projector wants object in world coordinates,
1763 # so we should remember to apply modelview transformations
1764 # _before_ we do projection transformations.
1765 self.proj = Projector(self.cameraObj, self.canvasRatio)
1768 renderedScene = self.doRenderScene(inputScene)
1770 print "There was an error! Aborting."
1772 print traceback.print_exc()
1774 self._SCENE.makeCurrent()
1775 Scene.unlink(inputScene)
1779 outputWriter.printCanvas(renderedScene,
1780 doPrintPolygons = config.polygons['SHOW'],
1781 doPrintEdges = config.edges['SHOW'],
1782 showHiddenEdges = config.edges['SHOW_HIDDEN'])
1784 # delete the rendered scene
1785 self._SCENE.makeCurrent()
1786 Scene.unlink(renderedScene)
1789 outputWriter.close()
1791 context.currentFrame(origCurrentFrame)
1794 def doRenderScene(self, workScene):
1795 """Control the rendering process.
1797 Here we control the entire rendering process invoking the operation
1798 needed to transform and project the 3D scene in two dimensions.
1801 # global processing of the scene
1803 self._doSceneClipping(workScene)
1805 self._doConvertGeometricObjsToMesh(workScene)
1807 if config.output['JOIN_OBJECTS']:
1808 self._joinMeshObjectsInScene(workScene)
1810 self._doSceneDepthSorting(workScene)
1812 # Per object activities
1814 Objects = workScene.getChildren()
1815 print "Total Objects: %d" % len(Objects)
1816 for i,obj in enumerate(Objects):
1818 print "Rendering Object: %d" % i
1820 if obj.getType() != 'Mesh':
1821 print "Only Mesh supported! - Skipping type:", obj.getType()
1824 print "Rendering: ", obj.getName()
1826 mesh = obj.getData(mesh=1)
1828 self._doModelingTransformation(mesh, obj.matrix)
1830 self._doBackFaceCulling(mesh)
1833 # When doing HSR with NEWELL we may want to flip all normals
1835 if config.polygons['HSR'] == "NEWELL":
1836 for f in mesh.faces:
1839 for f in mesh.faces:
1842 self._doLighting(mesh)
1844 # Do "projection" now so we perform further processing
1845 # in Normalized View Coordinates
1846 self._doProjection(mesh, self.proj)
1848 self._doViewFrustumClipping(mesh)
1850 self._doHiddenSurfaceRemoval(mesh)
1852 self._doEdgesStyle(mesh, edgeStyles[config.edges['STYLE']])
1854 # Update the object data, important! :)
1866 def _getObjPosition(self, obj):
1867 """Return the obj position in World coordinates.
1869 return obj.matrix.translationPart()
1871 def _cameraViewVector(self):
1872 """Get the View Direction form the camera matrix.
1874 return Vector(self.cameraObj.matrix[2]).resize3D()
1879 def _isFaceVisible(self, face):
1880 """Determine if a face of an object is visible from the current camera.
1882 The view vector is calculated from the camera location and one of the
1883 vertices of the face (expressed in World coordinates, after applying
1884 modelview transformations).
1886 After those transformations we determine if a face is visible by
1887 computing the angle between the face normal and the view vector, this
1888 angle has to be between -90 and 90 degrees for the face to be visible.
1889 This corresponds somehow to the dot product between the two, if it
1890 results > 0 then the face is visible.
1892 There is no need to normalize those vectors since we are only interested in
1893 the sign of the cross product and not in the product value.
1895 NOTE: here we assume the face vertices are in WorldCoordinates, so
1896 please transform the object _before_ doing the test.
1899 normal = Vector(face.no)
1900 camPos = self._getObjPosition(self.cameraObj)
1903 # View Vector in orthographics projections is the view Direction of
1905 if self.cameraObj.data.getType() == 1:
1906 view_vect = self._cameraViewVector()
1908 # View vector in perspective projections can be considered as
1909 # the difference between the camera position and one point of
1910 # the face, we choose the farthest point from the camera.
1911 if self.cameraObj.data.getType() == 0:
1912 vv = max( [ ((camPos - Vector(v.co)).length, (camPos - Vector(v.co))) for v in face] )
1916 # if d > 0 the face is visible from the camera
1917 d = view_vect * normal
1927 def _doSceneClipping(self, scene):
1928 """Clip whole objects against the View Frustum.
1930 For now clip away only objects according to their center position.
1933 cpos = self._getObjPosition(self.cameraObj)
1934 view_vect = self._cameraViewVector()
1936 near = self.cameraObj.data.clipStart
1937 far = self.cameraObj.data.clipEnd
1939 aspect = float(self.canvasRatio[0])/float(self.canvasRatio[1])
1940 fovy = atan(0.5/aspect/(self.cameraObj.data.lens/32))
1941 fovy = fovy * 360.0/pi
1943 Objects = scene.getChildren()
1945 if o.getType() != 'Mesh': continue;
1947 obj_vect = Vector(cpos) - self._getObjPosition(o)
1949 d = obj_vect*view_vect
1950 theta = AngleBetweenVecs(obj_vect, view_vect)
1952 # if the object is outside the view frustum, clip it away
1953 if (d < near) or (d > far) or (theta > fovy):
1956 def _doConvertGeometricObjsToMesh(self, scene):
1957 """Convert all "geometric" objects to mesh ones.
1959 geometricObjTypes = ['Mesh', 'Surf', 'Curve', 'Text']
1960 #geometricObjTypes = ['Mesh', 'Surf', 'Curve']
1962 Objects = scene.getChildren()
1963 objList = [ o for o in Objects if o.getType() in geometricObjTypes ]
1966 obj = self._convertToRawMeshObj(obj)
1968 scene.unlink(old_obj)
1971 # XXX Workaround for Text and Curve which have some normals
1972 # inverted when they are converted to Mesh, REMOVE that when
1973 # blender will fix that!!
1974 if old_obj.getType() in ['Curve', 'Text']:
1975 me = obj.getData(mesh=1)
1976 for f in me.faces: f.sel = 1;
1977 for v in me.verts: v.sel = 1;
1984 def _doSceneDepthSorting(self, scene):
1985 """Sort objects in the scene.
1987 The object sorting is done accordingly to the object centers.
1990 c = self._getObjPosition(self.cameraObj)
1992 by_center_pos = (lambda o1, o2:
1993 (o1.getType() == 'Mesh' and o2.getType() == 'Mesh') and
1994 cmp((self._getObjPosition(o1) - Vector(c)).length,
1995 (self._getObjPosition(o2) - Vector(c)).length)
1998 # TODO: implement sorting by bounding box, if obj1.bb is inside obj2.bb,
1999 # then ob1 goes farther than obj2, useful when obj2 has holes
2002 Objects = scene.getChildren()
2003 Objects.sort(by_center_pos)
2010 def _joinMeshObjectsInScene(self, scene):
2011 """Merge all the Mesh Objects in a scene into a single Mesh Object.
2014 oList = [o for o in scene.getChildren() if o.getType()=='Mesh']
2016 # FIXME: Object.join() do not work if the list contains 1 object
2020 mesh = Mesh.New('BigOne')
2021 bigObj = Object.New('Mesh', 'BigOne')
2028 except RuntimeError:
2029 print "\nWarning! - Can't Join Objects\n"
2030 scene.unlink(bigObj)
2033 print "Objects Type error?"
2041 # Per object/mesh methods
2043 def _convertToRawMeshObj(self, object):
2044 """Convert geometry based object to a mesh object.
2046 me = Mesh.New('RawMesh_'+object.name)
2047 me.getFromObject(object.name)
2049 newObject = Object.New('Mesh', 'RawMesh_'+object.name)
2052 # If the object has no materials set a default material
2053 if not me.materials:
2054 me.materials = [Material.New()]
2055 #for f in me.faces: f.mat = 0
2057 newObject.setMatrix(object.getMatrix())
2061 def _doModelingTransformation(self, mesh, matrix):
2062 """Transform object coordinates to world coordinates.
2064 This step is done simply applying to the object its tranformation
2065 matrix and recalculating its normals.
2067 # XXX FIXME: blender do not transform normals in the right way when
2068 # there are negative scale values
2069 if matrix[0][0] < 0 or matrix[1][1] < 0 or matrix[2][2] < 0:
2070 print "WARNING: Negative scales, expect incorrect results!"
2072 mesh.transform(matrix, True)
2074 def _doBackFaceCulling(self, mesh):
2075 """Simple Backface Culling routine.
2077 At this level we simply do a visibility test face by face and then
2078 select the vertices belonging to visible faces.
2081 # Select all vertices, so edges can be displayed even if there are no
2083 for v in mesh.verts:
2086 Mesh.Mode(Mesh.SelectModes['FACE'])
2088 for f in mesh.faces:
2090 if self._isFaceVisible(f):
2093 def _doLighting(self, mesh):
2094 """Apply an Illumination and shading model to the object.
2096 The model used is the Phong one, it may be inefficient,
2097 but I'm just learning about rendering and starting from Phong seemed
2098 the most natural way.
2101 # If the mesh has vertex colors already, use them,
2102 # otherwise turn them on and do some calculations
2103 if mesh.vertexColors:
2105 mesh.vertexColors = 1
2107 materials = mesh.materials
2109 camPos = self._getObjPosition(self.cameraObj)
2111 # We do per-face color calculation (FLAT Shading), we can easily turn
2112 # to a per-vertex calculation if we want to implement some shading
2113 # technique. For an example see:
2114 # http://www.miralab.unige.ch/papers/368.pdf
2115 for f in mesh.faces:
2121 mat = materials[f.mat]
2123 # A new default material
2125 mat = Material.New('defMat')
2127 # Check if it is a shadeless material
2128 elif mat.getMode() & Material.Modes['SHADELESS']:
2130 # Convert to a value between 0 and 255
2131 tmp_col = [ int(c * 255.0) for c in I]
2142 # do vertex color calculation
2144 TotDiffSpec = Vector([0.0, 0.0, 0.0])
2146 for l in self.lights:
2148 light_pos = self._getObjPosition(l)
2149 light = light_obj.getData()
2151 L = Vector(light_pos).normalize()
2153 V = (Vector(camPos) - Vector(f.cent)).normalize()
2155 N = Vector(f.no).normalize()
2157 if config.polygons['SHADING'] == 'TOON':
2158 NL = ShadingUtils.toonShading(N*L)
2162 # Should we use NL instead of (N*L) here?
2163 R = 2 * (N*L) * N - L
2165 Ip = light.getEnergy()
2167 # Diffuse co-efficient
2168 kd = mat.getRef() * Vector(mat.getRGBCol())
2170 kd[i] *= light.col[i]
2172 Idiff = Ip * kd * max(0, NL)
2175 # Specular component
2176 ks = mat.getSpec() * Vector(mat.getSpecCol())
2177 ns = mat.getHardness()
2178 Ispec = Ip * ks * pow(max(0, (V*R)), ns)
2180 TotDiffSpec += (Idiff+Ispec)
2184 Iamb = Vector(Blender.World.Get()[0].getAmb())
2187 # Emissive component (convert to a triplet)
2188 ki = Vector([mat.getEmit()]*3)
2190 #I = ki + Iamb + (Idiff + Ispec)
2191 I = ki + (ka * Iamb) + TotDiffSpec
2194 # Set Alpha component
2196 I.append(mat.getAlpha())
2198 # Clamp I values between 0 and 1
2199 I = [ min(c, 1) for c in I]
2200 I = [ max(0, c) for c in I]
2202 # Convert to a value between 0 and 255
2203 tmp_col = [ int(c * 255.0) for c in I]
2211 def _doProjection(self, mesh, projector):
2212 """Apply Viewing and Projection tranformations.
2215 for v in mesh.verts:
2216 p = projector.doProjection(v.co[:])
2221 #mesh.recalcNormals()
2224 # We could reeset Camera matrix, since now
2225 # we are in Normalized Viewing Coordinates,
2226 # but doung that would affect World Coordinate
2227 # processing for other objects
2229 #self.cameraObj.data.type = 1
2230 #self.cameraObj.data.scale = 2.0
2231 #m = Matrix().identity()
2232 #self.cameraObj.setMatrix(m)
2234 def _doViewFrustumClipping(self, mesh):
2235 """Clip faces against the View Frustum.
2239 def __simpleDepthSort(self, mesh):
2240 """Sort faces by the furthest vertex.
2242 This simple mesthod is known also as the painter algorithm, and it
2243 solves HSR correctly only for convex meshes.
2248 # The sorting requires circa n*log(n) steps
2250 progress.setActivity("HSR: Painter", n*log(n))
2252 by_furthest_z = (lambda f1, f2: progress.update() and
2253 cmp(max([v.co[2] for v in f1]), max([v.co[2] for v in f2])+EPS)
2256 # FIXME: using NMesh to sort faces. We should avoid that!
2257 nmesh = NMesh.GetRaw(mesh.name)
2259 # remember that _higher_ z values mean further points
2260 nmesh.faces.sort(by_furthest_z)
2261 nmesh.faces.reverse()
2266 def __newellDepthSort(self, mesh):
2267 """Newell's depth sorting.
2273 # Find non planar quads and convert them to triangle
2274 #for f in mesh.faces:
2276 # if is_nonplanar_quad(f.v):
2277 # print "NON QUAD??"
2281 # Now reselect all faces
2282 for f in mesh.faces:
2284 mesh.quadToTriangle()
2286 # FIXME: using NMesh to sort faces. We should avoid that!
2287 nmesh = NMesh.GetRaw(mesh.name)
2289 # remember that _higher_ z values mean further points
2290 nmesh.faces.sort(by_furthest_z)
2291 nmesh.faces.reverse()
2293 # Begin depth sort tests
2295 # use the smooth flag to set marked faces
2296 for f in nmesh.faces:
2299 facelist = nmesh.faces[:]
2303 # The steps are _at_least_ equal to len(facelist), we do not count the
2304 # feces coming out from splitting!!
2305 progress.setActivity("HSR: Newell", len(facelist))
2306 #progress.setQuiet(True)
2309 while len(facelist):
2310 debug("\n----------------------\n")
2311 debug("len(facelits): %d\n" % len(facelist))
2314 pSign = sign(P.normal[2])
2316 # We can discard faces parallel to the view vector
2317 #if P.normal[2] == 0:
2318 # facelist.remove(P)
2324 for Q in facelist[1:]:
2326 debug("P.smooth: " + str(P.smooth) + "\n")
2327 debug("Q.smooth: " + str(Q.smooth) + "\n")
2330 qSign = sign(Q.normal[2])
2331 # TODO: check also if Q is parallel??
2333 # Test 0: We need to test only those Qs whose furthest vertex
2334 # is closer to the observer than the closest vertex of P.
2336 zP = [v.co[2] for v in P.v]
2337 zQ = [v.co[2] for v in Q.v]
2338 notZOverlap = min(zP) > max(zQ) + EPS
2342 debug("NOT Z OVERLAP!\n")
2344 # If Q is not marked then we can safely print P
2347 debug("met a marked face\n")
2351 # Test 1: X extent overlapping
2352 xP = [v.co[0] for v in P.v]
2353 xQ = [v.co[0] for v in Q.v]
2354 #notXOverlap = (max(xP) <= min(xQ)) or (max(xQ) <= min(xP))
2355 notXOverlap = (min(xQ) >= max(xP)-EPS) or (min(xP) >= max(xQ)-EPS)
2359 debug("NOT X OVERLAP!\n")
2363 # Test 2: Y extent Overlapping
2364 yP = [v.co[1] for v in P.v]
2365 yQ = [v.co[1] for v in Q.v]
2366 #notYOverlap = (max(yP) <= min(yQ)) or (max(yQ) <= min(yP))
2367 notYOverlap = (min(yQ) >= max(yP)-EPS) or (min(yP) >= max(yQ)-EPS)
2371 debug("NOT Y OVERLAP!\n")
2375 # Test 3: P vertices are all behind the plane of Q
2378 d = qSign * HSR.Distance(Vector(Pi), Q)
2381 pVerticesBehindPlaneQ = (n == len(P))
2383 if pVerticesBehindPlaneQ:
2385 debug("P BEHIND Q!\n")
2389 # Test 4: Q vertices in front of the plane of P
2392 d = pSign * HSR.Distance(Vector(Qi), P)
2395 qVerticesInFrontPlaneP = (n == len(Q))
2397 if qVerticesInFrontPlaneP:
2399 debug("Q IN FRONT OF P!\n")
2403 # Test 5: Check if projections of polygons effectively overlap,
2404 # in previous tests we checked only bounding boxes.
2406 #if not projectionsOverlap(P, Q):
2407 if not ( HSR.projectionsOverlap(P, Q) or HSR.projectionsOverlap(Q, P)):
2409 debug("Projections do not overlap!\n")
2412 # We still can't say if P obscures Q.
2414 # But if Q is marked we do a face-split trying to resolve a
2415 # difficulty (maybe a visibility cycle).
2418 debug("Possibly a cycle detected!\n")
2419 debug("Split here!!\n")
2421 facelist = HSR.facesplit(P, Q, facelist, nmesh)
2425 # The question now is: Does Q obscure P?
2428 # Test 3bis: Q vertices are all behind the plane of P
2431 d = pSign * HSR.Distance(Vector(Qi), P)
2434 qVerticesBehindPlaneP = (n == len(Q))
2436 if qVerticesBehindPlaneP:
2437 debug("\nTest 3bis\n")
2438 debug("Q BEHIND P!\n")
2441 # Test 4bis: P vertices in front of the plane of Q
2444 d = qSign * HSR.Distance(Vector(Pi), Q)
2447 pVerticesInFrontPlaneQ = (n == len(P))
2449 if pVerticesInFrontPlaneQ:
2450 debug("\nTest 4bis\n")
2451 debug("P IN FRONT OF Q!\n")
2454 # We don't even know if Q does obscure P, so they should
2455 # intersect each other, split one of them in two parts.
2456 if not qVerticesBehindPlaneP and not pVerticesInFrontPlaneQ:
2457 debug("\nSimple Intersection?\n")
2458 debug("Test 3bis or 4bis failed\n")
2459 debug("Split here!!2\n")
2461 facelist = HSR.facesplit(P, Q, facelist, nmesh)
2466 facelist.insert(0, Q)
2469 debug("Q marked!\n")
2473 if split_done == 0 and face_marked == 0:
2476 dumpfaces(maplist, "dump"+str(len(maplist)).zfill(4)+".svg")
2480 if len(facelist) == 870:
2481 dumpfaces([P, Q], "loopdebug.svg")
2484 #if facelist == None:
2486 # print [v.co for v in P]
2487 # print [v.co for v in Q]
2490 # end of while len(facelist)
2493 nmesh.faces = maplist
2494 #for f in nmesh.faces:
2500 def _doHiddenSurfaceRemoval(self, mesh):
2501 """Do HSR for the given mesh.
2503 if len(mesh.faces) == 0:
2506 if config.polygons['HSR'] == 'PAINTER':
2507 print "\nUsing the Painter algorithm for HSR."
2508 self.__simpleDepthSort(mesh)
2510 elif config.polygons['HSR'] == 'NEWELL':
2511 print "\nUsing the Newell's algorithm for HSR."
2512 self.__newellDepthSort(mesh)
2515 def _doEdgesStyle(self, mesh, edgestyleSelect):
2516 """Process Mesh Edges accroding to a given selection style.
2518 Examples of algorithms:
2521 given an edge if its adjacent faces have the same normal (that is
2522 they are complanar), than deselect it.
2525 given an edge if one its adjacent faces is frontfacing and the
2526 other is backfacing, than select it, else deselect.
2529 Mesh.Mode(Mesh.SelectModes['EDGE'])
2531 edge_cache = MeshUtils.buildEdgeFaceUsersCache(mesh)
2533 for i,edge_faces in enumerate(edge_cache):
2534 mesh.edges[i].sel = 0
2535 if edgestyleSelect(edge_faces):
2536 mesh.edges[i].sel = 1
2539 for e in mesh.edges:
2542 if edgestyleSelect(e, mesh):
2548 # ---------------------------------------------------------------------
2550 ## GUI Class and Main Program
2552 # ---------------------------------------------------------------------
2555 from Blender import BGL, Draw
2556 from Blender.BGL import *
2562 # Output Format menu
2563 output_format = config.output['FORMAT']
2564 default_value = outputWriters.keys().index(output_format)+1
2565 GUI.outFormatMenu = Draw.Create(default_value)
2566 GUI.evtOutFormatMenu = 0
2568 # Animation toggle button
2569 GUI.animToggle = Draw.Create(config.output['ANIMATION'])
2570 GUI.evtAnimToggle = 1
2572 # Join Objects toggle button
2573 GUI.joinObjsToggle = Draw.Create(config.output['JOIN_OBJECTS'])
2574 GUI.evtJoinObjsToggle = 2
2576 # Render filled polygons
2577 GUI.polygonsToggle = Draw.Create(config.polygons['SHOW'])
2579 # Shading Style menu
2580 shading_style = config.polygons['SHADING']
2581 default_value = shadingStyles.keys().index(shading_style)+1
2582 GUI.shadingStyleMenu = Draw.Create(default_value)
2583 GUI.evtShadingStyleMenu = 21
2585 GUI.evtPolygonsToggle = 3
2586 # We hide the config.polygons['EXPANSION_TRICK'], for now
2588 # Render polygon edges
2589 GUI.showEdgesToggle = Draw.Create(config.edges['SHOW'])
2590 GUI.evtShowEdgesToggle = 4
2592 # Render hidden edges
2593 GUI.showHiddenEdgesToggle = Draw.Create(config.edges['SHOW_HIDDEN'])
2594 GUI.evtShowHiddenEdgesToggle = 5
2597 edge_style = config.edges['STYLE']
2598 default_value = edgeStyles.keys().index(edge_style)+1
2599 GUI.edgeStyleMenu = Draw.Create(default_value)
2600 GUI.evtEdgeStyleMenu = 6
2603 GUI.edgeWidthSlider = Draw.Create(config.edges['WIDTH'])
2604 GUI.evtEdgeWidthSlider = 7
2607 c = config.edges['COLOR']
2608 GUI.edgeColorPicker = Draw.Create(c[0]/255.0, c[1]/255.0, c[2]/255.0)
2609 GUI.evtEdgeColorPicker = 71
2612 GUI.evtRenderButton = 8
2615 GUI.evtExitButton = 9
2619 # initialize static members
2622 glClear(GL_COLOR_BUFFER_BIT)
2623 glColor3f(0.0, 0.0, 0.0)
2624 glRasterPos2i(10, 350)
2625 Draw.Text("VRM: Vector Rendering Method script. Version %s." %
2627 glRasterPos2i(10, 335)
2628 Draw.Text("Press Q or ESC to quit.")
2630 # Build the output format menu
2631 glRasterPos2i(10, 310)
2632 Draw.Text("Select the output Format:")
2633 outMenuStruct = "Output Format %t"
2634 for t in outputWriters.keys():
2635 outMenuStruct = outMenuStruct + "|%s" % t
2636 GUI.outFormatMenu = Draw.Menu(outMenuStruct, GUI.evtOutFormatMenu,
2637 10, 285, 160, 18, GUI.outFormatMenu.val, "Choose the Output Format")
2640 GUI.animToggle = Draw.Toggle("Animation", GUI.evtAnimToggle,
2641 10, 260, 160, 18, GUI.animToggle.val,
2642 "Toggle rendering of animations")
2644 # Join Objects toggle
2645 GUI.joinObjsToggle = Draw.Toggle("Join objects", GUI.evtJoinObjsToggle,
2646 10, 235, 160, 18, GUI.joinObjsToggle.val,
2647 "Join objects in the rendered file")
2650 Draw.Button("Render", GUI.evtRenderButton, 10, 210-25, 75, 25+18,
2652 Draw.Button("Exit", GUI.evtExitButton, 95, 210-25, 75, 25+18, "Exit!")
2655 glRasterPos2i(200, 310)
2656 Draw.Text("Rendering Style:")
2659 GUI.polygonsToggle = Draw.Toggle("Filled Polygons", GUI.evtPolygonsToggle,
2660 200, 285, 160, 18, GUI.polygonsToggle.val,
2661 "Render filled polygons")
2663 if GUI.polygonsToggle.val == 1:
2665 # Polygon Shading Style
2666 shadingStyleMenuStruct = "Shading Style %t"
2667 for t in shadingStyles.keys():
2668 shadingStyleMenuStruct = shadingStyleMenuStruct + "|%s" % t.lower()
2669 GUI.shadingStyleMenu = Draw.Menu(shadingStyleMenuStruct, GUI.evtShadingStyleMenu,
2670 200, 260, 160, 18, GUI.shadingStyleMenu.val,
2671 "Choose the shading style")
2675 GUI.showEdgesToggle = Draw.Toggle("Show Edges", GUI.evtShowEdgesToggle,
2676 200, 235, 160, 18, GUI.showEdgesToggle.val,
2677 "Render polygon edges")
2679 if GUI.showEdgesToggle.val == 1:
2682 edgeStyleMenuStruct = "Edge Style %t"
2683 for t in edgeStyles.keys():
2684 edgeStyleMenuStruct = edgeStyleMenuStruct + "|%s" % t.lower()
2685 GUI.edgeStyleMenu = Draw.Menu(edgeStyleMenuStruct, GUI.evtEdgeStyleMenu,
2686 200, 210, 160, 18, GUI.edgeStyleMenu.val,
2687 "Choose the edge style")
2690 GUI.edgeWidthSlider = Draw.Slider("Width: ", GUI.evtEdgeWidthSlider,
2691 200, 185, 140, 18, GUI.edgeWidthSlider.val,
2692 0.0, 10.0, 0, "Change Edge Width")
2695 GUI.edgeColorPicker = Draw.ColorPicker(GUI.evtEdgeColorPicker,
2696 342, 185, 18, 18, GUI.edgeColorPicker.val, "Choose Edge Color")
2699 GUI.showHiddenEdgesToggle = Draw.Toggle("Show Hidden Edges",
2700 GUI.evtShowHiddenEdgesToggle,
2701 200, 160, 160, 18, GUI.showHiddenEdgesToggle.val,
2702 "Render hidden edges as dashed lines")
2704 glRasterPos2i(10, 160)
2705 Draw.Text("%s (c) 2006" % __author__)
2707 def event(evt, val):
2709 if evt == Draw.ESCKEY or evt == Draw.QKEY:
2716 def button_event(evt):
2718 if evt == GUI.evtExitButton:
2721 elif evt == GUI.evtOutFormatMenu:
2722 i = GUI.outFormatMenu.val - 1
2723 config.output['FORMAT']= outputWriters.keys()[i]
2724 # Set the new output file
2726 outputfile = Blender.sys.splitext(basename)[0] + "." + str(config.output['FORMAT']).lower()
2728 elif evt == GUI.evtAnimToggle:
2729 config.output['ANIMATION'] = bool(GUI.animToggle.val)
2731 elif evt == GUI.evtJoinObjsToggle:
2732 config.output['JOIN_OBJECTS'] = bool(GUI.joinObjsToggle.val)
2734 elif evt == GUI.evtPolygonsToggle:
2735 config.polygons['SHOW'] = bool(GUI.polygonsToggle.val)
2737 elif evt == GUI.evtShadingStyleMenu:
2738 i = GUI.shadingStyleMenu.val - 1
2739 config.polygons['SHADING'] = shadingStyles.keys()[i]
2741 elif evt == GUI.evtShowEdgesToggle:
2742 config.edges['SHOW'] = bool(GUI.showEdgesToggle.val)
2744 elif evt == GUI.evtShowHiddenEdgesToggle:
2745 config.edges['SHOW_HIDDEN'] = bool(GUI.showHiddenEdgesToggle.val)
2747 elif evt == GUI.evtEdgeStyleMenu:
2748 i = GUI.edgeStyleMenu.val - 1
2749 config.edges['STYLE'] = edgeStyles.keys()[i]
2751 elif evt == GUI.evtEdgeWidthSlider:
2752 config.edges['WIDTH'] = float(GUI.edgeWidthSlider.val)
2754 elif evt == GUI.evtEdgeColorPicker:
2755 config.edges['COLOR'] = [int(c*255.0) for c in GUI.edgeColorPicker.val]
2757 elif evt == GUI.evtRenderButton:
2758 label = "Save %s" % config.output['FORMAT']
2759 # Show the File Selector
2761 Blender.Window.FileSelector(vectorize, label, outputfile)
2764 print "Event: %d not handled!" % evt
2771 from pprint import pprint
2773 pprint(config.output)
2774 pprint(config.polygons)
2775 pprint(config.edges)
2777 _init = staticmethod(_init)
2778 draw = staticmethod(draw)
2779 event = staticmethod(event)
2780 button_event = staticmethod(button_event)
2781 conf_debug = staticmethod(conf_debug)
2783 # A wrapper function for the vectorizing process
2784 def vectorize(filename):
2785 """The vectorizing process is as follows:
2787 - Instanciate the writer and the renderer
2792 print "\nERROR: invalid file name!"
2795 from Blender import Window
2796 editmode = Window.EditMode()
2797 if editmode: Window.EditMode(0)
2799 actualWriter = outputWriters[config.output['FORMAT']]
2800 writer = actualWriter(filename)
2802 renderer = Renderer()
2803 renderer.doRendering(writer, config.output['ANIMATION'])
2805 if editmode: Window.EditMode(1)
2810 if __name__ == "__main__":
2815 basename = Blender.sys.basename(Blender.Get('filename'))
2817 outputfile = Blender.sys.splitext(basename)[0] + "." + str(config.output['FORMAT']).lower()
2819 if Blender.mode == 'background':
2820 progress = ConsoleProgressIndicator()
2821 vectorize(outputfile)
2823 progress = GraphicalProgressIndicator()
2824 Draw.Register(GUI.draw, GUI.event, GUI.button_event)