]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
Make it possible to export instance arrays from Blender
[libs/gl.git] / blender / io_mspgl / export_scene.py
1 import math
2 import os
3 import itertools
4 import mathutils
5
6 class SceneExporter:
7         def export_to_file(self, ctx, out_fn, *, selected_only=False, visible_only=True, collection=True, skip_existing=True):
8                 from .scene import create_scene_from_current
9                 task = ctx.task("Preparing scene", 0.1)
10                 scene = create_scene_from_current(task, selected_only=selected_only, visible_only=visible_only)
11
12                 resources = {}
13                 task = ctx.task("Exporting resources", 0.9)
14                 self.export_scene_resources(task, scene, resources)
15                 task = ctx.task(scene, 1.0)
16                 scene_res = self.export_scene(scene, resources)
17
18                 path, base = os.path.split(out_fn)
19                 base, ext = os.path.splitext(base)
20
21                 task = ctx.task("Writing files", 1.0)
22                 if collection:
23                         existing = None
24                         if skip_existing:
25                                 existing = lambda r: not os.path.exists(os.path.join(path, r.name))
26                         scene_res.write_collection(out_fn, filter=existing)
27                 else:
28                         scene_res.write_to_file(out_fn)
29                         for r in scene_res.collect_references():
30                                 r.write_to_file(os.path.join(path, r.name))
31
32         def export_scene_resources(self, ctx, scene, resources):
33                 from .export import DataExporter
34                 data_exporter = DataExporter()
35
36                 data_exporter.export_resources(ctx, [p.object for p in scene.prototypes], resources)
37
38         def export_scene(self, scene, resources):
39                 from .datafile import Resource, Statement, Token
40                 scene_res = Resource(scene.name+".scene", "scene")
41
42                 if scene.background_set or (scene.instances and scene.blended_instances):
43                         scene_res.statements.append(Statement("type", Token("ordered")))
44                         if scene.background_set:
45                                 scene_res.statements.append(scene_res.create_reference_statement("scene", resources[scene.background_set.name+".scene"]))
46
47                         if scene.instances:
48                                 st = Statement("scene")
49                                 st.sub.append(Statement("type", Token("simple")))
50                                 self.add_instances(scene_res, st.sub, scene.instances, resources)
51                                 scene_res.statements.append(st)
52
53                         if scene.blended_instances:
54                                 st = Statement("scene")
55                                 st.sub.append(Statement("type", Token("zsorted")))
56                                 self.add_instances(scene_res, st.sub, scene.blended_instances, resources)
57                                 scene_res.statements.append(st)
58                 else:
59                         scene_type = "zsorted" if scene.blended_instances else "simple"
60                         scene_res.statements.append(Statement("type", Token(scene_type)))
61
62                         self.add_instances(scene_res, scene_res.statements, scene.instances, resources)
63                         self.add_instances(scene_res, scene_res.statements, scene.blended_instances, resources)
64
65                 return scene_res
66
67         def add_instances(self, scene_res, statements, instances, resources):
68                 from .datafile import Statement
69
70                 array_prototypes = []
71
72                 for i in instances:
73                         if i.prototype.use_array:
74                                 if i.prototype not in array_prototypes:
75                                         array_prototypes.append(i.prototype)
76                                 continue
77
78                         obj_res = resources[i.prototype.name+".object"]
79                         st = scene_res.create_reference_statement("object", obj_res)
80                         if i.name:
81                                 st.append(i.name)
82
83                         st.sub.append(self.create_transform_statement(i))
84                         statements.append(st)
85
86                 for p in array_prototypes:
87                         obj_res = resources[p.name+".object"]
88                         st = scene_res.create_reference_statement("array", obj_res)
89
90                         for i in p.instances:
91                                 ss = Statement("instance")
92                                 ss.sub.append(self.create_transform_statement(i))
93                                 st.sub.append(ss)
94
95                         statements.append(st)
96
97         def create_transform_statement(self, instance):
98                 from .datafile import Statement
99
100                 st = Statement("transform")
101
102                 loc = instance.matrix_world.to_translation()
103                 st.sub.append(Statement("position", *tuple(loc)))
104
105                 quat = instance.matrix_world.to_quaternion()
106                 if instance.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
107                         angles = [a*180/math.pi for a in quat.to_euler()]
108                         st.sub.append(Statement("euler", *angles));
109                 else:
110                         st.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
111
112                 scale = instance.matrix_world.to_scale()
113                 st.sub.append(Statement("scale", *tuple(scale)))
114
115                 return st
116
117         def export_sequence_resources(self, scene, resources):
118                 from .datafile import Resource, Statement, Token
119
120                 lights = []
121                 s = scene
122                 while s:
123                         lights += s.lights
124                         s = s.background_set
125
126                 from .util import make_unique
127                 lights = make_unique(lights)
128
129                 from .export_light import LightExporter
130                 light_exporter = LightExporter()
131                 for l in lights:
132                         light_name = l.name+".light"
133                         if light_name not in resources:
134                                 resources[light_name] = light_exporter.export_light(l)
135
136                 lighting_name = scene.name+".lightn"
137                 if lighting_name not in resources:
138                         lighting_res = Resource(lighting_name, "lighting")
139                         lighting_res.statements.append(Statement("ambient", *tuple(scene.ambient_light)))
140                         for l in lights:
141                                 lighting_res.statements.append(lighting_res.create_reference_statement("light", resources[l.name+".light"]))
142
143                         resources[lighting_name] = lighting_res
144
145         def export_sequence(self, scene, resources):
146                 from .datafile import Resource, Statement, Token
147                 seq_res = Resource(scene.name+".seq", "sequence")
148
149                 if scene.use_hdr:
150                         seq_res.statements.append(Statement("hdr", True))
151
152                 self.add_clear(seq_res.statements, (0.0, 0.0, 0.0, 0.0), 1.0)
153
154                 scene_res = resources[scene.name+".scene"]
155                 seq_res.statements.append(seq_res.create_reference_statement("renderable", "content", scene_res))
156
157                 lighting_res = resources[scene.name+".lightn"]
158
159                 any_opaque = False
160                 any_blended = False
161                 use_ibl = False
162                 use_shadow = False
163                 shadowed_lights = []
164                 shadow_casters = []
165                 s = scene
166                 while s:
167                         if s.instances:
168                                 any_opaque = True
169                         if s.blended_instances:
170                                 any_blended = True
171                         if s.use_ibl:
172                                 use_ibl = True
173                         if s.use_shadow:
174                                 use_shadow = True
175                         shadowed_lights += [l.data for l in s.lights if l.data.use_shadow]
176                         for i in itertools.chain(s.instances, s.blended_instances):
177                                 o = i.prototype.object
178                                 if o.material_slots and o.material_slots[0].material and o.material_slots[0].material.shadow_method!='NONE':
179                                         shadow_casters.append(i)
180                         s = s.background_set
181
182                 shadowed_lights.sort(key=lambda l:l.shadow_map_size, reverse=True)
183
184                 main_tags = []
185                 if any_opaque:
186                         main_tags.append("")
187                 if any_blended:
188                         main_tags.append("blended")
189
190                 content = "content"
191                 if use_ibl and scene.use_sky:
192                         self.add_auxiliary_sequence(seq_res, "environment", "sky", ((0.0, 0.0, 0.0, 0.0), 1.0), main_tags, lighting_res)
193
194                         st = Statement("effect", "environment")
195                         st.sub.append(Statement("type", Token("environment_map")))
196                         st.sub.append(Statement("size", 32))
197                         st.sub.append(Statement("roughness_levels", 2))
198                         st.sub.append(Statement("fixed_position", 0.0, 0.0, 0.0))
199                         st.sub.append(Statement("content", content))
200                         st.sub.append(Statement("environment", "environment_sequence"))
201
202                         seq_res.statements.append(st)
203                         content = "environment"
204
205                 if scene.use_sky:
206                         st = Statement("effect", "sky")
207                         st.sub.append(Statement("type", Token("sky")))
208                         st.sub.append(seq_res.create_reference_statement("sun", resources[scene.sun_light.name+".light"]))
209                         st.sub.append(Statement("content", content))
210
211                         seq_res.statements.append(st)
212                         content = "sky"
213
214                 if use_shadow:
215                         self.add_auxiliary_sequence(seq_res, "shadow", "content", (None, 1.0), ["shadow"], None)
216                         self.add_auxiliary_sequence(seq_res, "thsm", "content", (None, 1.0), ["shadow_thsm"], None)
217
218                         st = Statement("effect", "shadow_map")
219                         st.sub.append(Statement("type", Token("shadow_map")))
220                         st.sub.append(Statement("enable_for_method", "blended"))
221                         st.sub.append(Statement("size", *self.compute_shadowmap_size(shadowed_lights)))
222                         target, radius = self.compute_bounding_sphere(shadow_casters)
223                         st.sub.append(Statement("target", *target))
224                         st.sub.append(Statement("radius", radius))
225                         st.sub.append(Statement("content", content))
226                         st.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
227                         for l in shadowed_lights:
228                                 ss = seq_res.create_reference_statement("light", resources[l.name+".light"])
229                                 ss.sub.append(Statement("size", int(l.shadow_map_size)))
230                                 shadow_caster = "thsm_sequence" if l.type=='POINT' else "shadow_sequence"
231                                 ss.sub.append(Statement("shadow_caster", shadow_caster))
232                                 st.sub.append(ss)
233
234                         seq_res.statements.append(st)
235                         content = "shadow_map"
236
237                 self.add_content_steps(seq_res, content, lighting_res, main_tags)
238
239                 if scene.use_ao:
240                         ss = Statement("postprocessor")
241                         ss.sub.append(Statement("type", Token("ambient_occlusion")))
242                         ss.sub.append(Statement("occlusion_radius", scene.ao_distance))
243                         ss.sub.append(Statement("samples", scene.ao_samples))
244                         seq_res.statements.append(ss)
245
246                 if scene.use_hdr:
247                         ss = Statement("postprocessor")
248                         ss.sub.append(Statement("type", Token("bloom")))
249                         seq_res.statements.append(ss)
250
251                         ss = Statement("postprocessor")
252                         ss.sub.append(Statement("type", Token("colorcurve")))
253                         ss.sub.append(Statement("exposure_adjust", scene.exposure))
254                         ss.sub.append(Statement("srgb"))
255                         seq_res.statements.append(ss)
256                 else:
257                         # Add a colorcurve with linear response to convert into sRGB color space
258                         ss = Statement("postprocessor")
259                         ss.sub.append(Statement("type", Token("colorcurve")))
260                         ss.sub.append(Statement("brightness_response", 1.0))
261                         ss.sub.append(Statement("srgb"))
262                         seq_res.statements.append(ss)
263
264                 return seq_res
265
266         def add_clear(self, statements, color, depth):
267                 from .datafile import Statement
268
269                 st = Statement("clear")
270                 if color is not None:
271                         st.sub.append(Statement("color", *color))
272                 if depth is not None:
273                         st.sub.append(Statement("depth", depth))
274                 statements.append(st)
275
276         def add_content_steps(self, seq_res, renderable, lighting, tags):
277                 from .datafile import Statement, Token
278
279                 for t in tags:
280                         st = Statement("step", t, renderable)
281                         st.sub.append(Statement("depth_test", Token("LEQUAL")))
282                         if lighting:
283                                 st.sub.append(seq_res.create_reference_statement("lighting", lighting))
284                         seq_res.statements.append(st)
285
286         def add_auxiliary_sequence(self, seq_res, aux_name, content, clear_values, step_tags, lighting):
287                 seq_name = os.path.splitext(seq_res.name)[0]
288
289                 from .datafile import Resource, Statement
290                 aux_seq_res = Resource("{}_{}.seq".format(seq_name, aux_name), "sequence")
291                 self.add_clear(aux_seq_res.statements, *clear_values)
292                 aux_seq_res.statements.append(Statement("renderable", "content"))
293                 self.add_content_steps(aux_seq_res, "content", lighting, step_tags)
294
295                 st = seq_res.create_reference_statement("sequence", aux_name+"_sequence", aux_seq_res)
296                 st.sub.append(Statement("renderable", "content", content))
297                 seq_res.statements.append(st)
298
299         def compute_shadowmap_size(self, lights):
300                 total_area = 0
301                 for l in lights:
302                         s = int(l.shadow_map_size)
303                         total_area += s*s
304
305                 size = 1
306                 while size*size<total_area:
307                         size *= 2
308                 if size*size>total_area*2:
309                         return (size, size//2)
310                 else:
311                         return (size, size)
312
313         def compute_bounding_sphere(self, instances):
314                 points = []
315                 for i in instances:
316                         points += [i.matrix_world@mathutils.Vector(c) for c in i.prototype.object.bound_box]
317
318                 from .util import compute_bounding_sphere
319                 return compute_bounding_sphere(points)