]> git.tdb.fi Git - libs/gl.git/commitdiff
Blender exporter: Add support for texture coordinates
authorMikko Rasa <tdb@tdb.fi>
Fri, 12 Oct 2007 09:18:12 +0000 (09:18 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 12 Oct 2007 09:18:12 +0000 (09:18 +0000)
mesh_export.py

index 0158bdbf9189d3cff6e2bedae25b8e040601d02b..75db9d12dd1d1ab70faf9347c2b247f822155604 100644 (file)
@@ -14,9 +14,13 @@ import Blender
 
 class Edge:
        def __init__(self, me):
-               self._medge=me
+               if me.__class__==Edge:
+                       self._medge=me._medge
+                       self.smooth=me.smooth
+               else:
+                       self._medge=me
+                       self.smooth=False
                self.faces=[]
-               self.smooth=False
 
        def __getattr__(self, attr):
                return getattr(self._medge, attr)
@@ -43,8 +47,11 @@ class Vertex:
        def __init__(self, mv):
                if mv.__class__==Vertex:
                        self._mvert=mv._mvert
+                       self.uv=mv.uv
                else:
                        self._mvert=mv
+                       self.uv=None
+               self.orig_index=self._mvert.index
 
        def __getattr__(self, attr):
                return getattr(self._mvert, attr)
@@ -83,17 +90,66 @@ class SmoothGroup:
                                v=f.verts[i]
                                if v.index not in vert_map:
                                        vt=Vertex(v)
-                                       vt.no=Blender.Mathutils.Vector(f.no)
+                                       vt.index=len(self.verts)
                                        self.verts.append(vt)
+
                                        vert_map[v.index]=vt
-                                       f.verts[i]=vt
+                                       vt.no=Blender.Mathutils.Vector(f.no)
                                else:
-                                       f.verts[i]=vert_map[v.index]
-                                       vert_map[v.index].no+=f.no
+                                       vt=vert_map[v.index]
+                                       vt.no+=f.no
+                               f.verts[i]=vt
 
                for v in self.verts:
                        v.no.normalize()
 
+       def separate_uv(self):
+               copies={}
+               for f in self.faces:
+                       for i in range(len(f.verts)):
+                               v=f.verts[i]
+                               if not v.uv:
+                                       v.uv=f.uv[i]
+                               elif f.uv[i]!=v.uv:
+                                       if v.index not in copies:
+                                               copies[v.index]=[]
+
+                                       vt=None
+                                       for w in copies[v.index]:
+                                               if w.uv==f.uv[i]:
+                                                       vt=w
+                                                       break
+
+                                       if not vt:
+                                               vt=Vertex(v)
+                                               vt.index=len(self.verts)
+                                               vt.uv=f.uv[i]
+                                               self.verts.append(vt)
+                                               copies[v.index].append(vt)
+
+                                       f.verts[i]=vt
+
+       def create_edges(self):
+               edge_map={}
+               for f in self.faces:
+                       vert_map=dict([(v.orig_index, v) for v in f.verts])
+                       for i in range(len(f.edges)):
+                               v1=vert_map[f.edges[i].v1.index]
+                               v2=vert_map[f.edges[i].v2.index]
+                               key=tuple(sorted((v1.index, v2.index)))
+
+                               if key in edge_map:
+                                       e=edge_map[key]
+                               else:
+                                       e=Edge(f.edges[i])
+                                       edge_map[key]=e
+                                       e.v1=v1
+                                       e.v2=v2
+                                       e.key=key
+
+                               f.edges[i]=e
+                               e.faces.append(f)
+
 
 class Exporter:
        def __init__(self, fn):
@@ -201,9 +257,18 @@ class Exporter:
 
                for sg in smooth_groups:
                        sg.find_vertices()
+                       if mesh.faceUV:
+                               sg.separate_uv()
+                       sg.create_edges()
+
+               verts=[]
+               for sg in smooth_groups:
+                       for v in sg.verts:
+                               v.index=len(verts)
+                               verts.append(v)
 
                if self.debug:
-                       print "%d smooth groups:"
+                       print "%d smooth groups:"%(len(smooth_groups))
                        for i in range(len(smooth_groups)):
                                sg=smooth_groups[i]
                                print "  %d: %d faces, %d vertices"%(i, len(sg.faces), len(sg.verts))
@@ -235,18 +300,19 @@ class Exporter:
 
                                strips=[big_strip]
 
-               verts=[]
-               for sg in smooth_groups:
-                       for v in sg.verts:
-                               v.index=len(verts)
-                               verts.append(v)
-
-               self.out_file.write("vertices NORMAL3_VERTEX3\n{\n")
+               self.out_file.write("vertices NORMAL3")
+               if mesh.faceUV:
+                       self.out_file.write("_TEXCOORD2")
+               self.out_file.write("_VERTEX3\n{\n")
                norm=None
+               uv=None
                for v in verts:
                        if v.no!=norm:
                                self.out_file.write("\tnormal3 %f %f %f;\n"%tuple(v.no))
                                norm=v.no
+                       if v.uv!=uv:
+                               self.out_file.write("\ttexcoord2 %f %f;\n"%tuple(v.uv))
+                               uv=v.uv
                        self.out_file.write("\tvertex3 %f %f %f;\n"%tuple(v.co))
                self.out_file.write("};\n")
                for s in strips:
@@ -279,6 +345,7 @@ class FrontEnd:
        def export(self, fn):
                exp=Exporter(fn)
                #exp.use_degen_tris=False
+               #exp.debug=True
                exp.export()