Fix and reindent th main loop vrm-0.2
authorAntonio Ospite <ospite@studenti.unina.it>
Thu, 23 Mar 2006 14:47:08 +0000 (15:47 +0100)
committerAntonio Ospite <ospite@studenti.unina.it>
Thu, 24 Sep 2009 10:41:08 +0000 (12:41 +0200)
Signed-off-by: Antonio Ospite <ospite@studenti.unina.it>
vrm.py

diff --git a/vrm.py b/vrm.py
index bbff105..726c83d 100755 (executable)
--- a/vrm.py
+++ b/vrm.py
@@ -7,19 +7,12 @@ Group: 'Export'
 Tooltip: 'Vector Rendering Method Export Script'
 """
 
-
 import Blender
 from Blender import Scene, Object, Lamp, Camera
 from math import *
 from Blender.Window import *
 from Blender.Scene import Render
-   
-def init():
-
-    print "Init\n"
 
-    renderDir = context.getRenderPath()
-    
 
 # distance from camera Z'
 def Distance(PX,PY,PZ):
@@ -27,10 +20,6 @@ def Distance(PX,PY,PZ):
     dist = sqrt(PX*PX+PY*PY+PZ*PZ)
     return dist
 
-def Dodaj(x,y,z):
-    
-    print ""
-
 def RotatePoint(PX,PY,PZ,AngleX,AngleY,AngleZ):
     
     NewPoint = []
@@ -52,6 +41,16 @@ def RotatePoint(PX,PY,PZ,AngleX,AngleY,AngleZ):
     NewPoint.append(NewZ)
     return NewPoint
 
+def vetmatmult(v, M):
+    
+    v2 = [0, 0, 0, 0]
+    
+    for i in range(0, 3):
+        for j in range(0, 3):
+            v2[i] += (v[i]*M[i][j])
+
+    return v2
+
 def flatern(vertx, verty, vertz):
 
     cam = Camera.get()            # Get the cameras in scene
@@ -64,25 +63,22 @@ def flatern(vertx, verty, vertz):
     yres = msize[1]                # Y res for output
     ratio = xres/yres
 
+    fov = atan(ratio * 16.0 / Lens)  # Get fov stuff
+    
+    dist = xres/2*tan(fov)         # Calculate dist from pinhole camera to image plane
+
     screenxy=[0,0]
     x=-vertx
     y=verty
     z=vertz
 
-    fov = atan(ratio * 16.0 / Lens)  # Get fov stuff
-    dist = xres/2*tan(fov)         # Calculate dist from pinhole camera to image plane
-#----------------------------        
-# calculate x'=dist*x/z & y'=dist*x/z
-#----------------------------
-    screenxy[0]=int(xres/2+4*x*dist/z)
-    screenxy[1]=int(yres/2+4*y*dist/z)
+    #----------------------------        
+    # calculate x'=dist*x/z & y'=dist*x/z
+    #----------------------------
+    screenxy[0]=int(xres/2.0+4*x*dist/z)
+    screenxy[1]=int(yres/2.0+4*y*dist/z)
     return screenxy
 
-def writesvg(ob):
-
-    for i in range(0, ob[0]+1):
-      print ob[i], "\n"
-    print "WriteSVG\n"
 
 ########
 # Main #
@@ -90,21 +86,15 @@ def writesvg(ob):
 
 scena = Scene.GetCurrent()
 context = scena.getRenderingContext()
-
-#print dir(context)
-
-init()
-
-tacka = [0,0,0]
-lice = [3,tacka,tacka,tacka,tacka]
+renderDir = context.getRenderPath()
 
 msize = (context.imageSizeX(), context.imageSizeY())
-print msize
 
 file=open("proba.svg","w")
 
+file.write("<?xml version=\"1.0\"?>\n")
 file.write("<svg width=\"" + `msize[0]` + "\" height=\"" + `msize[1]` + "\"\n")
-file.write("xmlns=\"http://www.w3.org/2000/svg\" version=\"1.2\" streamable=\"true\">\n")
+file.write("\txmlns=\"http://www.w3.org/2000/svg\" version=\"1.2\" streamable=\"true\">\n")
 #file.write("<pageSet>\n")
 
 Objects = Blender.Object.Get()
@@ -113,138 +103,144 @@ NUMobjects = len(Objects)
 startFrm = context.startFrame()
 endFrm = startFrm
 #endFrm = context.endFrame()
+frames = range(startFrm, endFrm+1)
+
+# are we rendering an animation ?
+animation = (len(frames) > 1)
+
 camera = scena.getCurrentCamera() # Get the current camera
 
-for f in range(startFrm, endFrm+1):
-  #scena.currentFrame(f)
-  Blender.Set('curframe', f)
-
-  DrawProgressBar (f/(endFrm+1-startFrm),"Rendering ..." + str(context.currentFrame()))
-
-  print "Frame: ", f, "\n"
-  if startFrm <> endFrm: file.write("<g id=\"Frame" + str(f) + "\" style=\"visibility:hidden\">\n")
-  for o in range(NUMobjects):
-
-    if Objects[o].getType() == "Mesh":
-
-      obj = Objects[o]                  # Get the first selected object
-      objname = obj.name                # The object name
-
-
-      OBJmesh = obj.getData()           # Get the mesh data for the object
-      numfaces=len(OBJmesh.faces)         # The number of faces in the object
-      numEachVert=len(OBJmesh.faces[0])    # The number of verts in each face
-
-      #------------
-      # Get the Material Colors
-      #------------
-#      MATinfo = OBJmesh.getMaterials()
-#    
-#      if len(MATinfo) > 0:
-#          RGB=MATinfo[0].rgbCol
-#          R=int(RGB[0]*255)
-#          G=int(RGB[1]*255)
-#          B=int(RGB[2]*255)
-#          color=`R`+"."+`G`+"."+`B`
-#          print color
-#      else:
-#          color="100.100.100"
-
-      objekat = []
-
-      objekat.append(0)
-
-      for face in range(numfaces):
-        numvert = len(OBJmesh.faces[face])
-        objekat.append(numvert)
-        objekat[0] += 1
-        
-# backface cutting
-        a = []
-        a.append(OBJmesh.faces[face][0][0])
-        a.append(OBJmesh.faces[face][0][1])
-        a.append(OBJmesh.faces[face][0][2])
-        a = RotatePoint(a[0], a[1], a[2], obj.RotX, obj.RotY, obj.RotZ)
-        a[0] += obj.LocX - camera.LocX
-        a[1] += obj.LocY - camera.LocY
-        a[2] += obj.LocZ - camera.LocZ
-        b = []
-        b.append(OBJmesh.faces[face][1][0])
-        b.append(OBJmesh.faces[face][1][1])
-        b.append(OBJmesh.faces[face][1][2])
-        b = RotatePoint(b[0], b[1], b[2], obj.RotX, obj.RotY, obj.RotZ)
-        b[0] += obj.LocX - camera.LocX
-        b[1] += obj.LocY - camera.LocY
-        b[2] += obj.LocZ - camera.LocZ
-        c = []
-        c.append(OBJmesh.faces[face][numvert-1][0])
-        c.append(OBJmesh.faces[face][numvert-1][1])
-        c.append(OBJmesh.faces[face][numvert-1][2])
-        c = RotatePoint(c[0], c[1], c[2], obj.RotX, obj.RotY, obj.RotZ)
-        c[0] += obj.LocX - camera.LocX
-        c[1] += obj.LocY - camera.LocY
-        c[2] += obj.LocZ - camera.LocZ
-
-        norm = [0,0,0]
-        norm[0] = (b[1] - a[1])*(c[2] - a[2]) - (c[1] - a[1])*(b[2] - a[2])
-        norm[1] = -((b[0] - a[0])*(c[2] - a[2]) - (c[0] - a[0])*(b[2] - a[2]))
-        norm[2] = (b[0] - a[0])*(c[1] - a[1]) - (c[0] - a[0])*(b[1] - a[1])
-
-        d = norm[0]*a[0] + norm[1]*a[1] + norm[2]*a[2]
-
-        if d < 0:
-          file.write("<polygon points=\"")
-          for vert in range(numvert):
-
-            objekat[0] += 3
-
-            vertxyz = []
-
-            if vert != 0: file.write(", ")
-
-            vertxyz.append(OBJmesh.faces[face][vert][0])
-            vertxyz.append(OBJmesh.faces[face][vert][1])
-            vertxyz.append(OBJmesh.faces[face][vert][2])
-
-# rotate object
-
-            vertxyz = RotatePoint(vertxyz[0], vertxyz[1], vertxyz[2], obj.RotX, obj.RotY, obj.RotZ)
-
-            vertxyz[0] += obj.LocX - camera.LocX
-            vertxyz[1] += obj.LocY - camera.LocY
-            vertxyz[2] += obj.LocZ - camera.LocZ
-
-# rotate camera
-
-            vertxyz = RotatePoint(vertxyz[0], vertxyz[1], vertxyz[2], -camera.RotX, -camera.RotY, -camera.RotZ)
-
-            objekat.append(Distance(vertxyz[0], vertxyz[1], vertxyz[2]))
-#            dist = Distance(vertxyz[0], vertxyz[1], vertxyz[2])
-            xy = flatern(vertxyz[0], vertxyz[1], vertxyz[2])
-            px = int(xy[0])
-            py = int(xy[1])
-            objekat.append(px)
-            objekat.append(py)
-            # add/sorting in Z' direction
-            #Dodaj(px,py,Distance(vertxyz[0], vertxyz[1], vertxyz[2]))
-            file.write(`px` + ", " + `py`)
-          ambient = -200
-          svetlo = [1,1,-1]
-          vektori = (norm[0]*svetlo[0]+norm[1]*svetlo[1]+norm[2]*svetlo[2])
-          vduzine = fabs(sqrt(pow(norm[0],2)+pow(norm[1],2)+pow(norm[2],2))*sqrt(pow(svetlo[0],2)+pow(svetlo[1],2)+pow(svetlo[2],2)))
-          intensity = floor(ambient + 255 * acos(vektori/vduzine))
-          print vektori/vduzine
-          if intensity < 0: intensity = 0
-          file.write("\"\n style=\"fill:rgb("+str(intensity)+","+str(intensity)+","+str(intensity)+");stroke:rgb(0,0,0);stroke-width:1\"/>\n")
-  if startFrm <> endFrm:
-    file.write("<animate attributeName=\"visibility\" begin=\""+str(f*0.08)+"s\" dur=\"0.08s\" fill=\"remove\" to=\"visible\">\n")
-    file.write("</animate>\n")
-    file.write("</g>\n")
-
-#flatern()
-#writesvg(objekat)
+for f in frames:
+    context.currentFrame(f)
+    Blender.Set('curframe', f)
+
+    DrawProgressBar (f/len(frames),"Rendering ..." + str(f))
+
+    print "Frame: ", f, "\n"
+    if animation :
+        file.write("<g id=\"Frame" + str(f) + "\" style=\"visibility:hidden\">\n")
+
+    for o in Objects:
+
+        if o.getType() == "Mesh":
+
+            obj = o                  # Get the first selected object
+            objname = obj.name                # The object name
+
+
+            OBJmesh = obj.getData()           # Get the mesh data for the object
+            meshfaces = OBJmesh.faces        # The number of faces in the object
+
+            #------------
+            # Get the Material Colors
+            #------------
+            
+            #MATinfo = OBJmesh.getMaterials()
+            #    
+            #if len(MATinfo) > 0:
+            #    RGB=MATinfo[0].rgbCol
+            #    R=int(RGB[0]*255)
+            #    G=int(RGB[1]*255)
+            #    B=int(RGB[2]*255)
+            #    color=`R`+"."+`G`+"."+`B`
+            #    print color
+            #else:
+            #    color="100.100.100"
+
+
+            for face in range(0, len(meshfaces)):
+                numvert = len(OBJmesh.faces[face])
+                
+                #if (isVisible(face)):
+                #    print "face visible"
+
+                # backface culling
+                a = []
+                a.append(OBJmesh.faces[face][0][0])
+                a.append(OBJmesh.faces[face][0][1])
+                a.append(OBJmesh.faces[face][0][2])
+                a = RotatePoint(a[0], a[1], a[2], obj.RotX, obj.RotY, obj.RotZ)
+                a[0] += obj.LocX - camera.LocX
+                a[1] += obj.LocY - camera.LocY
+                a[2] += obj.LocZ - camera.LocZ
+                b = []
+                b.append(OBJmesh.faces[face][1][0])
+                b.append(OBJmesh.faces[face][1][1])
+                b.append(OBJmesh.faces[face][1][2])
+                b = RotatePoint(b[0], b[1], b[2], obj.RotX, obj.RotY, obj.RotZ)
+                b[0] += obj.LocX - camera.LocX
+                b[1] += obj.LocY - camera.LocY
+                b[2] += obj.LocZ - camera.LocZ
+                c = []
+                c.append(OBJmesh.faces[face][numvert-1][0])
+                c.append(OBJmesh.faces[face][numvert-1][1])
+                c.append(OBJmesh.faces[face][numvert-1][2])
+                c = RotatePoint(c[0], c[1], c[2], obj.RotX, obj.RotY, obj.RotZ)
+                c[0] += obj.LocX - camera.LocX
+                c[1] += obj.LocY - camera.LocY
+                c[2] += obj.LocZ - camera.LocZ
+
+                norm = [0,0,0]
+                norm[0] = (b[1] - a[1])*(c[2] - a[2]) - (c[1] - a[1])*(b[2] - a[2])
+                norm[1] = -((b[0] - a[0])*(c[2] - a[2]) - (c[0] - a[0])*(b[2] - a[2]))
+                norm[2] = (b[0] - a[0])*(c[1] - a[1]) - (c[0] - a[0])*(b[1] - a[1])
+
+                d = norm[0]*a[0] + norm[1]*a[1] + norm[2]*a[2]
+
+                # if the face is visible flatten it on the "picture plane"
+                if d < 0:
+                    file.write("<polygon points=\"")
+                    for vert in range(numvert):
+
+                        vertxyz = []
+
+                        if vert != 0: file.write(", ")
+
+                        vertxyz.append(OBJmesh.faces[face][vert][0])
+                        vertxyz.append(OBJmesh.faces[face][vert][1])
+                        vertxyz.append(OBJmesh.faces[face][vert][2])
+
+                        # rotate object
+                        vertxyz = RotatePoint(vertxyz[0], vertxyz[1], vertxyz[2], obj.RotX, obj.RotY, obj.RotZ)
+
+                        # original setting for translate
+                        vertxyz[0] += (obj.LocX - camera.LocX)
+                        vertxyz[1] += (obj.LocY - camera.LocY)
+                        vertxyz[2] += (obj.LocZ - camera.LocZ)
+
+                        # rotate camera
+                        vertxyz = RotatePoint(vertxyz[0], vertxyz[1], vertxyz[2], -camera.RotX, -camera.RotY, -camera.RotZ)
+
+                        #dist = Distance(vertxyz[0], vertxyz[1], vertxyz[2])
+                        xy = flatern(vertxyz[0], vertxyz[1], vertxyz[2])
+                        px = int(xy[0])
+                        py = int(xy[1])
+                        # add/sorting in Z' direction
+                        #Dodaj(px,py,Distance(vertxyz[0], vertxyz[1], vertxyz[2]))
+                        file.write(`px` + ", " + `py`)
+
+                    # per face color calculation
+                    ambient = -200
+                    #svetlo = [1, 1, -1] #original
+                    svetlo = [1, 1, -0.3]
+                    vektori = (norm[0]*svetlo[0]+norm[1]*svetlo[1]+norm[2]*svetlo[2])
+                    vduzine = fabs(sqrt(pow(norm[0],2)+pow(norm[1],2)+pow(norm[2],2))*sqrt(pow(svetlo[0],2)+pow(svetlo[1],2)+pow(svetlo[2],2)))
+                    intensity = floor(ambient + 255 * acos(vektori/vduzine))
+                    if intensity < 0:
+                        intensity = 0
+                    
+                    wireframe = True
+                    if wireframe:
+                        stroke_width=1
+                    else: 
+                        stroke_width=0
+                    file.write("\"\n style=\"fill:rgb("+str(intensity)+","+str(intensity)+","+str(intensity)+");stroke:rgb(0,0,0);stroke-width:"+str(stroke_width)+"\"/>\n")
+    if animation:
+        file.write("<animate attributeName=\"visibility\" begin=\""+str(f*0.08)+"s\" dur=\"0.08s\" fill=\"remove\" to=\"visible\">\n")
+        file.write("</animate>\n")
+        file.write("</g>\n")
+
 file.write("</svg>")
 file.close()
-print file
 DrawProgressBar (1.0,"Finished.")
 print "Finished\n"