8 self.show_progress = True
10 self.use_degen_tris = False
11 self.export_all = False
13 def join_strips(self, strips):
18 # Generate glue elements, ensuring that the next strip begins at
20 glue = [big_strip[-1], s[0]]
30 def export_to_file(self, context, out_fn):
32 objs = [o for o in context.selected_objects if o.type=="MESH"]
34 objs = [context.active_object]
36 from .util import Progress
38 path, base = os.path.split(out_fn)
39 base, ext = os.path.splitext(base)
41 progress = Progress(self.show_progress and context)
42 for i, obj in enumerate(objs):
44 out_fn = os.path.join(path, obj.data.name+ext)
46 progress.push_task_slice(obj.data.name, i, len(objs))
47 resource = self.export_mesh(context, obj, progress)
49 resource.write_to_file(out_fn)
52 def export_mesh(self, context, mesh_or_obj, progress):
53 from .mesh import Mesh, create_mesh_from_object
55 if type(mesh_or_obj)==Mesh:
58 progress.push_task("", 0.0, 0.9)
59 mesh = create_mesh_from_object(context, mesh_or_obj, progress)
62 from .datafile import Resource, Statement, Token
63 resource = Resource(mesh.name+".mesh", "mesh")
64 statements = resource.statements
66 st = Statement("vertices", Token("NORMAL3"))
67 if mesh.vertices[0].color:
68 st.append(Token("COLOR4_UBYTE"))
70 for u in mesh.uv_layers:
71 size = str(len(u.uvs[0]))
73 st.append(Token("TEXCOORD"+size))
75 st.append(Token("TEXCOORD{}_{}".format(size, u.unit)))
77 st.append(Token("TANGENT3"))
78 st.append(Token("BINORMAL3"))
79 if mesh.vertex_groups:
80 st.append(Token("GENERIC{}_0".format(mesh.max_groups_per_vertex*2)))
81 st.append(Token("VERTEX3"))
85 uvs = [None]*len(mesh.uv_layers)
89 for v in mesh.vertices:
91 st.sub.append(Statement("normal", *v.normal))
94 st.sub.append(Statement("color", *v.color))
96 for i, u in enumerate(mesh.uv_layers):
99 st.sub.append(Statement("texcoord", *v.uvs[i]))
101 st.sub.append(Statement("multitexcoord", u.unit, *v.uvs[i]))
105 st.sub.append(Statement("tangent", *v.tan))
108 st.sub.append(Statement("binormal", *v.bino))
110 if mesh.vertex_groups:
111 group_attr = [(group_index_map[g.group], g.weight*v.group_weight_scale) for g in v.groups[:mesh.max_groups_per_vertex]]
112 while len(group_attr)<mesh.max_groups_per_vertex:
113 group_attr.append((0, 0.0))
114 group_attr = list(itertools.chain(*group_attr))
115 if group_attr!=group:
116 st.sub.append(Statement("generic", 0, *group_attr))
118 st.sub.append(Statement("vertex", *v.co))
120 statements.append(st)
123 strips = mesh.vertex_sequence
124 if self.use_degen_tris:
125 strips = [self.join_strips(strips)]
128 st = Statement("batch", Token("TRIANGLE_STRIP"))
129 for i in range(0, len(s), 32):
130 st.sub.append(Statement("indices", *(v.index for v in s[i:i+32])))
131 statements.append(st)
133 st = Statement("batch", Token('TRIANGLES'))
135 st.sub.append(Statement("indices", *(v.index for v in f.vertices)))
136 statements.append(st)
139 st = Statement("batch", Token('LINES'))
141 st.sub.append(Statement("indices", *(v.index for v in l.vertices)))
142 statements.append(st)
144 if mesh.winding_test:
145 statements.append(Statement("winding", Token('COUNTERCLOCKWISE')))
147 progress.set_progress(1.0)