X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=mesh_export.py;h=d7531b36c0f5210d56b8085463c116db0d6e67fd;hb=3f285d3f4fd0a6790bf1efa780284dc7ba2287a2;hp=016db5bf40c1c3914a30ccb3535d74821538f52c;hpb=d8233a3fe32bb3fd7d88b673b2c0d2f6dd10fd87;p=libs%2Fgl.git diff --git a/mesh_export.py b/mesh_export.py index 016db5bf..d7531b36 100644 --- a/mesh_export.py +++ b/mesh_export.py @@ -9,6 +9,7 @@ Group: 'Export' import sys import math +import bpy import Blender class Edge: @@ -56,23 +57,17 @@ class Face: self.edges=[] self.verts=[v for v in mf.verts] self.flag=False - + def __getattr__(self, attr): return getattr(self._mface, attr) - def get_following_vertex(self, *vt): - seen=False - indices=[v.index for v in vt] - for v in self.verts: - if v.index in indices: - seen=True - elif seen: - return v - - if seen: - return self.verts[0] - - return None + def get_vertices_from(self, *vt): + indices=[u.index for u in vt] + flags=[(v.index in indices) for v in self.verts] + l=len(self.verts) + for i in range(l): + if flags[i] and not flags[(i+l-1)%l]: + return self.verts[i:]+self.verts[:i] class SmoothGroup: @@ -104,6 +99,9 @@ class Exporter: self.out_file=sys.stdout else: self.out_file=file(fn, "w") + self.use_strips=True + self.use_degen_tris=True + self.debug=False def find_smooth_group(self, face, sg): face.smooth_group=sg @@ -130,25 +128,24 @@ class Exporter: if not edge: return None - v1=face.get_following_vertex(edge.v1, edge.v2) - v2=face.get_following_vertex(v1) - if len(face.verts)==4: - result=[v2, v1] - else: - result=[v1, v2] - - print edge.key - print [v.index for v in result] + if self.debug: + print "Starting strip from %s"%[v.index for v in face.verts] + + verts=face.get_vertices_from(edge.v1, edge.v2) + result=[verts[-2], verts[-1]] while 1: + verts=face.get_vertices_from(*result[-2:]) + k=len(result)%2 + if self.debug: + print " %d %s"%(len(result), [v.index for v in verts]) + face.flag=True - print "face =",[v.index for v in face.verts] - print [e.key for e in face.edges] - for i in range(2, len(face.verts)): - v=face.get_following_vertex(result[-2], result[-1]) - print v.index - result.append(v) - print [v.index for v in result] + if len(verts)==4 and not k: + result.append(verts[3]) + result.append(verts[2]) + if len(verts)==4 and k: + result.append(verts[3]) i1=result[-2].index i2=result[-1].index @@ -159,16 +156,19 @@ class Exporter: break next=edge.other_face(face) - if next.smooth_group.index!=face.smooth_group.index or next.flag: + if not next or next.smooth_group.index!=face.smooth_group.index or next.flag: break face=next + if self.debug: + print " %s"%[v.index for v in result] + return result def export(self): - scene=Blender.Scene.GetCurrent() + scene=bpy.data.scenes.active - obj=scene.getActiveObject() + obj=scene.objects.active if obj.getType()!="Mesh": raise Exception, "Can only export Mesh data" @@ -186,6 +186,9 @@ class Exporter: for e in edges.itervalues(): e.check_smooth(smooth_limit) + if self.debug: + print "%d faces, %d edges"%(len(faces), len(edges)) + smooth_groups=[] for f in faces: if not f.smooth_group: @@ -193,38 +196,64 @@ class Exporter: smooth_groups.append(sg) self.find_smooth_group(f, sg) - strips=[] - """XXX Stripping and smoothing are currently imcompatible for sg in smooth_groups: - for f in sg.faces: - if not f.flag: - strip=self.create_strip(f) - if strip: - strips.append(strip)""" + sg.find_vertices() - for i in mesh.verts: - print i.co + if self.debug: + print "%d 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)) - #print [v.index for v in strips[0]] + strips=[] + if self.use_strips: + for sg in smooth_groups: + for f in sg.faces: + if not f.flag: + strip=self.create_strip(f) + if strip: + strips.append(strip) + + if self.use_degen_tris: + big_strip=[] + for s in strips: + if big_strip: + big_strip+=[big_strip[-1], s[0]] + big_strip+=s + + for f in faces: + if not f.flag: + if big_strip: + big_strip+=[big_strip[-1], f.verts[0]] + big_strip+=[f.verts[i] for i in (0, 1, -1)] + if len(f.verts)==4: + big_strip.append(f.verts[-2]) + f.flag=True + + strips=[big_strip] verts=[] for sg in smooth_groups: - sg.find_vertices() for v in sg.verts: v.index=len(verts) verts.append(v) - #print [v.index for v in strips[0]] - self.out_file.write("vertices NORMAL3_VERTEX3\n{\n") + norm=None for v in verts: - self.out_file.write("\tnormal3 %f %f %f;\n"%tuple(v.no)) + if v.no!=norm: + self.out_file.write("\tnormal3 %f %f %f;\n"%tuple(v.no)) + norm=v.no self.out_file.write("\tvertex3 %f %f %f;\n"%tuple(v.co)) self.out_file.write("};\n") for s in strips: self.out_file.write("batch TRIANGLE_STRIP\n{\n\tindices") + n=0 for v in s: self.out_file.write(" %u"%v.index) + n+=1; + if n%32==0: + self.out_file.write(";\n\tindices") self.out_file.write(";\n};\n") first=True @@ -246,6 +275,7 @@ class FrontEnd: def export(self, fn): exp=Exporter(fn) + #exp.use_degen_tris=False exp.export()