]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_mesh.py
Some cleanups to exporter code
[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(self, context, out_file, obj=None, progress=None):
29                 if obj is None:
30                         obj = context.active_object
31
32                 from .mesh import create_mesh_from_object
33                 from .util import Progress
34
35                 if not progress:
36                         progress = Progress(self.show_progress and context)
37                 progress.push_task("", 0.0, 0.9)
38
39                 mesh = create_mesh_from_object(context, obj, progress)
40
41                 strips = []
42                 loose = mesh.faces
43                 if self.use_strips:
44                         strips = mesh.vertex_sequence
45                         if self.use_degen_tris:
46                                 strips = [self.join_strips(strips)]
47                         loose = []
48
49                 progress.set_task("Writing file", 0.9, 1.0)
50
51                 from .outfile import open_output
52                 out_file = open_output(out_file)
53
54                 fmt = ["NORMAL3"]
55                 if mesh.uv_layers:
56                         for u in mesh.uv_layers:
57                                 size = str(len(u.uvs[0]))
58                                 if u.unit==0:
59                                         fmt.append("TEXCOORD"+size)
60                                 else:
61                                         fmt.append("TEXCOORD%s_%d"%(size, u.unit))
62                         if mesh.tbn_vecs:
63                                 fmt += ["TANGENT3", "BINORMAL3"]
64                 if mesh.vertex_groups:
65                         fmt.append("ATTRIB%d_5"%(mesh.max_groups_per_vertex*2))
66                 fmt.append("VERTEX3")
67                 out_file.begin("vertices", *fmt)
68                 normal = None
69                 uvs = {}
70                 tan = None
71                 bino = None
72                 group = None
73                 for v in mesh.vertices:
74                         if v.normal!=normal:
75                                 out_file.write("normal3", *v.normal)
76                                 normal = v.normal
77                         for i, u in enumerate(mesh.uv_layers):
78                                 if v.uvs[i]!=uvs.get(i):
79                                         size = str(len(v.uvs[i]))
80                                         if u.unit==0:
81                                                 out_file.write("texcoord"+size, *v.uvs[i])
82                                         else:
83                                                 out_file.write("multitexcoord"+size, u.unit, *v.uvs[i])
84                                         uvs[i] = v.uvs[i]
85                         if mesh.tbn_vecs:
86                                 if v.tan!=tan:
87                                         out_file.write("tangent3", *v.tan)
88                                         tan = v.tan
89                                 if v.bino!=bino:
90                                         out_file.write("binormal3", *v.bino)
91                                         bino = v.bino
92                         if mesh.vertex_groups:
93                                 group_attr = [(group_index_map[g.group], g.weight*v.group_weight_scale) for g in v.groups[:mesh.max_groups_per_vertex]]
94                                 while len(group_attr)<mesh.max_groups_per_vertex:
95                                         group_attr.append((0, 0.0))
96                                 group_attr = list(itertools.chain(*group_attr))
97                                 if group_attr!=group:
98                                         out_file.write("attrib%d"%len(group_attr), 5, *group_attr)
99                                         group = group_attr
100                         out_file.write("vertex3", *v.co)
101                 out_file.end()
102                 for s in strips:
103                         out_file.begin("batch", "TRIANGLE_STRIP")
104                         indices = []
105                         n = 0
106                         for v in s:
107                                 indices.append(v.index)
108                                 if len(indices)>=32:
109                                         out_file.write("indices", *indices)
110                                         indices = []
111                         if indices:
112                                 out_file.write("indices", *indices)
113                         out_file.end()
114
115                 if loose:
116                         out_file.begin("batch", "TRIANGLES")
117                         for f in loose:
118                                 for i in range(2, len(f.vertices)):
119                                         out_file.write("indices", f.vertices[0].index, f.vertices[i-1].index, f.vertices[i].index)
120                         out_file.end()
121
122                 if mesh.lines:
123                         out_file.begin("batch", "LINES")
124                         for l in mesh.lines:
125                                 out_file.write("indices", l.vertices[0].index, l.vertices[1].index)
126                         out_file.end()
127
128                 if mesh.winding_test:
129                         out_file.write("winding", "COUNTERCLOCKWISE")
130
131                 progress.pop_task()
132
133                 return mesh