]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_mesh.py
Fix and improve vertex group support
[libs/gl.git] / blender / io_mspgl / export_mesh.py
1 import itertools
2 import os
3 import bpy
4 import mathutils
5
6 class MeshExporter:
7         def __init__(self):
8                 self.show_progress = True
9                 self.use_strips = True
10                 self.use_degen_tris = False
11                 self.export_all = False
12
13         def join_strips(self, strips):
14                 big_strip = []
15
16                 for s in strips:
17                         if big_strip:
18                                 # Generate glue elements, ensuring that the next strip begins at
19                                 # an even position
20                                 glue = [big_strip[-1], s[0]]
21                                 if len(big_strip)%2:
22                                         glue += [s[0]]
23
24                                 big_strip += glue
25
26                         big_strip += s
27
28                 return big_strip
29
30         def export_to_file(self, context, out_fn):
31                 if self.export_all:
32                         objs = [o for o in context.selected_objects if o.type=="MESH"]
33                 else:
34                         objs = [context.active_object]
35
36                 from .util import Progress
37
38                 path, base = os.path.split(out_fn)
39                 base, ext = os.path.splitext(base)
40
41                 progress = Progress(self.show_progress and context)
42                 for i, obj in enumerate(objs):
43                         if self.export_all:
44                                 out_fn = os.path.join(path, obj.data.name+ext)
45
46                         progress.push_task_slice(obj.data.name, i, len(objs))
47                         resource = self.export_mesh(context, obj, progress)
48
49                         resource.write_to_file(out_fn)
50                         progress.pop_task()
51
52         def export_mesh(self, context, mesh_or_obj, progress):
53                 from .mesh import Mesh, create_mesh_from_object
54
55                 if type(mesh_or_obj)==Mesh:
56                         mesh = mesh_or_obj
57                 else:
58                         progress.push_task("", 0.0, 0.9)
59                         mesh = create_mesh_from_object(context, mesh_or_obj, progress)
60                         progress.pop_task()
61
62                 from .datafile import Resource, Statement, Token
63                 resource = Resource(mesh.name+".mesh", "mesh")
64                 statements = resource.statements
65
66                 st = Statement("vertices", Token("NORMAL3"))
67                 if mesh.vertices[0].color:
68                         st.append(Token("COLOR4_UBYTE"))
69                 if mesh.uv_layers:
70                         for u in mesh.uv_layers:
71                                 size = str(len(u.uvs[0]))
72                                 if u.unit==0:
73                                         st.append(Token("TEXCOORD"+size))
74                                 else:
75                                         st.append(Token("TEXCOORD{}_{}".format(size, u.unit)))
76                         if mesh.tbn_vecs:
77                                 st.append(Token("TANGENT3"))
78                                 st.append(Token("BINORMAL3"))
79                 if mesh.vertex_groups:
80                         st.append(Token("GROUP{}".format(mesh.max_groups_per_vertex)))
81                         st.append(Token("WEIGHT{}".format(mesh.max_groups_per_vertex)))
82                 st.append(Token("VERTEX3"))
83
84                 normal = None
85                 color = None
86                 uvs = [None]*len(mesh.uv_layers)
87                 tan = None
88                 bino = None
89                 group = None
90                 weight = None
91                 for v in mesh.vertices:
92                         if v.normal!=normal:
93                                 st.sub.append(Statement("normal", *v.normal))
94                                 normal = v.normal
95                         if v.color!=color:
96                                 st.sub.append(Statement("color", *v.color))
97                                 color = v.color
98                         for i, u in enumerate(mesh.uv_layers):
99                                 if v.uvs[i]!=uvs[i]:
100                                         if u.unit==0:
101                                                 st.sub.append(Statement("texcoord", *v.uvs[i]))
102                                         else:
103                                                 st.sub.append(Statement("multitexcoord", u.unit, *v.uvs[i]))
104                                         uvs[i] = v.uvs[i]
105                         if mesh.tbn_vecs:
106                                 if v.tan!=tan:
107                                         st.sub.append(Statement("tangent", *v.tan))
108                                         tan = v.tan
109                                 if v.bino!=bino:
110                                         st.sub.append(Statement("binormal", *v.bino))
111                                         bino = v.bino
112                         if mesh.vertex_groups:
113                                 v_group = [g.group for g in v.groups]
114                                 v_weight = [g.weight for g in v.groups]
115                                 if v_group!=group:
116                                         st.sub.append(Statement("group", *v_group))
117                                         group = v_group
118                                 if v_weight!=weight:
119                                         st.sub.append(Statement("weight", *v_weight))
120                                         weight = v_weight
121                         st.sub.append(Statement("vertex", *v.co))
122
123                 statements.append(st)
124
125                 if self.use_strips:
126                         strips = mesh.vertex_sequence
127                         if self.use_degen_tris:
128                                 strips = [self.join_strips(strips)]
129
130                         for s in strips:
131                                 st = Statement("batch", Token("TRIANGLE_STRIP"))
132                                 for i in range(0, len(s), 32):
133                                         st.sub.append(Statement("indices", *(v.index for v in s[i:i+32])))
134                                 statements.append(st)
135                 else:
136                         st = Statement("batch", Token('TRIANGLES'))
137                         for f in mesh.faces:
138                                 st.sub.append(Statement("indices", *(v.index for v in f.vertices)))
139                         statements.append(st)
140
141                 if mesh.lines:
142                         st = Statement("batch", Token('LINES'))
143                         for l in mesh.lines:
144                                 st.sub.append(Statement("indices", *(v.index for v in l.vertices)))
145                         statements.append(st)
146
147                 if mesh.winding_test:
148                         statements.append(Statement("winding", Token('COUNTERCLOCKWISE')))
149
150                 progress.set_progress(1.0)
151
152                 return resource