]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_mesh.py
Remove outdated includes
[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 export_mesh(self, context, mesh_or_obj, progress):
8                 from .mesh import Mesh, create_mesh_from_object
9
10                 if type(mesh_or_obj)==Mesh:
11                         mesh = mesh_or_obj
12                 else:
13                         progress.push_task("", 0.0, 0.9)
14                         mesh = create_mesh_from_object(context, mesh_or_obj, progress)
15                         progress.pop_task()
16
17                 from .datafile import Resource, Statement, Token
18                 resource = Resource(mesh.name+".mesh", "mesh")
19                 statements = resource.statements
20
21                 st = Statement("vertices", Token("NORMAL3"))
22                 if mesh.vertices[0].color:
23                         st.append(Token("COLOR4_UBYTE"))
24                 if mesh.uv_layers:
25                         for u in mesh.uv_layers:
26                                 size = str(len(u.uvs[0]))
27                                 if u.unit==0:
28                                         st.append(Token("TEXCOORD"+size))
29                                 else:
30                                         st.append(Token("TEXCOORD{}_{}".format(size, u.unit)))
31                         if mesh.tangent_vecs:
32                                 st.append(Token("TANGENT3"))
33                 if mesh.vertex_groups:
34                         st.append(Token("GROUP{}".format(mesh.max_groups_per_vertex)))
35                         st.append(Token("WEIGHT{}".format(mesh.max_groups_per_vertex)))
36                 st.append(Token("VERTEX3"))
37
38                 normal = None
39                 color = None
40                 uvs = [None]*len(mesh.uv_layers)
41                 tan = None
42                 group = None
43                 weight = None
44                 for v in mesh.vertices:
45                         if v.normal!=normal:
46                                 st.sub.append(Statement("normal", *v.normal))
47                                 normal = v.normal
48                         if v.color!=color:
49                                 st.sub.append(Statement("color", *v.color))
50                                 color = v.color
51                         for i, u in enumerate(mesh.uv_layers):
52                                 if v.uvs[i]!=uvs[i]:
53                                         if u.unit==0:
54                                                 st.sub.append(Statement("texcoord", *v.uvs[i]))
55                                         else:
56                                                 st.sub.append(Statement("multitexcoord", u.unit, *v.uvs[i]))
57                                         uvs[i] = v.uvs[i]
58                         if mesh.tangent_vecs:
59                                 if v.tan!=tan:
60                                         st.sub.append(Statement("tangent", *v.tan))
61                                         tan = v.tan
62                         if mesh.vertex_groups:
63                                 v_group = [g.group for g in v.groups]
64                                 v_weight = [g.weight for g in v.groups]
65                                 if v_group!=group:
66                                         st.sub.append(Statement("group", *v_group))
67                                         group = v_group
68                                 if v_weight!=weight:
69                                         st.sub.append(Statement("weight", *v_weight))
70                                         weight = v_weight
71                         st.sub.append(Statement("vertex", *v.co))
72
73                 statements.append(st)
74
75                 if mesh.use_strips:
76                         for s in mesh.vertex_sequence:
77                                 st = Statement("batch", Token("TRIANGLE_STRIP"))
78                                 for i in range(0, len(s), 32):
79                                         st.sub.append(Statement("indices", *(v.index for v in s[i:i+32])))
80                                 statements.append(st)
81                 else:
82                         st = Statement("batch", Token('TRIANGLES'))
83                         for f in mesh.faces:
84                                 st.sub.append(Statement("indices", *(v.index for v in f.vertices)))
85                         statements.append(st)
86
87                 if mesh.lines:
88                         st = Statement("batch", Token('LINES'))
89                         for l in mesh.lines:
90                                 st.sub.append(Statement("indices", *(v.index for v in l.vertices)))
91                         statements.append(st)
92
93                 if mesh.winding_test:
94                         statements.append(Statement("winding", Token('COUNTERCLOCKWISE')))
95
96                 progress.set_progress(1.0)
97
98                 return resource