Initial commit vrm-0.1
authorNikola Radovanovic <nrad@EUnet.yu>
Fri, 23 Jan 2004 10:34:07 +0000 (11:34 +0100)
committerAntonio Ospite <ospite@studenti.unina.it>
Wed, 23 Sep 2009 19:59:33 +0000 (21:59 +0200)
Original first version by Nikola Radovanovic.

Signed-off-by: Antonio Ospite <ospite@studenti.unina.it>
vrm.py [new file with mode: 0644]

diff --git a/vrm.py b/vrm.py
new file mode 100644 (file)
index 0000000..380a65b
--- /dev/null
+++ b/vrm.py
@@ -0,0 +1,244 @@
+#!BPY
+
+"""
+Name: 'VRM'
+Blender: 228
+Group: 'Export'
+Tooltip: 'Vector Rendering Method Export Script'
+"""
+
+
+import Blender
+from Blender import Scene, Object, Lamp, Camera
+from math import *
+from Blender.Window import *
+   
+def init():
+
+    print "Init\n"
+
+    renderDir = scena.getRenderdir()
+
+# distance from camera Z'
+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 = []
+    # Rotate X
+    NewY = (PY * cos(AngleX))-(PZ * sin(AngleX))
+    NewZ = (PZ * cos(AngleX))+(PY * sin(AngleX))
+    # Rotate Y
+    PZ = NewZ
+    PY = NewY
+    NewZ = (PZ * cos(AngleY))-(PX * sin(AngleY))
+    NewX = (PX * cos(AngleY))+(PZ * sin(AngleY))
+    PX = NewX
+    PZ = NewZ
+    # Rotate Z
+    NewX = (PX * cos(AngleZ))-(PY * sin(AngleZ))
+    NewY = (PY * cos(AngleZ))+(PX * sin(AngleZ))
+    NewPoint.append(NewX)
+    NewPoint.append(NewY)
+    NewPoint.append(NewZ)
+    return NewPoint
+
+def flatern(vertx, verty, vertz):
+
+    cam = Camera.get()            # Get the cameras in scene
+    Lens = cam[0].getLens()       # The First Blender camera lens
+
+    camTyp = cam[0].getType()
+
+    msize = scena.getWinSize()
+    xres = msize[0]             # X res for output
+    yres = msize[1]                # Y res for output
+    ratio = xres/yres
+
+    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)
+    return screenxy
+
+def writesvg(ob):
+
+    for i in range(0, ob[0]+1):
+      print ob[i], "\n"
+    print "WriteSVG\n"
+
+########
+# Main #
+########
+
+scena = Scene.GetCurrent()
+init()
+
+tacka = [0,0,0]
+lice = [3,tacka,tacka,tacka,tacka]
+
+msize = scena.getWinSize()
+
+file=open("d:\proba.svg","w")
+
+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("<pageSet>\n")
+
+Objects = Blender.Object.Get()
+NUMobjects = len(Objects)
+
+startFrm = scena.startFrame()
+endFrm = scena.endFrame()
+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(scena.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`)
+#          svetla = Blender.Lamp.Get()
+#          svetlo = svetla[0]
+#          print svetlo.LocX
+          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)
+file.write("</svg>")
+file.close()
+DrawProgressBar (1.0,"Finished.")
+print "Finished\n"