5 Name: 'MSP GL Mesh (.mesh)...'
16 def __init__(self, me):
17 if me.__class__==Edge:
25 def __getattr__(self, attr):
26 return getattr(self._medge, attr)
28 def check_smooth(self, limit):
29 if len(self.faces)!=2:
32 d=Blender.Mathutils.DotVecs(self.faces[0].no, self.faces[1].no)
33 if (d>limit and self.faces[0].smooth and self.faces[1].smooth) or d>0.999:
36 def other_face(self, f):
37 if f.index==self.faces[0].index:
38 if len(self.faces)>=2:
47 def __init__(self, mv):
48 if mv.__class__==Vertex:
54 self.orig_index=self._mvert.index
56 def __getattr__(self, attr):
57 return getattr(self._mvert, attr)
61 def __init__(self, mf):
63 self.smooth_group=None
65 self.verts=[v for v in mf.verts]
68 def __getattr__(self, attr):
69 return getattr(self._mface, attr)
71 def get_vertices_from(self, *vt):
72 indices=[u.index for u in vt]
73 flags=[(v.index in indices) for v in self.verts]
76 if flags[i] and not flags[(i+l-1)%l]:
77 return self.verts[i:]+self.verts[:i]
81 def __init__(self, index):
86 def find_vertices(self):
89 for i in range(len(f.verts)):
91 if v.index not in vert_map:
93 vt.index=len(self.verts)
97 vt.no=Blender.Mathutils.Vector(f.no)
106 def separate_uv(self):
109 for i in range(len(f.verts)):
114 if v.index not in copies:
118 for w in copies[v.index]:
125 vt.index=len(self.verts)
127 self.verts.append(vt)
128 copies[v.index].append(vt)
132 def create_edges(self):
135 vert_map=dict([(v.orig_index, v) for v in f.verts])
136 for i in range(len(f.edges)):
137 v1=vert_map[f.edges[i].v1.index]
138 v2=vert_map[f.edges[i].v2.index]
139 key=tuple(sorted((v1.index, v2.index)))
155 def __init__(self, fn):
158 self.out_file=sys.stdout
160 self.out_file=file(fn, "w")
162 self.use_degen_tris=True
165 def find_smooth_group(self, face, sg):
167 sg.faces.append(face)
173 other=e.other_face(cur)
174 if other and not other.smooth_group:
175 other.smooth_group=sg
176 sg.faces.append(other)
179 def create_strip(self, face):
182 other=e.other_face(face)
183 if other and other.smooth_group.index==face.smooth_group.index and not other.flag:
191 print "Starting strip from %s"%[v.index for v in face.verts]
193 verts=face.get_vertices_from(edge.v1, edge.v2)
194 result=[verts[-2], verts[-1]]
197 verts=face.get_vertices_from(*result[-2:])
200 print " %d %s"%(len(result), [v.index for v in verts])
203 if len(verts)==4 and not k:
204 result.append(verts[3])
205 result.append(verts[2])
206 if len(verts)==4 and k:
207 result.append(verts[3])
211 ekey=(min(i1, i2), max(i1, i2))
217 next=edge.other_face(face)
218 if not next or next.smooth_group.index!=face.smooth_group.index or next.flag:
223 print " %s"%[v.index for v in result]
228 scene=bpy.data.scenes.active
230 obj=scene.objects.active
231 if obj.getType()!="Mesh":
232 raise Exception, "Can only export Mesh data"
234 mesh=obj.getData(mesh=True)
236 faces=[Face(f) for f in mesh.faces]
238 edges=dict([(e.key, Edge(e)) for e in mesh.edges])
240 for e in f.edge_keys:
241 edges[e].faces.append(f)
242 f.edges.append(edges[e])
244 smooth_limit=math.cos(mesh.degr*math.pi/180)
245 for e in edges.itervalues():
246 e.check_smooth(smooth_limit)
249 print "%d faces, %d edges"%(len(faces), len(edges))
253 if not f.smooth_group:
254 sg=SmoothGroup(len(smooth_groups))
255 smooth_groups.append(sg)
256 self.find_smooth_group(f, sg)
258 for sg in smooth_groups:
265 for sg in smooth_groups:
271 print "%d smooth groups:"%(len(smooth_groups))
272 for i in range(len(smooth_groups)):
274 print " %d: %d faces, %d vertices"%(i, len(sg.faces), len(sg.verts))
278 for sg in smooth_groups:
281 strip=self.create_strip(f)
285 if self.use_degen_tris:
289 big_strip+=[big_strip[-1], s[0]]
295 big_strip+=[big_strip[-1], f.verts[0]]
296 big_strip+=[f.verts[i] for i in (0, 1, -1)]
298 big_strip.append(f.verts[-2])
303 self.out_file.write("vertices NORMAL3")
305 self.out_file.write("_TEXCOORD2")
306 self.out_file.write("_VERTEX3\n{\n")
311 self.out_file.write("\tnormal3 %f %f %f;\n"%tuple(v.no))
314 self.out_file.write("\ttexcoord2 %f %f;\n"%tuple(v.uv))
316 self.out_file.write("\tvertex3 %f %f %f;\n"%tuple(v.co))
317 self.out_file.write("};\n")
319 self.out_file.write("batch TRIANGLE_STRIP\n{\n\tindices")
322 self.out_file.write(" %u"%v.index)
325 self.out_file.write(";\n\tindices")
326 self.out_file.write(";\n};\n")
332 self.out_file.write("batch TRIANGLES\n{\n")
334 for i in range(2, len(f.verts)):
335 self.out_file.write("\tindices %u %u %u;\n"%(f.verts[0].index, f.verts[i-1].index, f.verts[i].index))
337 self.out_file.write("};\n")
343 Blender.Window.FileSelector(self.export, "Export MSP GL mesh", Blender.sys.makename(ext='.mesh'))
345 def export(self, fn):
347 #exp.use_degen_tris=False
352 if __name__=="__main__":