]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
Refactor the structure of sequence template files
[libs/gl.git] / blender / io_mspgl / export_scene.py
1 import math
2 import os
3
4 class SceneExporter:
5         def export_to_file(self, context, out_fn, *, selected_only=False, visible_only=True, collection=True, skip_existing=True):
6                 from .util import Progress
7                 progress = Progress(context)
8
9                 from .scene import create_scene_from_current
10                 scene = create_scene_from_current(context, selected_only=selected_only, visible_only=visible_only)
11
12                 resources = {}
13                 self.export_scene_resources(context, scene, resources, progress)
14                 scene_res = self.export_scene(scene, resources)
15                 progress.set_progress(1.0)
16
17                 path, base = os.path.split(out_fn)
18                 base, ext = os.path.splitext(base)
19
20                 if collection:
21                         existing = None
22                         if skip_existing:
23                                 existing = lambda r: not os.path.exists(os.path.join(path, r.name))
24                         scene_res.write_collection(out_fn, filter=existing)
25                 else:
26                         scene_res.write_to_file(out_fn)
27                         for r in scene_res.collect_references():
28                                 r.write_to_file(os.path.join(path, r.name))
29
30         def export_scene_resources(self, context, scene, resources, progress):
31                 from .export import DataExporter
32                 data_exporter = DataExporter()
33
34                 data_exporter.export_resources(context, scene.prototypes, resources, None, progress)
35
36         def export_scene(self, scene, resources):
37                 from .datafile import Resource, Statement, Token
38                 scene_res = Resource(scene.name+".scene", "scene")
39
40                 if scene.background_set or (scene.instances and scene.blended_instances):
41                         scene_res.statements.append(Statement("type", Token("ordered")))
42                         if scene.background_set:
43                                 scene_res.statements.append(scene_res.create_reference_statement("scene", resources[scene.background_set.name+".scene"]))
44
45                         if scene.instances:
46                                 st = Statement("scene")
47                                 st.sub.append(Statement("type", Token("simple")))
48                                 self.add_instances(scene_res, st.sub, scene.instances, resources)
49                                 scene_res.statements.append(st)
50
51                         if scene.blended_instances:
52                                 st = Statement("scene")
53                                 st.sub.append(Statement("type", Token("zsorted")))
54                                 self.add_instances(scene_res, st.sub, scene.blended_instances, resources)
55                                 scene_res.statements.append(st)
56                 else:
57                         scene_type = "zsorted" if scene.blended_instances else "simple"
58                         scene_res.statements.append(Statement("type", Token(scene_type)))
59
60                         self.add_instances(scene_res, scene_res.statements, scene.instances, resources)
61                         self.add_instances(scene_res, scene_res.statements, scene.blended_instances, resources)
62
63                 return scene_res
64
65         def add_instances(self, scene_res, statements, instances, resources):
66                 from .datafile import Statement
67
68                 for i in instances:
69                         obj_res = resources[i.prototype+".object"]
70                         st = scene_res.create_reference_statement("object", obj_res, i.name)
71
72                         ss = Statement("transform")
73
74                         loc = i.matrix_world.to_translation()
75                         ss.sub.append(Statement("position", *tuple(loc)))
76
77                         quat = i.matrix_world.to_quaternion()
78                         if i.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
79                                 angles = [a*180/math.pi for a in quat.to_euler()]
80                                 ss.sub.append(Statement("euler", *angles));
81                         else:
82                                 ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
83
84                         scale = i.matrix_world.to_scale()
85                         ss.sub.append(Statement("scale", *tuple(scale)))
86
87                         st.sub.append(ss)
88                         statements.append(st)
89
90         def export_sequence_resources(self, scene, resources):
91                 from .datafile import Resource, Statement, Token
92
93                 lights = []
94                 s = scene
95                 while s:
96                         lights += s.lights
97                         s = s.background_set
98
99                 from .util import make_unique
100                 lights = make_unique(lights)
101
102                 from .export_light import LightExporter
103                 light_exporter = LightExporter()
104                 for l in lights:
105                         light_name = l.name+".light"
106                         if light_name not in resources:
107                                 resources[light_name] = light_exporter.export_light(l)
108
109                 lighting_name = scene.name+".lightn"
110                 if lighting_name not in resources:
111                         lighting_res = Resource(lighting_name, "lighting")
112                         lighting_res.statements.append(Statement("ambient", *tuple(scene.ambient_light)))
113                         for l in lights:
114                                 lighting_res.statements.append(lighting_res.create_reference_statement("light", resources[l.name+".light"]))
115
116                         resources[lighting_name] = lighting_res
117
118         def export_sequence(self, scene, resources):
119                 from .datafile import Resource, Statement, Token
120                 seq_res = Resource(scene.name+".seq", "sequence")
121
122                 if scene.use_hdr:
123                         seq_res.statements.append(Statement("hdr", True))
124
125                 ss = Statement("clear")
126                 ss.sub.append(Statement("color", 0.0, 0.0, 0.0, 0.0))
127                 ss.sub.append(Statement("depth", 1.0))
128                 seq_res.statements.append(ss)
129
130                 scene_res = resources[scene.name+".scene"]
131                 seq_res.statements.append(seq_res.create_reference_statement("renderable", "content", scene_res))
132
133                 lighting_res = resources[scene.name+".lightn"]
134
135                 any_opaque = False
136                 any_blended = False
137                 s = scene
138                 while s:
139                         if s.instances:
140                                 any_opaque = True
141                         if s.blended_instances:
142                                 any_blended = True
143                         s = s.background_set
144
145                 if any_opaque:
146                         ss = Statement("step", "", "content")
147                         ss.sub.append(Statement("depth_test", Token("LEQUAL")))
148                         ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
149                         seq_res.statements.append(ss)
150
151                 if any_blended:
152                         ss = Statement("step", "blended", "content")
153                         ss.sub.append(Statement("depth_test", Token("LEQUAL")))
154                         ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
155                         seq_res.statements.append(ss)
156
157                 if scene.use_ao:
158                         ss = Statement("postprocessor")
159                         ss.sub.append(Statement("type", Token("ambient_occlusion")))
160                         ss.sub.append(Statement("occlusion_radius", scene.ao_distance))
161                         ss.sub.append(Statement("samples", scene.ao_samples))
162                         seq_res.statements.append(ss)
163
164                 if scene.use_hdr:
165                         ss = Statement("postprocessor")
166                         ss.sub.append(Statement("type", Token("bloom")))
167                         seq_res.statements.append(ss)
168
169                         ss = Statement("postprocessor")
170                         ss.sub.append(Statement("type", Token("colorcurve")))
171                         ss.sub.append(Statement("exposure_adjust", scene.exposure))
172                         ss.sub.append(Statement("srgb"))
173                         seq_res.statements.append(ss)
174                 else:
175                         # Add a colorcurve with linear response to convert into sRGB color space
176                         ss = Statement("postprocessor")
177                         ss.sub.append(Statement("type", Token("colorcurve")))
178                         ss.sub.append(Statement("brightness_response", 1.0))
179                         ss.sub.append(Statement("srgb"))
180                         seq_res.statements.append(ss)
181
182                 return seq_res