]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
Raise an exception if the TBN UV layer is not found during export
[libs/gl.git] / blender / io_mspgl / export_object.py
1 import os
2 import mathutils
3
4 class ObjectExporter:
5         def __init__(self):
6                 self.show_progress = True
7                 self.use_strips = True
8                 self.use_degen_tris = False
9                 self.use_textures = True
10                 self.single_file = True
11                 self.shared_resources = True
12                 self.export_lods = True
13
14         def compute_bounding_sphere(self, obj):
15                 p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0]
16                 p2 = max(((v.co, (v.co-p1).length) for v in obj.data.vertices), key=lambda x:x[1])[0]
17                 center = (p1+p2)/2
18                 radius = (p1-p2).length/2
19                 for v in obj.data.vertices:
20                         d = v.co-center
21                         if d.length>radius:
22                                 center += d*(1-radius/d.length)/2
23                                 radius = (radius+d.length)/2
24
25                 return center, radius
26
27         def collect_object_lods(self, obj):
28                 lods = [obj]
29                 if self.export_lods:
30                         lods += sorted([c for c in obj.children if c.lod_for_parent], key=(lambda l: l.lod_index))
31                         for i, l in enumerate(lods):
32                                 if i>0 and l.lod_index!=i:
33                                         raise Exception("Inconsistent LOD indices")
34
35                 return lods
36
37         def create_mesh_exporter(self):
38                 from .export_mesh import MeshExporter
39                 mesh_export = MeshExporter()
40                 mesh_export.use_strips = self.use_strips
41                 mesh_export.use_degen_tris = self.use_degen_tris
42                 return mesh_export
43
44         def create_material_exporter(self):
45                 from .export_material import MaterialExporter
46                 material_export = MaterialExporter()
47                 material_export.single_file = self.single_file
48                 material_export.use_textures = self.use_textures
49                 return material_export
50
51         def create_material_map_exporter(self):
52                 from .export_material import MaterialMapExporter
53                 material_map_export = MaterialMapExporter()
54                 material_map_export.single_file = self.single_file
55                 return material_map_export
56
57         def export_to_file(self, context, out_fn):
58                 obj = context.active_object
59
60                 from .util import Progress
61                 progress = Progress(self.show_progress and context)
62
63                 path, base = os.path.split(out_fn)
64                 base = os.path.splitext(base)[0]
65
66                 resources = {}
67                 self.export_object_resources(context, obj, resources, progress)
68
69                 obj_res = self.export_object(context, obj, progress, resources=resources)
70                 refs = obj_res.collect_references()
71                 if not self.shared_resources:
72                         numbers = {}
73                         for r in refs:
74                                 ext = os.path.splitext(r.name)[1]
75                                 n = numbers.get(ext, 0)
76                                 if n>0:
77                                         r.name = "{}_{}{}".format(base, n, ext)
78                                 else:
79                                         r.name = base+ext
80                                 numbers[ext] = n+1
81
82                 for r in refs:
83                         with open(os.path.join(path, r.name), "w") as out_file:
84                                 for s in r.statements:
85                                         s.write_to_file(out_file)
86
87                 with open(out_fn, "w") as out_file:
88                         for s in obj_res.statements:
89                                 s.write_to_file(out_file)
90
91         def export_object_resources(self, context, obj, resources, progress, material_maps=None):
92                 if material_maps is None:
93                         material_maps = {}
94
95                 lods = self.collect_object_lods(obj)
96
97                 from .mesh import create_mesh_from_object
98                 from .material import create_material_map
99                 mesh_export = self.create_mesh_exporter()
100                 material_export = self.create_material_exporter()
101                 material_map_export = self.create_material_map_exporter()
102
103                 for i, l in enumerate(lods):
104                         lod_index = l.lod_index if l.lod_for_parent else 0
105                         progress.push_task_slice("LOD {}".format(lod_index), i, len(lods))
106
107                         material_map = None
108                         mapped_count = sum(m.render_mode!='EXTERNAL' and m.material_map for m in l.data.materials if m)
109                         if mapped_count:
110                                 mmk = lambda m: m.shader if m.render_mode=='CUSTOM' else ""
111                                 material_map_key = mmk(l.data.materials[0])
112                                 key_mismatch = any(mmk(m)!=material_map_key for m in l.data.materials)
113                                 if mapped_count!=len(l.data.materials) or key_mismatch:
114                                         raise Exception("Conflicting settings in object materials")
115
116                                 if material_map_key in material_maps:
117                                         material_map = material_maps[material_map_key]
118                                 else:
119                                         material_map = create_material_map(context, l.data.materials[0])
120                                         material_maps[material_map_key] = material_map
121
122                                 tech_name = "{}.tech".format(material_map.name)
123                                 if tech_name not in resources:
124                                         material_map_export.export_technique_resources(material_map, resources)
125                                         resources[tech_name] = material_map_export.export_technique(material_map, resources=resources)
126                         elif l.material_slots and l.material_slots[0].material:
127                                 material = l.material_slots[0].material
128                                 if material.render_mode!='EXTERNAL':
129                                         tech_name = material.name+".tech"
130                                         if tech_name not in resources:
131                                                 material_export.export_technique_resources(material, resources)
132                                                 resources[tech_name] = material_export.export_technique(material, resources=resources)
133                         elif "stub.tech" not in resources:
134                                 resources["stub.tech"] = self.export_stub_technique()
135
136                         mesh_name = l.data.name+".mesh"
137                         if mesh_name not in resources:
138                                 mesh = create_mesh_from_object(context, l, progress, material_map=material_map)
139                                 mesh_res = mesh_export.export_mesh(context, mesh, progress)
140                                 resources[mesh_name] = mesh_res
141
142                         progress.pop_task()
143
144         def export_object(self, context, obj, progress, *, resources=None):
145                 if resources is None:
146                         resources = {}
147                         self.export_object_resources(context, obj, resources, progress)
148
149                 lods = self.collect_object_lods(obj)
150
151                 from .datafile import Resource, Statement
152                 obj_res = Resource(obj.name+".object")
153                 statements = obj_res.statements
154
155                 center, radius = self.compute_bounding_sphere(obj)
156                 statements.append(Statement("bounding_sphere_hint", *center, radius))
157
158                 prev_mesh = None
159                 prev_tech = None
160                 for i, l in enumerate(lods):
161                         lod_st = []
162
163                         if l.data.name!=prev_mesh:
164                                 mesh_res = resources[l.data.name+".mesh"]
165                                 if not self.single_file:
166                                         lod_st.append(obj_res.create_reference_statement("mesh", mesh_res))
167                                 else:
168                                         lod_st.append(obj_res.create_embed_statement("mesh", mesh_res))
169
170                                 prev_mesh = l.data.name
171
172                         material = None
173                         if l.material_slots:
174                                 material = l.material_slots[0].material
175                         if material:
176                                 if material.material_map:
177                                         tech_res = resources["material_map_{}.tech".format(os.path.splitext(material.technique)[0])]
178                                 else:
179                                         tech_res = resources[material.name+".tech"]
180                         else:
181                                 tech_res = resources["stub.tech"]
182
183                         if tech_res.name!=prev_tech:
184                                 if material and material.render_mode=='EXTERNAL':
185                                         lod_st.append(Statement("technique", material.technique))
186                                 elif not self.single_file:
187                                         lod_st.append(obj_res.create_reference_statement("technique", tech_res))
188                                 else:
189                                         lod_st.append(obj_res.create_embed_statement("technique", tech_res))
190                                 prev_tech = tech_res.name
191
192                         if i>0:
193                                 st = Statement("level_of_detail", i)
194                                 st.sub = lod_st
195                                 statements.append(st)
196                         else:
197                                 statements += lod_st
198
199                 progress.set_progress(1.0)
200
201                 return obj_res
202
203         def export_stub_technique(self):
204                 from .datafile import Resource, Statement
205                 tech_res = Resource("stub.tech")
206                 pass_st = Statement("pass", "")
207                 tech_res.statements.append(pass_st)
208                 mat_st = Statement("material")
209                 pass_st.sub.append(mat_st)
210                 mat_st.sub.append(Statement("basic"))
211                 return tech_res