]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_mesh.py
Change default binding ranges to match lowest OpenGL standard
[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_BYTE"))
22                 if mesh.uv_layers and mesh.tangent_vecs:
23                         st.append(Token("TANGENT3_BYTE"))
24                 if mesh.vertices[0].color:
25                         st.append(Token("COLOR4_UBYTE"))
26                 if mesh.uv_layers:
27                         for u in mesh.uv_layers:
28                                 size = str(len(u.uvs[0]))
29                                 min_val = min(*u.uvs[0])
30                                 max_val = max(*u.uvs[1])
31                                 for c in u.uvs:
32                                         min_val = min(min_val, *c)
33                                         max_val = max(max_val, *c)
34                                 uv_type = "USHORT" if min_val>=0.0 and max_val<=1.0 else "FLOAT"
35                                 if u.unit==0:
36                                         st.append(Token("TEXCOORD{}_{}".format(size, uv_type)))
37                                 else:
38                                         st.append(Token("TEXCOORD{}_{}_{}".format(size, u.unit, uv_type)))
39                 if mesh.vertex_groups:
40                         st.append(Token("GROUP{}_UBYTE".format(mesh.max_groups_per_vertex)))
41                         st.append(Token("WEIGHT{}_USHORT".format(mesh.max_groups_per_vertex)))
42                 st.append(Token("VERTEX3_FLOAT"))
43
44                 normal = None
45                 color = None
46                 uvs = [None]*len(mesh.uv_layers)
47                 tan = None
48                 group = None
49                 weight = None
50                 for v in mesh.vertices:
51                         if v.normal!=normal:
52                                 st.sub.append(Statement("normal", *v.normal))
53                                 normal = v.normal
54                         if v.color!=color:
55                                 st.sub.append(Statement("color", *v.color))
56                                 color = v.color
57                         for i, u in enumerate(mesh.uv_layers):
58                                 if v.uvs[i]!=uvs[i]:
59                                         if u.unit==0:
60                                                 st.sub.append(Statement("texcoord", *v.uvs[i]))
61                                         else:
62                                                 st.sub.append(Statement("multitexcoord", u.unit, *v.uvs[i]))
63                                         uvs[i] = v.uvs[i]
64                         if mesh.tangent_vecs:
65                                 if v.tan!=tan:
66                                         st.sub.append(Statement("tangent", *v.tan))
67                                         tan = v.tan
68                         if mesh.vertex_groups:
69                                 v_group = [g.group for g in v.groups]
70                                 v_weight = [g.weight for g in v.groups]
71                                 if v_group!=group:
72                                         st.sub.append(Statement("group", *v_group))
73                                         group = v_group
74                                 if v_weight!=weight:
75                                         st.sub.append(Statement("weight", *v_weight))
76                                         weight = v_weight
77                         st.sub.append(Statement("vertex", *v.co))
78
79                 statements.append(st)
80
81                 if mesh.use_strips:
82                         for s in mesh.vertex_sequence:
83                                 st = Statement("batch", Token("TRIANGLE_STRIP"))
84                                 for i in range(0, len(s), 32):
85                                         st.sub.append(Statement("indices", *(v.index for v in s[i:i+32])))
86                                 statements.append(st)
87                 else:
88                         st = Statement("batch", Token('TRIANGLES'))
89                         for f in mesh.faces:
90                                 st.sub.append(Statement("indices", *(v.index for v in f.vertices)))
91                         statements.append(st)
92
93                 if mesh.lines:
94                         st = Statement("batch", Token('LINES'))
95                         for l in mesh.lines:
96                                 st.sub.append(Statement("indices", *(v.index for v in l.vertices)))
97                         statements.append(st)
98
99                 if mesh.winding_test:
100                         statements.append(Statement("winding", Token('COUNTERCLOCKWISE')))
101
102                 progress.set_progress(1.0)
103
104                 return resource