5 Name: 'MSP GL Mesh (.mesh)...'
16 def __init__(self, me):
21 def __getattr__(self, attr):
22 return getattr(self._medge, attr)
24 def check_smooth(self, limit):
25 if len(self.faces)!=2:
28 d=Blender.Mathutils.DotVecs(self.faces[0].no, self.faces[1].no)
29 if (d>limit and self.faces[0].smooth and self.faces[1].smooth) or d>0.999:
32 def other_face(self, f):
33 if f.index==self.faces[0].index:
34 if len(self.faces)>=2:
43 def __init__(self, mv):
44 if mv.__class__==Vertex:
49 def __getattr__(self, attr):
50 return getattr(self._mvert, attr)
54 def __init__(self, mf):
56 self.smooth_group=None
58 self.verts=[v for v in mf.verts]
61 def __getattr__(self, attr):
62 return getattr(self._mface, attr)
64 def get_vertices_from(self, *vt):
65 indices=[u.index for u in vt]
66 flags=[(v.index in indices) for v in self.verts]
69 if flags[i] and not flags[(i+l-1)%l]:
70 return self.verts[i:]+self.verts[:i]
74 def __init__(self, index):
79 def find_vertices(self):
82 for i in range(len(f.verts)):
84 if v.index not in vert_map:
92 f.verts[i]=vert_map[v.index]
96 def __init__(self, fn):
99 self.out_file=sys.stdout
101 self.out_file=file(fn, "w")
103 self.use_degen_tris=True
106 def find_smooth_group(self, face, sg):
108 sg.faces.append(face)
114 other=e.other_face(cur)
115 if other and not other.smooth_group:
116 other.smooth_group=sg
117 sg.faces.append(other)
120 def create_strip(self, face):
123 other=e.other_face(face)
124 if other and other.smooth_group.index==face.smooth_group.index and not other.flag:
132 print "Starting strip from %s"%[v.index for v in face.verts]
134 verts=face.get_vertices_from(edge.v1, edge.v2)
135 result=[verts[-2], verts[-1]]
138 verts=face.get_vertices_from(*result[-2:])
141 print " %d %s"%(len(result), [v.index for v in verts])
144 if len(verts)==4 and not k:
145 result.append(verts[3])
146 result.append(verts[2])
147 if len(verts)==4 and k:
148 result.append(verts[3])
152 ekey=(min(i1, i2), max(i1, i2))
158 next=edge.other_face(face)
159 if not next or next.smooth_group.index!=face.smooth_group.index or next.flag:
164 print " %s"%[v.index for v in result]
169 scene=bpy.data.scenes.active
171 obj=scene.objects.active
172 if obj.getType()!="Mesh":
173 raise Exception, "Can only export Mesh data"
175 mesh=obj.getData(mesh=True)
177 faces=[Face(f) for f in mesh.faces]
179 edges=dict([(e.key, Edge(e)) for e in mesh.edges])
181 for e in f.edge_keys:
182 edges[e].faces.append(f)
183 f.edges.append(edges[e])
185 smooth_limit=math.cos(mesh.degr*math.pi/180)
186 for e in edges.itervalues():
187 e.check_smooth(smooth_limit)
190 print "%d faces, %d edges"%(len(faces), len(edges))
194 if not f.smooth_group:
195 sg=SmoothGroup(len(smooth_groups))
196 smooth_groups.append(sg)
197 self.find_smooth_group(f, sg)
199 for sg in smooth_groups:
203 print "%d smooth groups:"
204 for i in range(len(smooth_groups)):
206 print " %d: %d faces, %d vertices"%(i, len(sg.faces), len(sg.verts))
210 for sg in smooth_groups:
213 strip=self.create_strip(f)
217 if self.use_degen_tris:
221 big_strip+=[big_strip[-1], s[0]]
227 big_strip+=[big_strip[-1], f.verts[0]]
228 big_strip+=[f.verts[i] for i in (0, 1, -1)]
230 big_strip.append(f.verts[-2])
236 for sg in smooth_groups:
241 self.out_file.write("vertices NORMAL3_VERTEX3\n{\n")
245 self.out_file.write("\tnormal3 %f %f %f;\n"%tuple(v.no))
247 self.out_file.write("\tvertex3 %f %f %f;\n"%tuple(v.co))
248 self.out_file.write("};\n")
250 self.out_file.write("batch TRIANGLE_STRIP\n{\n\tindices")
253 self.out_file.write(" %u"%v.index)
256 self.out_file.write(";\n\tindices")
257 self.out_file.write(";\n};\n")
263 self.out_file.write("batch TRIANGLES\n{\n")
265 for i in range(2, len(f.verts)):
266 self.out_file.write("\tindices %u %u %u;\n"%(f.verts[0].index, f.verts[i-1].index, f.verts[i].index))
268 self.out_file.write("};\n")
274 Blender.Window.FileSelector(self.export, "Export MSP GL mesh", Blender.sys.makename(ext='.mesh'))
276 def export(self, fn):
278 #exp.use_degen_tris=False
282 if __name__=="__main__":