]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_mesh.py
Check the flat qualifier from the correct member
[libs/gl.git] / blender / io_mspgl / export_mesh.py
1 class MeshExporter:
2         def export_mesh(self, ctx, mesh_or_obj):
3                 from .mesh import Mesh, create_mesh_from_object
4
5                 if type(mesh_or_obj)==Mesh:
6                         mesh = mesh_or_obj
7                 else:
8                         task = ctx.task("Preparing mesh", 0.9)
9                         mesh = create_mesh_from_object(task, mesh_or_obj)
10
11                 from .datafile import Resource, Statement, Token
12                 resource = Resource(mesh.name+".mesh", "mesh")
13                 statements = resource.statements
14
15                 task = ctx.task("Creating statements", 1.0)
16
17                 statements.append(Statement("winding", Token('COUNTERCLOCKWISE')))
18
19                 st = Statement("vertices", Token("VERTEX3_FLOAT"))
20                 stride = 12
21                 if mesh.vertices[0].color:
22                         st.append(Token("COLOR4_UBYTE"))
23                         stride += 4
24                 if mesh.uv_layers:
25                         for u in mesh.uv_layers:
26                                 size = len(u.uvs[0])
27                                 min_val = min(*u.uvs[0])
28                                 max_val = max(*u.uvs[1])
29                                 for c in u.uvs:
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:
34                                         pad = 4-stride%4
35                                         st.append(Token("PADDING{}".format(pad)))
36                                         stride += pad
37                                 if u.unit==0:
38                                         st.append(Token("TEXCOORD{}_{}".format(size, uv_type)))
39                                 else:
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"))
47                 stride += 3
48                 if mesh.uv_layers and mesh.tangent_vecs:
49                         st.append(Token("TANGENT3_BYTE"))
50                         stride += 3
51                 if stride%4:
52                         pad = 4-stride%4
53                         st.append(Token("PADDING{}_UBYTE".format(pad)))
54                         stride += pad
55
56                 normal = None
57                 color = None
58                 uvs = [None]*len(mesh.uv_layers)
59                 tan = None
60                 group = None
61                 weight = None
62                 for v in mesh.vertices:
63                         if v.normal!=normal:
64                                 st.sub.append(Statement("normal", *v.normal))
65                                 normal = v.normal
66                         if v.color!=color:
67                                 st.sub.append(Statement("color", *v.color))
68                                 color = v.color
69                         for i, u in enumerate(mesh.uv_layers):
70                                 if v.uvs[i]!=uvs[i]:
71                                         if u.unit==0:
72                                                 st.sub.append(Statement("texcoord", *v.uvs[i]))
73                                         else:
74                                                 st.sub.append(Statement("multitexcoord", u.unit, *v.uvs[i]))
75                                         uvs[i] = v.uvs[i]
76                         if mesh.tangent_vecs:
77                                 if v.tan!=tan:
78                                         st.sub.append(Statement("tangent", *v.tan))
79                                         tan = 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]
83                                 if v_group!=group:
84                                         st.sub.append(Statement("group", *v_group))
85                                         group = v_group
86                                 if v_weight!=weight:
87                                         st.sub.append(Statement("weight", *v_weight))
88                                         weight = v_weight
89                         st.sub.append(Statement("vertex", *v.co))
90
91                 statements.append(st)
92
93                 for b in mesh.batches:
94                         st = Statement("batch", Token(b.primitive_type))
95                         if b.primitive_type=="PATCHES":
96                                 st.sub.append(Statement("patch_size", b.patch_size))
97                         for i in range(0, len(b.vertices), 32):
98                                 st.sub.append(Statement("indices", *(v.index for v in b.vertices[i:i+32])))
99                         statements.append(st)
100
101                 task.set_progress(1.0)
102
103                 return resource