]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_mesh.py
Rewrite triangle strip generation in the Blender exporter
[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                 self.material_tex = False
11
12         def join_strips(self, strips):
13                 big_strip = []
14
15                 for s in strips:
16                         if big_strip:
17                                 # Generate glue elements, ensuring that the next strip begins at
18                                 # an even position
19                                 glue = [big_strip[-1], s[0]]
20                                 if len(big_strip)%2:
21                                         glue += [s[0]]
22
23                                 big_strip += glue
24
25                         big_strip += s
26
27                 return big_strip
28
29         def export(self, context, out_file, obj=None, progress=None):
30                 if obj is None:
31                         obj = context.active_object
32
33                 from .mesh import create_mesh_from_object
34                 from .util import Progress
35
36                 if not progress:
37                         progress = Progress(self.show_progress and context)
38                 progress.push_task("", 0.0, 0.9)
39
40                 mesh = create_mesh_from_object(context, obj, progress)
41
42                 strips = []
43                 loose = mesh.faces
44                 if self.use_strips:
45                         strips = mesh.vertex_sequence
46                         if self.use_degen_tris:
47                                 strips = [self.join_strips(strips)]
48                         loose = []
49
50                 progress.set_task("Writing file", 0.9, 1.0)
51
52                 from .outfile import open_output
53                 out_file = open_output(out_file)
54
55                 fmt = ["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                                         fmt.append("TEXCOORD"+size)
61                                 else:
62                                         fmt.append("TEXCOORD%s_%d"%(size, u.unit))
63                         if mesh.tbn_vecs:
64                                 fmt += ["TANGENT3", "BINORMAL3"]
65                 if mesh.vertex_groups:
66                         fmt.append("ATTRIB%d_5"%(mesh.max_groups_per_vertex*2))
67                 fmt.append("VERTEX3")
68                 out_file.begin("vertices", *fmt)
69                 normal = None
70                 uvs = {}
71                 tan = None
72                 bino = None
73                 group = None
74                 for v in mesh.vertices:
75                         if v.normal!=normal:
76                                 out_file.write("normal3", *v.normal)
77                                 normal = v.normal
78                         for i, u in enumerate(mesh.uv_layers):
79                                 if v.uvs[i]!=uvs.get(i):
80                                         size = str(len(v.uvs[i]))
81                                         if u.unit==0:
82                                                 out_file.write("texcoord"+size, *v.uvs[i])
83                                         else:
84                                                 out_file.write("multitexcoord"+size, u.unit, *v.uvs[i])
85                                         uvs[i] = v.uvs[i]
86                         if mesh.tbn_vecs:
87                                 if v.tan!=tan:
88                                         out_file.write("tangent3", *v.tan)
89                                         tan = v.tan
90                                 if v.bino!=bino:
91                                         out_file.write("binormal3", *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                                         out_file.write("attrib%d"%len(group_attr), 5, *group_attr)
100                                         group = group_attr
101                         out_file.write("vertex3", *v.co)
102                 out_file.end()
103                 for s in strips:
104                         out_file.begin("batch", "TRIANGLE_STRIP")
105                         indices = []
106                         n = 0
107                         for v in s:
108                                 indices.append(v.index)
109                                 if len(indices)>=32:
110                                         out_file.write("indices", *indices)
111                                         indices = []
112                         if indices:
113                                 out_file.write("indices", *indices)
114                         out_file.end()
115
116                 if loose:
117                         out_file.begin("batch", "TRIANGLES")
118                         for f in loose:
119                                 for i in range(2, len(f.vertices)):
120                                         out_file.write("indices", f.vertices[0].index, f.vertices[i-1].index, f.vertices[i].index)
121                         out_file.end()
122
123                 if mesh.lines:
124                         out_file.begin("batch", "LINES")
125                         for l in mesh.lines:
126                                 out_file.write("indices", l.vertices[0].index, l.vertices[1].index)
127                         out_file.end()
128
129                 if mesh.winding_test:
130                         out_file.write("winding", "COUNTERCLOCKWISE")
131
132                 progress.pop_task()
133
134                 return mesh