]> git.tdb.fi Git - libs/gl.git/commitdiff
Blender export: Add support for smoothing
authorMikko Rasa <tdb@tdb.fi>
Tue, 4 Sep 2007 09:06:55 +0000 (09:06 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 4 Sep 2007 09:06:55 +0000 (09:06 +0000)
mesh_export.py

index 0e63d336aa2d1ad0a42bd409c94ca90718639359..932433360f93649db45d61b6c63b290990a37f0b 100644 (file)
@@ -8,8 +8,79 @@ Group: 'Export'
 """
 
 import sys
+import math
 import Blender
 
+class Edge:
+       def __init__(self, me):
+               self._medge=me
+               self.faces=[]
+               self.smooth=False
+
+       def __getattr__(self, attr):
+               return getattr(self._medge, attr)
+
+       def check_smooth(self, limit):
+               if len(self.faces)!=2:
+                       return
+
+               d=Blender.Mathutils.DotVecs(self.faces[0].no, self.faces[1].no)
+               if (d>limit and self.faces[0].smooth and self.faces[1].smooth) or d>0.999:
+                       self.smooth=1
+
+       def other_face(self, f):
+               if f.index==self.faces[0].index:
+                       if len(self.faces)>=2:
+                               return self.faces[1]
+                       else:
+                               return None
+               else:
+                       return self.faces[0]
+
+
+class Vertex:
+       def __init__(self, mv):
+               if mv.__class__==Vertex:
+                       self._mvert=mv._mvert
+               else:
+                       self._mvert=mv
+
+       def __getattr__(self, attr):
+               return getattr(self._mvert, attr)
+
+
+class Face:
+       def __init__(self, mf):
+               self._mface=mf
+               self.smooth_group=None
+               self.edges=[]
+               self.verts=[v for v in mf.verts]
+       
+       def __getattr__(self, attr):
+               return getattr(self._mface, attr)
+
+
+class SmoothGroup:
+       def __init__(self):
+               self.faces=[]
+               self.verts=[]
+
+       def find_vertices(self):
+               vert_map={}
+               for f in self.faces:
+                       for i in range(len(f.verts)):
+                               v=f.verts[i]
+                               if v.index not in vert_map:
+                                       vt=Vertex(v)
+                                       if not f.smooth:
+                                               vt.no=f.no
+                                       self.verts.append(vt)
+                                       vert_map[v.index]=vt
+                                       f.verts[i]=vt
+                               else:
+                                       f.verts[i]=vert_map[v.index]
+
+
 class Exporter:
        def __init__(self, fn):
                self.filename=fn
@@ -18,6 +89,20 @@ class Exporter:
                else:
                        self.out_file=file(fn, "w")
 
+       def find_smooth_group(self, face, sg):
+               face.smooth_group=sg
+               sg.faces.append(face)
+               queue=[face]
+               while queue:
+                       f=queue.pop(0)
+                       for e in f.edges:
+                               if e.smooth:
+                                       f2=e.other_face(f)
+                                       if f2 and not f2.smooth_group:
+                                               f2.smooth_group=sg
+                                               sg.faces.append(f2)
+                                               queue.append(f2)
+
        def export(self):
                scene=Blender.Scene.GetCurrent()
 
@@ -27,26 +112,54 @@ class Exporter:
 
                mesh=obj.getData(mesh=True)
 
-               self.out_file.write("vertices NORMAL3,VERTEX3\n{\n")
-               for v in mesh.verts:
+               faces=[Face(f) for f in mesh.faces]
+
+               edges=dict([(e.key, Edge(e)) for e in mesh.edges])
+               for f in faces:
+                       for e in f.edge_keys:
+                               edges[e].faces.append(f)
+                               f.edges.append(edges[e])
+
+               smooth_limit=math.cos(mesh.degr*math.pi/180)
+               for e in edges.itervalues():
+                       e.check_smooth(smooth_limit)
+
+               smooth_groups=[]
+               for f in faces:
+                       if not f.smooth_group:
+                               sg=SmoothGroup()
+                               smooth_groups.append(sg)
+                               self.find_smooth_group(f, sg)
+
+               verts=[]
+               for sg in smooth_groups:
+                       sg.find_vertices()
+                       for v in sg.verts:
+                               v.index=len(verts)
+                               verts.append(v)
+
+               self.out_file.write("vertices NORMAL3_VERTEX3\n{\n")
+               for v in verts:
                        self.out_file.write("\tnormal3 %g %g %g;\n"%tuple(v.no))
                        self.out_file.write("\tvertex3 %g %g %g;\n"%tuple(v.co))
                self.out_file.write("};\n")
                self.out_file.write("batch TRIANGLES\n{\n")
-               for f in mesh.faces:
+               for f in faces:
                        for i in range(2, len(f.verts)):
                                self.out_file.write("\tindices %u %u %u;\n"%(f.verts[0].index, f.verts[i-1].index, f.verts[i].index))
                self.out_file.write("};\n")
 
+
 class FrontEnd:
        def run(self):
                #self.export(None)
                Blender.Window.FileSelector(self.export, "Export MSP GL mesh", Blender.sys.makename(ext='.mesh'))
-       
+
        def export(self, fn):
                exp=Exporter(fn)
                exp.export()
 
+
 if __name__=="__main__":
        fe=FrontEnd()
        fe.run()