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