]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
Remove support for material atlases from the Blender exporter
[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, 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                 for i in instances:
71                         obj_res = resources[i.prototype.name+".object"]
72                         st = scene_res.create_reference_statement("object", obj_res)
73                         if i.name:
74                                 st.append(i.name)
75
76                         ss = Statement("transform")
77
78                         loc = i.matrix_world.to_translation()
79                         ss.sub.append(Statement("position", *tuple(loc)))
80
81                         quat = i.matrix_world.to_quaternion()
82                         if i.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
83                                 angles = [a*180/math.pi for a in quat.to_euler()]
84                                 ss.sub.append(Statement("euler", *angles));
85                         else:
86                                 ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
87
88                         scale = i.matrix_world.to_scale()
89                         ss.sub.append(Statement("scale", *tuple(scale)))
90
91                         st.sub.append(ss)
92                         statements.append(st)
93
94         def export_sequence_resources(self, scene, resources):
95                 from .datafile import Resource, Statement, Token
96
97                 lights = []
98                 s = scene
99                 while s:
100                         lights += s.lights
101                         s = s.background_set
102
103                 from .util import make_unique
104                 lights = make_unique(lights)
105
106                 from .export_light import LightExporter
107                 light_exporter = LightExporter()
108                 for l in lights:
109                         light_name = l.name+".light"
110                         if light_name not in resources:
111                                 resources[light_name] = light_exporter.export_light(l)
112
113                 lighting_name = scene.name+".lightn"
114                 if lighting_name not in resources:
115                         lighting_res = Resource(lighting_name, "lighting")
116                         lighting_res.statements.append(Statement("ambient", *tuple(scene.ambient_light)))
117                         for l in lights:
118                                 lighting_res.statements.append(lighting_res.create_reference_statement("light", resources[l.name+".light"]))
119
120                         resources[lighting_name] = lighting_res
121
122         def export_sequence(self, scene, resources):
123                 from .datafile import Resource, Statement, Token
124                 seq_res = Resource(scene.name+".seq", "sequence")
125
126                 if scene.use_hdr:
127                         seq_res.statements.append(Statement("hdr", True))
128
129                 self.add_clear(seq_res.statements, (0.0, 0.0, 0.0, 0.0), 1.0)
130
131                 scene_res = resources[scene.name+".scene"]
132                 seq_res.statements.append(seq_res.create_reference_statement("renderable", "content", scene_res))
133
134                 lighting_res = resources[scene.name+".lightn"]
135
136                 any_opaque = False
137                 any_blended = False
138                 use_ibl = False
139                 use_shadow = False
140                 shadowed_lights = []
141                 shadow_casters = []
142                 s = scene
143                 while s:
144                         if s.instances:
145                                 any_opaque = True
146                         if s.blended_instances:
147                                 any_blended = True
148                         if s.use_ibl:
149                                 use_ibl = True
150                         if s.use_shadow:
151                                 use_shadow = True
152                         shadowed_lights += [l.data for l in s.lights if l.data.use_shadow]
153                         for i in itertools.chain(s.instances, s.blended_instances):
154                                 p = i.prototype
155                                 if p.material_slots and p.material_slots[0].material and p.material_slots[0].material.shadow_method!='NONE':
156                                         shadow_casters.append(i)
157                         s = s.background_set
158
159                 shadowed_lights.sort(key=lambda l:l.shadow_map_size, reverse=True)
160
161                 main_tags = []
162                 if any_opaque:
163                         main_tags.append("")
164                 if any_blended:
165                         main_tags.append("blended")
166
167                 content = "content"
168                 if use_ibl and scene.use_sky:
169                         self.add_auxiliary_sequence(seq_res, "environment", "sky", ((0.0, 0.0, 0.0, 0.0), 1.0), main_tags, lighting_res)
170
171                         st = Statement("effect", "environment")
172                         st.sub.append(Statement("type", Token("environment_map")))
173                         st.sub.append(Statement("size", 32))
174                         st.sub.append(Statement("roughness_levels", 2))
175                         st.sub.append(Statement("fixed_position", 0.0, 0.0, 0.0))
176                         st.sub.append(Statement("content", content))
177                         st.sub.append(Statement("environment", "environment_sequence"))
178
179                         seq_res.statements.append(st)
180                         content = "environment"
181
182                 if scene.use_sky:
183                         st = Statement("effect", "sky")
184                         st.sub.append(Statement("type", Token("sky")))
185                         st.sub.append(seq_res.create_reference_statement("sun", resources[scene.sun_light.name+".light"]))
186                         st.sub.append(Statement("content", content))
187
188                         seq_res.statements.append(st)
189                         content = "sky"
190
191                 if use_shadow:
192                         self.add_auxiliary_sequence(seq_res, "shadow", "content", (None, 1.0), ["shadow"], None)
193                         self.add_auxiliary_sequence(seq_res, "thsm", "content", (None, 1.0), ["shadow_thsm"], None)
194
195                         st = Statement("effect", "shadow_map")
196                         st.sub.append(Statement("type", Token("shadow_map")))
197                         st.sub.append(Statement("enable_for_method", "blended"))
198                         st.sub.append(Statement("size", *self.compute_shadowmap_size(shadowed_lights)))
199                         target, radius = self.compute_bounding_sphere(shadow_casters)
200                         st.sub.append(Statement("target", *target))
201                         st.sub.append(Statement("radius", radius))
202                         st.sub.append(Statement("content", content))
203                         st.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
204                         for l in shadowed_lights:
205                                 ss = seq_res.create_reference_statement("light", resources[l.name+".light"])
206                                 ss.sub.append(Statement("size", int(l.shadow_map_size)))
207                                 shadow_caster = "thsm_sequence" if l.type=='POINT' else "shadow_sequence"
208                                 ss.sub.append(Statement("shadow_caster", shadow_caster))
209                                 st.sub.append(ss)
210
211                         seq_res.statements.append(st)
212                         content = "shadow_map"
213
214                 self.add_content_steps(seq_res, content, lighting_res, main_tags)
215
216                 if scene.use_ao:
217                         ss = Statement("postprocessor")
218                         ss.sub.append(Statement("type", Token("ambient_occlusion")))
219                         ss.sub.append(Statement("occlusion_radius", scene.ao_distance))
220                         ss.sub.append(Statement("samples", scene.ao_samples))
221                         seq_res.statements.append(ss)
222
223                 if scene.use_hdr:
224                         ss = Statement("postprocessor")
225                         ss.sub.append(Statement("type", Token("bloom")))
226                         seq_res.statements.append(ss)
227
228                         ss = Statement("postprocessor")
229                         ss.sub.append(Statement("type", Token("colorcurve")))
230                         ss.sub.append(Statement("exposure_adjust", scene.exposure))
231                         ss.sub.append(Statement("srgb"))
232                         seq_res.statements.append(ss)
233                 else:
234                         # Add a colorcurve with linear response to convert into sRGB color space
235                         ss = Statement("postprocessor")
236                         ss.sub.append(Statement("type", Token("colorcurve")))
237                         ss.sub.append(Statement("brightness_response", 1.0))
238                         ss.sub.append(Statement("srgb"))
239                         seq_res.statements.append(ss)
240
241                 return seq_res
242
243         def add_clear(self, statements, color, depth):
244                 from .datafile import Statement
245
246                 st = Statement("clear")
247                 if color is not None:
248                         st.sub.append(Statement("color", *color))
249                 if depth is not None:
250                         st.sub.append(Statement("depth", depth))
251                 statements.append(st)
252
253         def add_content_steps(self, seq_res, renderable, lighting, tags):
254                 from .datafile import Statement, Token
255
256                 for t in tags:
257                         st = Statement("step", t, renderable)
258                         st.sub.append(Statement("depth_test", Token("LEQUAL")))
259                         if lighting:
260                                 st.sub.append(seq_res.create_reference_statement("lighting", lighting))
261                         seq_res.statements.append(st)
262
263         def add_auxiliary_sequence(self, seq_res, aux_name, content, clear_values, step_tags, lighting):
264                 seq_name = os.path.splitext(seq_res.name)[0]
265
266                 from .datafile import Resource, Statement
267                 aux_seq_res = Resource("{}_{}.seq".format(seq_name, aux_name), "sequence")
268                 self.add_clear(aux_seq_res.statements, *clear_values)
269                 aux_seq_res.statements.append(Statement("renderable", "content"))
270                 self.add_content_steps(aux_seq_res, "content", lighting, step_tags)
271
272                 st = seq_res.create_reference_statement("sequence", aux_name+"_sequence", aux_seq_res)
273                 st.sub.append(Statement("renderable", "content", content))
274                 seq_res.statements.append(st)
275
276         def compute_shadowmap_size(self, lights):
277                 total_area = 0
278                 for l in lights:
279                         s = int(l.shadow_map_size)
280                         total_area += s*s
281
282                 size = 1
283                 while size*size<total_area:
284                         size *= 2
285                 if size*size>total_area*2:
286                         return (size, size//2)
287                 else:
288                         return (size, size)
289
290         def compute_bounding_sphere(self, instances):
291                 points = []
292                 for i in instances:
293                         points += [i.matrix_world@mathutils.Vector(c) for c in i.prototype.bound_box]
294
295                 from .util import compute_bounding_sphere
296                 return compute_bounding_sphere(points)