2 def export_mesh(self, ctx, mesh_or_obj):
3 from .mesh import Mesh, create_mesh_from_object
5 if type(mesh_or_obj)==Mesh:
8 task = ctx.task("Preparing mesh", 0.9)
9 mesh = create_mesh_from_object(task, mesh_or_obj)
11 from .datafile import Resource, Statement, Token
12 resource = Resource(mesh.name+".mesh", "mesh")
13 statements = resource.statements
15 task = ctx.task("Creating statements", 1.0)
17 statements.append(Statement("winding", Token('COUNTERCLOCKWISE')))
19 st = Statement("vertices", Token("VERTEX3_FLOAT"))
21 if mesh.vertices[0].color:
22 st.append(Token("COLOR4_UBYTE"))
25 for u in mesh.uv_layers:
27 min_val = min(*u.uvs[0])
28 max_val = max(*u.uvs[1])
30 min_val = min(min_val, *c)
31 max_val = max(max_val, *c)
32 uv_type = "USHORT" if min_val>=0.0 and max_val<=1.0 else "FLOAT"
33 if uv_type=="FLOAT" and stride%4:
35 st.append(Token("PADDING{}".format(pad)))
38 st.append(Token("TEXCOORD{}_{}".format(size, uv_type)))
40 st.append(Token("TEXCOORD{}_{}_{}".format(size, u.unit, uv_type)))
41 stride += (2 if uv_type=="USHORT" else 4)*size
42 if mesh.vertex_groups:
43 st.append(Token("WEIGHT{}_USHORT".format(mesh.max_groups_per_vertex)))
44 st.append(Token("GROUP{}_UBYTE".format(mesh.max_groups_per_vertex)))
45 stride += 3*mesh.max_groups_per_vertex
46 st.append(Token("NORMAL3_BYTE"))
48 if mesh.uv_layers and mesh.tangent_vecs:
49 st.append(Token("TANGENT3_BYTE"))
53 st.append(Token("PADDING{}_UBYTE".format(pad)))
58 uvs = [None]*len(mesh.uv_layers)
62 for v in mesh.vertices:
64 st.sub.append(Statement("normal", *v.normal))
67 st.sub.append(Statement("color", *v.color))
69 for i, u in enumerate(mesh.uv_layers):
72 st.sub.append(Statement("texcoord", *v.uvs[i]))
74 st.sub.append(Statement("multitexcoord", u.unit, *v.uvs[i]))
78 st.sub.append(Statement("tangent", *v.tan))
80 if mesh.vertex_groups:
81 v_group = [g.group for g in v.groups]
82 v_weight = [g.weight for g in v.groups]
84 st.sub.append(Statement("group", *v_group))
87 st.sub.append(Statement("weight", *v_weight))
89 st.sub.append(Statement("vertex", *v.co))
94 for s in mesh.vertex_sequence:
95 st = Statement("batch", Token("TRIANGLE_STRIP"))
96 for i in range(0, len(s), 32):
97 st.sub.append(Statement("indices", *(v.index for v in s[i:i+32])))
100 st = Statement("batch", Token('TRIANGLES'))
102 st.sub.append(Statement("indices", *(v.index for v in f.vertices)))
103 statements.append(st)
106 st = Statement("batch", Token('LINES'))
108 st.sub.append(Statement("indices", *(v.index for v in l.vertices)))
109 statements.append(st)
111 task.set_progress(1.0)