+ def begin(self, kwd, *params):
+ i = '\t'*self.indent
+ self.file.write("%s%s\n%s{\n"%(i, self.make(kwd, *params), i))
+ self.indent += 1
+
+ def end(self):
+ self.indent -= 1
+ self.file.write("%s};\n"%('\t'*self.indent))
+
+
+class Exporter:
+ def __init__(self, fn):
+ self.out_file = OutFile(fn)
+ self.use_strips = True
+ self.use_degen_tris = True
+ self.max_strip_len = 1024
+ self.optimize_cache = False
+ self.cache_size = 64
+ self.export_lines = True
+ self.tbn_vecs = False
+ self.compound = False
+ self.object = False
+ self.material_tex = False
+ self.debug = False
+ self.strip_debug = False
+ self.split_debug = False
+
+ def stripify(self, mesh):
+ for f in mesh.faces:
+ f.flag = False
+
+ faces_done = 0
+ strips = []
+ loose = []
+
+ cache = None
+ if self.optimize_cache:
+ cache = VertexCache(self.cache_size)
+
+ island = []
+ island_strips = []
+ while 1:
+ if not island:
+ queue = []
+ for f in mesh.faces:
+ if not f.flag:
+ f.flag = True
+ queue.append(f)
+ break
+
+ if not queue:
+ break
+
+ while queue:
+ f = queue[0]
+ del queue[0]
+ island.append(f)
+
+ for e in f.edges:
+ other = e.other_face(f)
+ if other and not other.flag:
+ other.flag = True
+ queue.append(other)
+
+ for f in island:
+ f.flag = False
+
+ best = 5
+ face = None
+ for f in island:
+ if f.flag:
+ continue
+ score = 0
+ for e in f.edges:
+ other = e.other_face(f)
+ if other and not other.flag:
+ score += 1
+ if score>0 and score<best:
+ face = f
+ best = score
+
+ if not face:
+ while island_strips:
+ best = 0
+ if cache:
+ best_hits = 0
+ for i in range(len(island_strips)):
+ hits = cache.test_strip(island_strips[i])
+ if hits>best_hits:
+ best = i
+ best_hits = hits
+
+ s = island_strips[best]
+ del island_strips[best]
+ strips.append(s)
+
+ if cache:
+ cache.fetch_strip(s)
+
+ faces_done += len(island)
+ set_progress(float(faces_done)/len(mesh.faces))
+
+ loose += [f for f in island if not f.flag]
+ for f in island:
+ f.flag = True
+
+ island = []
+ island_strips = []
+ continue
+
+ strip = mesh.create_strip(face, self.max_strip_len, self.strip_debug)
+ if strip:
+ island_strips.append(strip)
+
+ if self.debug:
+ print "%d strips:"%len(strips)
+ for i in range(len(strips)):
+ print " %d: %d indices"%(i, len(strips[i]))
+ print "%d loose faces"%len(loose)
+ nind = sum([len(s) for s in strips])+sum([len(f.verts) for f in loose])
+ print "%d indices total"%nind
+
+ if cache:
+ cache = VertexCache(self.cache_size)
+ total_hits = 0
+
+ if self.use_degen_tris and strips:
+ big_strip = []
+
+ for s in strips:
+ if big_strip:
+ glue = [big_strip[-1], s[0]]
+ if len(big_strip)%2:
+ glue += [s[0]]
+
+ big_strip += glue
+ if cache:
+ total_hits += cache.fetch_strip(glue)
+
+ big_strip += s
+ if cache:
+ total_hits += cache.fetch_strip(s)
+
+ for f in loose:
+ if len(big_strip)%2:
+ order = (-1, -2, 0, 1)
+ else:
+ order = (0, 1, -1, -2)
+ verts = [f.verts[i] for i in order[:len(f.verts)]]
+
+ if big_strip:
+ glue = [big_strip[-1], verts[0]]
+ big_strip += glue
+ if cache:
+ total_hits += cache.fetch_strip(glue)
+
+ big_strip += verts
+ if cache:
+ total_hits += cache.fetch_strip(verts)
+
+ strips = [big_strip]
+ loose = []
+
+ if self.debug:
+ nind = len(big_strip)
+ print "Big strip has %d indices"%nind
+ if self.optimize_cache:
+ print "%d cache hits"%total_hits
+
+ if self.debug:
+ ntris = sum([len(f.verts)-2 for f in mesh.faces])
+ print "%.2f indices per triangle"%(float(nind)/max(ntris, 1))
+
+ return strips, loose
+
+ def export(self):
+ scene = bpy.data.scenes.active
+
+ objs = Blender.Object.GetSelected()
+ if not objs:
+ raise Exception, "Nothing to export"
+ for o in objs:
+ if o.getType()!="Mesh":
+ raise Exception, "Can only export Mesh data"
+
+ Blender.Window.DrawProgressBar(0.0, "Preparing");
+
+ mesh = Blender.Mesh.New("export_tmp")
+ mesh.getFromObject(objs[0])
+ mesh = Mesh(mesh)
+ if self.compound:
+ # Must keep a ref to each Blender mesh
+ bmeshes = []
+ for o in objs[1:]:
+ bmesh = Blender.Mesh.New("export_tmp")
+ bmesh.getFromObject(o)
+ bmeshes.append(bmesh)
+ mesh.splice(Mesh(bmesh))