Mark datafile tokens as such
[libs/gl.git] / blender / io_mspgl / export_mesh.py
1 import itertools
2 import bpy
3 import mathutils
4
5 class MeshExporter:
6         def __init__(self):
7                 self.show_progress = True
8                 self.use_strips = True
9                 self.use_degen_tris = False
10
11         def join_strips(self, strips):
12                 big_strip = []
13
14                 for s in strips:
15                         if big_strip:
16                                 # Generate glue elements, ensuring that the next strip begins at
17                                 # an even position
18                                 glue = [big_strip[-1], s[0]]
19                                 if len(big_strip)%2:
20                                         glue += [s[0]]
21
22                                 big_strip += glue
23
24                         big_strip += s
25
26                 return big_strip
27
28         def export_to_file(self, context, out_fn):
29                 obj = context.active_object
30
31                 from .util import Progress
32
33                 progress = Progress(self.show_progress and context)
34                 progress.push_task("", 0.0, 0.95)
35                 resource = self.export_mesh(context, obj, progress)
36
37                 with open(out_fn, "w") as out_file:
38                         for s in resource.statements:
39                                 s.write_to_file(out_file)
40
41         def export_mesh(self, context, mesh_or_obj, progress):
42                 from .mesh import Mesh, create_mesh_from_object
43
44                 if type(mesh_or_obj)==Mesh:
45                         mesh = mesh_or_obj
46                 else:
47                         progress.push_task("", 0.0, 0.9)
48                         mesh = create_mesh_from_object(context, mesh_or_obj, progress)
49                         progress.pop_task()
50
51                 from .datafile import Resource, Statement, Token
52                 resource = Resource(mesh.name+".mesh")
53                 statements = resource.statements
54
55                 st = Statement("vertices", Token("NORMAL3"))
56                 if mesh.uv_layers:
57                         for u in mesh.uv_layers:
58                                 size = str(len(u.uvs[0]))
59                                 if u.unit==0:
60                                         st.append(Token("TEXCOORD"+size))
61                                 else:
62                                         st.append(Token("TEXCOORD{}_{}".format(size, u.unit)))
63                         if mesh.tbn_vecs:
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"))
69
70                 normal = None
71                 uvs = [None]*len(mesh.uv_layers)
72                 tan = None
73                 bino = None
74                 group = None
75                 for v in mesh.vertices:
76                         if v.normal!=normal:
77                                 st.sub.append(Statement("normal", *v.normal))
78                                 normal = v.normal
79                         for i, u in enumerate(mesh.uv_layers):
80                                 if v.uvs[i]!=uvs[i]:
81                                         if u.unit==0:
82                                                 st.sub.append(Statement("texcoord", *v.uvs[i]))
83                                         else:
84                                                 st.sub.append(Statement("multitexcoord", u.unit, *v.uvs[i]))
85                                         uvs[i] = v.uvs[i]
86                         if mesh.tbn_vecs:
87                                 if v.tan!=tan:
88                                         st.sub.append(Statement("tangent", *v.tan))
89                                         tan = v.tan
90                                 if v.bino!=bino:
91                                         st.sub.append(Statement("binormal", *v.bino))
92                                         bino = 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))
98                                 if group_attr!=group:
99                                         st.sub.append(Statement("attrib", 5, *group_attr))
100                                         group = group_attr
101                         st.sub.append(Statement("vertex", *v.co))
102
103                 statements.append(st)
104
105                 if self.use_strips:
106                         strips = mesh.vertex_sequence
107                         if self.use_degen_tris:
108                                 strips = [self.join_strips(strips)]
109
110                         for s in 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)
115                 else:
116                         st = Statement("batch", Token('TRIANGLES'))
117                         for f in mesh.faces:
118                                 st.sub.append(Statement("indices", *(v.index for v in f.vertices)))
119                         statements.append(st)
120
121                 if mesh.lines:
122                         st = Statement("batch", Token('LINES'))
123                         for l in mesh.lines:
124                                 st.sub.append(Statement("indices", *(v.index for v in l.vertices)))
125                         statements.append(st)
126
127                 if mesh.winding_test:
128                         statements.append(Statement("winding", Token('COUNTERCLOCKWISE')))
129
130                 progress.set_progress(1.0)
131
132                 return resource