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:
86 vt.no=Blender.Mathutils.Vector(f.no)
91 f.verts[i]=vert_map[v.index]
92 vert_map[v.index].no+=f.no
99 def __init__(self, fn):
102 self.out_file=sys.stdout
104 self.out_file=file(fn, "w")
106 self.use_degen_tris=True
109 def find_smooth_group(self, face, sg):
111 sg.faces.append(face)
117 other=e.other_face(cur)
118 if other and not other.smooth_group:
119 other.smooth_group=sg
120 sg.faces.append(other)
123 def create_strip(self, face):
126 other=e.other_face(face)
127 if other and other.smooth_group.index==face.smooth_group.index and not other.flag:
135 print "Starting strip from %s"%[v.index for v in face.verts]
137 verts=face.get_vertices_from(edge.v1, edge.v2)
138 result=[verts[-2], verts[-1]]
141 verts=face.get_vertices_from(*result[-2:])
144 print " %d %s"%(len(result), [v.index for v in verts])
147 if len(verts)==4 and not k:
148 result.append(verts[3])
149 result.append(verts[2])
150 if len(verts)==4 and k:
151 result.append(verts[3])
155 ekey=(min(i1, i2), max(i1, i2))
161 next=edge.other_face(face)
162 if not next or next.smooth_group.index!=face.smooth_group.index or next.flag:
167 print " %s"%[v.index for v in result]
172 scene=bpy.data.scenes.active
174 obj=scene.objects.active
175 if obj.getType()!="Mesh":
176 raise Exception, "Can only export Mesh data"
178 mesh=obj.getData(mesh=True)
180 faces=[Face(f) for f in mesh.faces]
182 edges=dict([(e.key, Edge(e)) for e in mesh.edges])
184 for e in f.edge_keys:
185 edges[e].faces.append(f)
186 f.edges.append(edges[e])
188 smooth_limit=math.cos(mesh.degr*math.pi/180)
189 for e in edges.itervalues():
190 e.check_smooth(smooth_limit)
193 print "%d faces, %d edges"%(len(faces), len(edges))
197 if not f.smooth_group:
198 sg=SmoothGroup(len(smooth_groups))
199 smooth_groups.append(sg)
200 self.find_smooth_group(f, sg)
202 for sg in smooth_groups:
206 print "%d smooth groups:"
207 for i in range(len(smooth_groups)):
209 print " %d: %d faces, %d vertices"%(i, len(sg.faces), len(sg.verts))
213 for sg in smooth_groups:
216 strip=self.create_strip(f)
220 if self.use_degen_tris:
224 big_strip+=[big_strip[-1], s[0]]
230 big_strip+=[big_strip[-1], f.verts[0]]
231 big_strip+=[f.verts[i] for i in (0, 1, -1)]
233 big_strip.append(f.verts[-2])
239 for sg in smooth_groups:
244 self.out_file.write("vertices NORMAL3_VERTEX3\n{\n")
248 self.out_file.write("\tnormal3 %f %f %f;\n"%tuple(v.no))
250 self.out_file.write("\tvertex3 %f %f %f;\n"%tuple(v.co))
251 self.out_file.write("};\n")
253 self.out_file.write("batch TRIANGLE_STRIP\n{\n\tindices")
256 self.out_file.write(" %u"%v.index)
259 self.out_file.write(";\n\tindices")
260 self.out_file.write(";\n};\n")
266 self.out_file.write("batch TRIANGLES\n{\n")
268 for i in range(2, len(f.verts)):
269 self.out_file.write("\tindices %u %u %u;\n"%(f.verts[0].index, f.verts[i-1].index, f.verts[i].index))
271 self.out_file.write("};\n")
277 Blender.Window.FileSelector(self.export, "Export MSP GL mesh", Blender.sys.makename(ext='.mesh'))
279 def export(self, fn):
281 #exp.use_degen_tris=False
285 if __name__=="__main__":