From 712d77b12b4bfee6d1f2e7436247ade5405e8f90 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 7 Oct 2010 16:52:13 +0000 Subject: [PATCH] Blender exporter: add spaces around assignments --- mesh_export.py | 400 ++++++++++++++++++++++++------------------------- 1 file changed, 200 insertions(+), 200 deletions(-) diff --git a/mesh_export.py b/mesh_export.py index 7f59e671..1ed2dcde 100644 --- a/mesh_export.py +++ b/mesh_export.py @@ -19,14 +19,14 @@ def make_edge_key(i1, i2): class Edge: def __init__(self, me): if me.__class__==Edge: - self._medge=me._medge - self.v1=me.v1 - self.v2=me.v2 - self.smooth=me.smooth + self._medge = me._medge + self.v1 = me.v1 + self.v2 = me.v2 + self.smooth = me.smooth else: - self._medge=me - self.smooth=False - self.faces=[] + self._medge = me + self.smooth = False + self.faces = [] def __getattr__(self, attr): return getattr(self._medge, attr) @@ -38,9 +38,9 @@ class Edge: if len(self.faces)!=2: return - d=Blender.Mathutils.DotVecs(self.faces[0].no, self.faces[1].no) + d = Blender.Mathutils.DotVecs(self.faces[0].no, self.faces[1].no) if (d>limit and self.faces[0].smooth and self.faces[1].smooth) or d>0.999: - self.smooth=True + self.smooth = True def other_face(self, f): if f.index==self.faces[0].index: @@ -55,17 +55,17 @@ class Edge: class Vertex: def __init__(self, mv): if mv.__class__==Vertex: - self._mvert=mv._mvert - self.no=mv.no - self.uv=mv.uv + self._mvert = mv._mvert + self.no = mv.no + self.uv = mv.uv else: - self._mvert=mv - self.uv=None - self.orig_index=self._mvert.index - self.flag=False - self.faces=[] - self.tan=None - self.bino=None + self._mvert = mv + self.uv = None + self.orig_index = self._mvert.index + self.flag = False + self.faces = [] + self.tan = None + self.bino = None def __getattr__(self, attr): return getattr(self._mvert, attr) @@ -78,15 +78,15 @@ class Vertex: def __str__(self): return ""%(self.index, self.co[0], self.co[1], self.co[2], self.no[0], self.no[1], self.no[2]) - __repr__=__str__ + __repr__ = __str__ class Face: def __init__(self, mf): - self._mface=mf - self.edges=[] - self.verts=[v for v in mf.verts] - self.flag=False + self._mface = mf + self.edges = [] + self.verts = [v for v in mf.verts] + self.flag = False def __getattr__(self, attr): return getattr(self._mface, attr) @@ -99,20 +99,20 @@ class Face: def __str__(self): return ""%(self.index, " ".join([str(v.index) for v in self.verts])) - __repr__=__str__ + __repr__ = __str__ def pivot_vertices(self, reverse, *vt): - verts=self.verts[:] + verts = self.verts[:] if reverse: verts.reverse() - flags=[(v in vt) for v in verts] - l=len(verts) + flags = [(v in vt) for v in verts] + l = len(verts) for i in range(l): if flags[i] and not flags[(i+l-1)%l]: return verts[i:]+verts[:i] def get_edge(self, v1, v2): - key=make_edge_key(v1.index, v2.index) + key = make_edge_key(v1.index, v2.index) for e in self.edges: if e.key==key: return e @@ -121,55 +121,55 @@ class Face: class Line: def __init__(self, e): - self.edge=e - self.verts=[e.v1, e.v2] - self.flag=False + self.edge = e + self.verts = [e.v1, e.v2] + self.flag = False def __str__(self): return ""%(self.verts[0].index, self.verts[1].index) - __repr__=__str__ + __repr__ = __str__ class Mesh: def __init__(self, m): - self._mesh=m - self.verts=[Vertex(v) for v in m.verts] - self.faces=[Face(f) for f in m.faces] + self._mesh = m + self.verts = [Vertex(v) for v in m.verts] + self.faces = [Face(f) for f in m.faces] for f in self.faces: for i in range(len(f.verts)): - f.verts[i]=self.verts[f.verts[i].index] + f.verts[i] = self.verts[f.verts[i].index] f.verts[i].faces.append(f) - self.edges=dict([(e.key, Edge(e)) for e in m.edges]) + self.edges = dict([(e.key, Edge(e)) for e in m.edges]) for f in self.faces: for k in f.edge_keys: - e=self.edges[k] + e = self.edges[k] e.faces.append(self.faces[f.index]) f.edges.append(e) - self.lines=[Line(e) for e in self.edges.itervalues() if not e.faces] + self.lines = [Line(e) for e in self.edges.itervalues() if not e.faces] if m.mode&Blender.Mesh.Modes.AUTOSMOOTH: - smooth_limit=math.cos(m.degr*math.pi/180) + smooth_limit = math.cos(m.degr*math.pi/180) else: - smooth_limit=-1 + smooth_limit = -1 for e in self.edges.itervalues(): - e.v1=self.verts[e.v1.index] - e.v2=self.verts[e.v2.index] + e.v1 = self.verts[e.v1.index] + e.v2 = self.verts[e.v2.index] e.check_smooth(smooth_limit) def __getattr__(self, attr): return getattr(self._mesh, attr) def split_vertices(self, find_group_func, debug): - groups=[] + groups = [] for v in self.verts: for f in v.faces: - f.flag=False + f.flag = False - vg=[] + vg = [] for f in v.faces: if not f.flag: vg.append(find_group_func(v, f)) @@ -184,8 +184,8 @@ class Mesh: print "Vertex %s has %d groups"%(self.verts[i], len(groups[i])) for g in groups[i][1:]: - v=Vertex(self.verts[i]) - v.index=len(self.verts) + v = Vertex(self.verts[i]) + v.index = len(self.verts) self.verts.append(v) if debug: @@ -193,7 +193,7 @@ class Mesh: for f in g: for j in range(len(f.edges)): - e=f.edges[j] + e = f.edges[j] if e.v1!=self.verts[i] and e.v2!=self.verts[i]: continue @@ -202,102 +202,102 @@ class Mesh: print " Splitting edge %s with faces %s"%(e.key, e.faces) if e.other_face(f) not in g and len(e.faces)>=2: - k=e.faces.index(f) + k = e.faces.index(f) e.faces.remove(f) - e=Edge(e) - f.edges[j]=e + e = Edge(e) + f.edges[j] = e e.faces.append(f) else: del self.edges[e.key] if e.v1==self.verts[i]: - e.v1=v + e.v1 = v elif e.v2==self.verts[i]: - e.v2=v + e.v2 = v - e.key=make_edge_key(e.v1.index, e.v2.index) - self.edges[e.key]=e + e.key = make_edge_key(e.v1.index, e.v2.index) + self.edges[e.key] = e self.verts[i].faces.remove(f) - f.verts[f.verts.index(self.verts[i])]=v + f.verts[f.verts.index(self.verts[i])] = v v.faces.append(f) - def split_smooth(self, debug=False): + def split_smooth(self, debug = False): self.split_vertices(self.find_smooth_group, debug) - def split_uv(self, debug=False): + def split_uv(self, debug = False): self.split_vertices(self.find_uv_group, debug) def find_smooth_group(self, vert, face): - face.flag=True - queue=[face] + face.flag = True + queue = [face] for f in queue: for e in f.edges: - other=e.other_face(f) + other = e.other_face(f) #if not other or other.index not in face_indices: if other not in vert.faces: continue if e.smooth: if not other.flag: - other.flag=True + other.flag = True queue.append(other) return queue def find_uv_group(self, vert, face): - uv=face.uv[face.verts.index(vert)] - face.flag=True - group=[face] + uv = face.uv[face.verts.index(vert)] + face.flag = True + group = [face] for f in vert.faces: if not f.flag and f.uv[f.verts.index(vert)]==uv: - f.flag=True + f.flag = True group.append(f) return group def compute_normals(self): for v in self.verts: if v.faces: - v.no=Blender.Mathutils.Vector() + v.no = Blender.Mathutils.Vector() for f in v.faces: - v.no+=f.no + v.no += f.no v.no.normalize() else: # XXX Should use edges to compute normal - v.no=Blender.Mathutils.Vector(0, 0, 1) + v.no = Blender.Mathutils.Vector(0, 0, 1) def compute_uv(self): for v in self.verts: if v.faces: - v.uv=v.faces[0].uv[v.faces[0].verts.index(v)] + v.uv = v.faces[0].uv[v.faces[0].verts.index(v)] def compute_tbn(self): for v in self.verts: - v.tan=Blender.Mathutils.Vector() - v.bino=Blender.Mathutils.Vector() + v.tan = Blender.Mathutils.Vector() + v.bino = Blender.Mathutils.Vector() for f in v.faces: - fverts=f.pivot_vertices(False, v) - v1=fverts[1] - v2=fverts[-1] - du1=v1.uv[0]-v.uv[0] - du2=v2.uv[0]-v.uv[0] - dv1=v1.uv[1]-v.uv[1] - dv2=v2.uv[1]-v.uv[1] - div=du1*dv2-du2*dv1 - edge1=fverts[1].co-fverts[0].co - edge2=fverts[-1].co-fverts[0].co - v.tan+=(edge1*dv2-edge2*dv1)/div - v.bino+=(edge2*du1-edge1*du2)/div + fverts = f.pivot_vertices(False, v) + v1 = fverts[1] + v2 = fverts[-1] + du1 = v1.uv[0]-v.uv[0] + du2 = v2.uv[0]-v.uv[0] + dv1 = v1.uv[1]-v.uv[1] + dv2 = v2.uv[1]-v.uv[1] + div = du1*dv2-du2*dv1 + edge1 = fverts[1].co-fverts[0].co + edge2 = fverts[-1].co-fverts[0].co + v.tan += (edge1*dv2-edge2*dv1)/div + v.bino += (edge2*du1-edge1*du2)/div v.tan.normalize() v.bino.normalize() def create_strip(self, face, reverse, debug): - edge=None + edge = None for e in face.edges: - other=e.other_face(face) + other = e.other_face(face) if other and not other.flag: - edge=e + edge = e break if not edge: @@ -306,34 +306,34 @@ class Mesh: if debug: print "Starting strip from %s, edge %s, reverse=%s"%([v.index for v in face.verts], (edge.v1.index, edge.v2.index), reverse) - verts=face.pivot_vertices(reverse, edge.v1, edge.v2) + verts = face.pivot_vertices(reverse, edge.v1, edge.v2) if len(verts)==3: - result=[verts[-1], verts[0]] + result = [verts[-1], verts[0]] else: - result=[verts[-2], verts[-1]] + result = [verts[-2], verts[-1]] while 1: - verts=face.pivot_vertices(reverse, *result[-2:]) - k=len(result)%2 + verts = face.pivot_vertices(reverse, *result[-2:]) + k = len(result)%2 if debug: print " Adding %s"%face - face.flag=True + face.flag = True 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]) - edge=face.get_edge(*result[-2:]) + edge = face.get_edge(*result[-2:]) if debug: print " Next edge is %s"%(edge.key, ) - next=edge.other_face(face) + next = edge.other_face(face) if not next or next.flag: break - face=next + face = next if debug: print " %s"%[v.index for v in result] @@ -343,54 +343,54 @@ class Mesh: class Exporter: def __init__(self, fn): - self.filename=fn + self.filename = fn if fn==None: - self.out_file=sys.stdout + self.out_file = sys.stdout else: - self.out_file=file(fn, "w") - self.use_strips=True - self.use_degen_tris=True - self.optimize_locality=True - self.export_lines=True - self.tbn_vecs=False - self.debug=False - self.strip_debug=False - self.split_debug=False + self.out_file = file(fn, "w") + self.use_strips = True + self.use_degen_tris = True + self.optimize_locality = True + self.export_lines = True + self.tbn_vecs = False + self.debug = False + self.strip_debug = False + self.split_debug = False def get_locality(self, strip): - total=0 + total = 0 for i in range(1, len(strip)): if strip[i].index!=strip[i-1].index: - total+=1.0/(abs(strip[i].index-strip[i-1].index)) + total += 1.0/(abs(strip[i].index-strip[i-1].index)) return total/len(strip) def get_followers(self, strip): - result={} + result = {} for i in range(len(strip)-1): - v=strip[i] - n=strip[i+1] + v = strip[i] + n = strip[i+1] if v.index!=n.index: if v.index not in result: - result[v.index]={} + result[v.index] = {} if n.index not in result[v.index]: - result[v.index][n.index]=1 + result[v.index][n.index] = 1 else: - result[v.index][n.index]+=1 + result[v.index][n.index] += 1 return result def export(self): - scene=bpy.data.scenes.active + scene = bpy.data.scenes.active - obj=scene.objects.active + obj = scene.objects.active if obj.getType()!="Mesh": raise Exception, "Can only export Mesh data" - mesh=Blender.Mesh.New("export_tmp") + mesh = Blender.Mesh.New("export_tmp") mesh.getFromObject(obj) - mesh=Mesh(mesh) + mesh = Mesh(mesh) if self.debug: - ntris=sum([len(f.verts)-2 for f in mesh.faces]) + ntris = sum([len(f.verts)-2 for f in mesh.faces]) print "Starting with %d vertices, %d faces (%d triangles) and %d edges"%(len(mesh.verts), len(mesh.faces), ntris, len(mesh.edges)) mesh.split_smooth(self.split_debug) @@ -409,30 +409,30 @@ class Exporter: if self.tbn_vecs: mesh.compute_tbn() - strips=[] + strips = [] if self.use_strips: for f in mesh.faces: - f.flag=False + f.flag = False while 1: - best=5 - face=None + best = 5 + face = None for f in mesh.faces: if f.flag: continue - score=0 + score = 0 for e in f.edges: - other=e.other_face(f) + other = e.other_face(f) if other and not other.flag: - score+=1 + score += 1 if score>0 and scorebest and not mesh.verts[n].flag: - next=mesh.verts[n] - best=flw[n]+0.9/abs(vert.index-n) + next = mesh.verts[n] + best = flw[n]+0.9/abs(vert.index-n) if not next: for v in mesh.verts: if not v.flag: - next=v + next = v break if not next: break - vert=next + vert = next - mesh.verts=verts2 + mesh.verts = verts2 for i in range(len(mesh.verts)): - mesh.verts[i].index=i + mesh.verts[i].index = i if self.debug: print "Locality after optimization: "+" ".join(["%.3f"%self.get_locality(s) for s in strips]) @@ -516,41 +516,41 @@ class Exporter: if self.tbn_vecs: self.out_file.write("_ATTRIB33_ATTRIB34") self.out_file.write("_VERTEX3\n{\n") - norm=None - uv=None - tan=None - bino=None + norm = None + uv = None + tan = None + bino = None for v in mesh.verts: if v.no!=norm: self.out_file.write("\tnormal3 %f %f %f;\n"%tuple(v.no)) - norm=v.no + norm = v.no if v.uv!=uv: self.out_file.write("\ttexcoord2 %f %f;\n"%tuple(v.uv)) - uv=v.uv + uv = v.uv if v.tan!=tan: self.out_file.write("\tattrib3 3 %f %f %f;\n"%tuple(v.tan)) - tan=v.tan + tan = v.tan if v.bino!=bino: self.out_file.write("\tattrib3 4 %f %f %f;\n"%tuple(v.bino)) - bino=v.bino + bino = v.bino 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 + n = 0 for v in s: self.out_file.write(" %u"%v.index) - n+=1; + n += 1; if n%32==0: self.out_file.write(";\n\tindices") self.out_file.write(";\n};\n") - first=True + first = True for f in mesh.faces: if not f.flag: if first: self.out_file.write("batch TRIANGLES\n{\n") - first=False + first = False for i in range(2, len(f.verts)): self.out_file.write("\tindices %u %u %u;\n"%(f.verts[0].index, f.verts[i-1].index, f.verts[i].index)) if not first: @@ -565,19 +565,19 @@ class Exporter: class FrontEnd: def __init__(self): - self.config=Blender.Registry.GetKey('mspgl_export', True) or {} - self.temp_config=Blender.Registry.GetKey('mspgl_export_temp') or {} + self.config = Blender.Registry.GetKey('mspgl_export', True) or {} + self.temp_config = Blender.Registry.GetKey('mspgl_export_temp') or {} def run(self): - self.use_strips=Blender.Draw.Create(self.config.get('use_strips', True)) - self.use_degen_tris=Blender.Draw.Create(self.config.get('use_degen_tris', True)) - self.optimize_locality=Blender.Draw.Create(self.config.get('optimize_locality', True)) - self.export_lines=Blender.Draw.Create(self.config.get('export_lines', False)) - self.tbn_vecs=Blender.Draw.Create(self.config.get('tbn_vecs', False)) - self.debug=Blender.Draw.Create(self.config.get('debug', False)) - self.strip_debug=Blender.Draw.Create(self.config.get('strip_debug', False)) - self.split_debug=Blender.Draw.Create(self.config.get('split_debug', False)) - ret=Blender.Draw.PupBlock("Export MSP GL mesh", + self.use_strips = Blender.Draw.Create(self.config.get('use_strips', True)) + self.use_degen_tris = Blender.Draw.Create(self.config.get('use_degen_tris', True)) + self.optimize_locality = Blender.Draw.Create(self.config.get('optimize_locality', True)) + self.export_lines = Blender.Draw.Create(self.config.get('export_lines', False)) + self.tbn_vecs = Blender.Draw.Create(self.config.get('tbn_vecs', False)) + self.debug = Blender.Draw.Create(self.config.get('debug', False)) + self.strip_debug = Blender.Draw.Create(self.config.get('strip_debug', False)) + self.split_debug = Blender.Draw.Create(self.config.get('split_debug', False)) + ret = Blender.Draw.PupBlock("Export MSP GL mesh", [("Use strips", self.use_strips, "Generage OpenGL triangle strips"), ("Use degen tris", self.use_degen_tris, "Use degenerate triangles to combine triangle strips"), ("Optimize locality", self.optimize_locality), @@ -588,40 +588,40 @@ class FrontEnd: ("Debug strips", self.strip_debug), ("Debug splitting", self.split_debug)]) if ret: - dirname=self.temp_config.get("dirname", Blender.sys.dirname(Blender.Get("filename"))) - obj=bpy.data.scenes.active.objects.active + dirname = self.temp_config.get("dirname", Blender.sys.dirname(Blender.Get("filename"))) + obj = bpy.data.scenes.active.objects.active Blender.Window.FileSelector(self.export, "Export MSP GL mesh", "%s/%s.mesh"%(dirname, obj.name)) def draw(self): pass def export(self, fn): - self.config['use_strips']=self.use_strips.val - self.config['use_degen_tris']=self.use_degen_tris.val - self.config['optimize_locality']=self.optimize_locality.val - self.config['export_lines']=self.export_lines.val - self.config['tbn_vecs']=self.tbn_vecs.val - self.config['debug']=self.debug.val - self.config['strip_debug']=self.strip_debug.val - self.config['split_debug']=self.split_debug.val + self.config['use_strips'] = self.use_strips.val + self.config['use_degen_tris'] = self.use_degen_tris.val + self.config['optimize_locality'] = self.optimize_locality.val + self.config['export_lines'] = self.export_lines.val + self.config['tbn_vecs'] = self.tbn_vecs.val + self.config['debug'] = self.debug.val + self.config['strip_debug'] = self.strip_debug.val + self.config['split_debug'] = self.split_debug.val Blender.Registry.SetKey('mspgl_export', self.config, True) import os - self.temp_config["dirname"]=os.path.dirname(fn) + self.temp_config["dirname"] = os.path.dirname(fn) Blender.Registry.SetKey('mspgl_export_temp', self.temp_config) - exp=Exporter(fn) - exp.use_strips=self.use_strips.val - exp.use_degen_tris=self.use_degen_tris.val - exp.optimize_locality=self.optimize_locality.val - exp.export_lines=self.export_lines.val - exp.tbn_vecs=self.tbn_vecs.val - exp.debug=self.debug.val - exp.strip_debug=self.strip_debug.val - exp.split_debug=self.split_debug.val + exp = Exporter(fn) + exp.use_strips = self.use_strips.val + exp.use_degen_tris = self.use_degen_tris.val + exp.optimize_locality = self.optimize_locality.val + exp.export_lines = self.export_lines.val + exp.tbn_vecs = self.tbn_vecs.val + exp.debug = self.debug.val + exp.strip_debug = self.strip_debug.val + exp.split_debug = self.split_debug.val exp.export() if __name__=="__main__": - fe=FrontEnd() + fe = FrontEnd() fe.run() -- 2.45.2