7 self.show_progress = True
9 self.use_degen_tris = False
11 def join_strips(self, strips):
16 # Generate glue elements, ensuring that the next strip begins at
18 glue = [big_strip[-1], s[0]]
28 def export_to_file(self, context, out_fn):
29 obj = context.active_object
31 from .util import Progress
33 progress = Progress(self.show_progress and context)
34 progress.push_task("", 0.0, 0.95)
35 resource = self.export_mesh(context, obj, progress)
37 with open(out_fn, "w") as out_file:
38 for s in resource.statements:
39 s.write_to_file(out_file)
41 def export_mesh(self, context, mesh_or_obj, progress):
42 from .mesh import Mesh, create_mesh_from_object
44 if type(mesh_or_obj)==Mesh:
47 progress.push_task("", 0.0, 0.9)
48 mesh = create_mesh_from_object(context, mesh_or_obj, progress)
51 from .datafile import Resource, Statement, Token
52 resource = Resource(mesh.name+".mesh")
53 statements = resource.statements
55 st = Statement("vertices", Token("NORMAL3"))
57 for u in mesh.uv_layers:
58 size = str(len(u.uvs[0]))
60 st.append(Token("TEXCOORD"+size))
62 st.append(Token("TEXCOORD{}_{}".format(size, u.unit)))
64 st.append(Token("TANGENT3"))
65 st.append(Token("BINORMAL3"))
66 if mesh.vertex_groups:
67 st.append(Token("ATTRIB{}_5".format(mesh.max_groups_per_vertex*2)))
68 st.append(Token("VERTEX3"))
71 uvs = [None]*len(mesh.uv_layers)
75 for v in mesh.vertices:
77 st.sub.append(Statement("normal", *v.normal))
79 for i, u in enumerate(mesh.uv_layers):
82 st.sub.append(Statement("texcoord", *v.uvs[i]))
84 st.sub.append(Statement("multitexcoord", u.unit, *v.uvs[i]))
88 st.sub.append(Statement("tangent", *v.tan))
91 st.sub.append(Statement("binormal", *v.bino))
93 if mesh.vertex_groups:
94 group_attr = [(group_index_map[g.group], g.weight*v.group_weight_scale) for g in v.groups[:mesh.max_groups_per_vertex]]
95 while len(group_attr)<mesh.max_groups_per_vertex:
96 group_attr.append((0, 0.0))
97 group_attr = list(itertools.chain(*group_attr))
99 st.sub.append(Statement("attrib", 5, *group_attr))
101 st.sub.append(Statement("vertex", *v.co))
103 statements.append(st)
106 strips = mesh.vertex_sequence
107 if self.use_degen_tris:
108 strips = [self.join_strips(strips)]
111 st = Statement("batch", Token("TRIANGLE_STRIP"))
112 for i in range(0, len(s), 32):
113 st.sub.append(Statement("indices", *(v.index for v in s[i:i+32])))
114 statements.append(st)
116 st = Statement("batch", Token('TRIANGLES'))
118 st.sub.append(Statement("indices", *(v.index for v in f.vertices)))
119 statements.append(st)
122 st = Statement("batch", Token('LINES'))
124 st.sub.append(Statement("indices", *(v.index for v in l.vertices)))
125 statements.append(st)
127 if mesh.winding_test:
128 statements.append(Statement("winding", Token('COUNTERCLOCKWISE')))
130 progress.set_progress(1.0)