7 def export_mesh(self, context, mesh_or_obj, progress):
8 from .mesh import Mesh, create_mesh_from_object
10 if type(mesh_or_obj)==Mesh:
13 progress.push_task("", 0.0, 0.9)
14 mesh = create_mesh_from_object(context, mesh_or_obj, progress)
17 from .datafile import Resource, Statement, Token
18 resource = Resource(mesh.name+".mesh", "mesh")
19 statements = resource.statements
21 st = Statement("vertices", Token("VERTEX3_FLOAT"))
23 if mesh.vertices[0].color:
24 st.append(Token("COLOR4_UBYTE"))
27 for u in mesh.uv_layers:
29 min_val = min(*u.uvs[0])
30 max_val = max(*u.uvs[1])
32 min_val = min(min_val, *c)
33 max_val = max(max_val, *c)
34 uv_type = "USHORT" if min_val>=0.0 and max_val<=1.0 else "FLOAT"
35 if uv_type=="FLOAT" and stride%4:
37 st.append(Token("PADDING{}".format(pad)))
40 st.append(Token("TEXCOORD{}_{}".format(size, uv_type)))
42 st.append(Token("TEXCOORD{}_{}_{}".format(size, u.unit, uv_type)))
43 stride += (2 if uv_type=="USHORT" else 4)*size
44 if mesh.vertex_groups:
45 st.append(Token("WEIGHT{}_USHORT".format(mesh.max_groups_per_vertex)))
46 st.append(Token("GROUP{}_UBYTE".format(mesh.max_groups_per_vertex)))
47 stride += 3*mesh.max_groups_per_vertex
48 st.append(Token("NORMAL3_BYTE"))
50 if mesh.uv_layers and mesh.tangent_vecs:
51 st.append(Token("TANGENT3_BYTE"))
55 st.append(Token("PADDING{}_UBYTE".format(pad)))
60 uvs = [None]*len(mesh.uv_layers)
64 for v in mesh.vertices:
66 st.sub.append(Statement("normal", *v.normal))
69 st.sub.append(Statement("color", *v.color))
71 for i, u in enumerate(mesh.uv_layers):
74 st.sub.append(Statement("texcoord", *v.uvs[i]))
76 st.sub.append(Statement("multitexcoord", u.unit, *v.uvs[i]))
80 st.sub.append(Statement("tangent", *v.tan))
82 if mesh.vertex_groups:
83 v_group = [g.group for g in v.groups]
84 v_weight = [g.weight for g in v.groups]
86 st.sub.append(Statement("group", *v_group))
89 st.sub.append(Statement("weight", *v_weight))
91 st.sub.append(Statement("vertex", *v.co))
96 for s in mesh.vertex_sequence:
97 st = Statement("batch", Token("TRIANGLE_STRIP"))
98 for i in range(0, len(s), 32):
99 st.sub.append(Statement("indices", *(v.index for v in s[i:i+32])))
100 statements.append(st)
102 st = Statement("batch", Token('TRIANGLES'))
104 st.sub.append(Statement("indices", *(v.index for v in f.vertices)))
105 statements.append(st)
108 st = Statement("batch", Token('LINES'))
110 st.sub.append(Statement("indices", *(v.index for v in l.vertices)))
111 statements.append(st)
113 if mesh.winding_test:
114 statements.append(Statement("winding", Token('COUNTERCLOCKWISE')))
116 progress.set_progress(1.0)