]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
Refactor scene export to use inline scenes instead of a wrapper scene
[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:
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                         st = Statement("scene")
46                         st.sub.append(Statement("type", Token("simple")))
47                         self.add_instances(scene_res, st.sub, scene.instances, resources)
48                         scene_res.statements.append(st)
49                 else:
50                         scene_res.statements.append(Statement("type", Token("simple")))
51
52                         self.add_instances(scene_res, scene_res.statements, scene.instances, resources)
53
54                 return scene_res
55
56         def add_instances(self, scene_res, statements, instances, resources):
57                 from .datafile import Statement
58
59                 for i in instances:
60                         obj_res = resources[i.prototype+".object"]
61                         st = scene_res.create_reference_statement("object", obj_res, i.name)
62
63                         ss = Statement("transform")
64
65                         loc = i.matrix_world.to_translation()
66                         ss.sub.append(Statement("position", *tuple(loc)))
67
68                         quat = i.matrix_world.to_quaternion()
69                         if i.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
70                                 angles = [a*180/math.pi for a in quat.to_euler()]
71                                 ss.sub.append(Statement("euler", *angles));
72                         else:
73                                 ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
74
75                         scale = i.matrix_world.to_scale()
76                         ss.sub.append(Statement("scale", *tuple(scale)))
77
78                         st.sub.append(ss)
79                         statements.append(st)
80
81         def export_sequence_resources(self, scene, resources):
82                 from .datafile import Resource, Statement, Token
83
84                 lights = []
85                 s = scene
86                 while s:
87                         lights += s.lights
88                         s = s.background_set
89
90                 from .util import make_unique
91                 lights = make_unique(lights)
92
93                 from .export_light import LightExporter
94                 light_exporter = LightExporter()
95                 for l in lights:
96                         light_name = l.name+".light"
97                         if light_name not in resources:
98                                 resources[light_name] = light_exporter.export_light(l)
99
100                 lighting_name = scene.name+".lightn"
101                 if lighting_name not in resources:
102                         lighting_res = Resource(lighting_name, "lighting")
103                         lighting_res.statements.append(Statement("ambient", *tuple(scene.ambient_light)))
104                         for l in lights:
105                                 lighting_res.statements.append(lighting_res.create_reference_statement("light", resources[l.name+".light"]))
106
107                         resources[lighting_name] = lighting_res
108
109         def export_sequence(self, scene, resources):
110                 from .datafile import Resource, Statement, Token
111                 seq_res = Resource(scene.name+".seq", "sequence")
112
113                 if scene.use_hdr:
114                         seq_res.statements.append(Statement("hdr", True))
115
116                 ss = Statement("clear")
117                 ss.sub.append(Statement("color", 0.0, 0.0, 0.0, 0.0))
118                 ss.sub.append(Statement("depth", 1.0))
119                 seq_res.statements.append(ss)
120
121                 ss = Statement("step", "", "content")
122                 ss.sub.append(Statement("depth_test", Token("LEQUAL")))
123                 ss.sub.append(seq_res.create_reference_statement("lighting", resources[scene.name+".lightn"]))
124                 ss.sub.append(seq_res.create_reference_statement("scene", resources[scene.name+".scene"]))
125                 seq_res.statements.append(ss)
126
127                 if scene.use_ao:
128                         ss = Statement("ambient_occlusion")
129                         ss.sub.append(Statement("occlusion_radius", scene.ao_distance))
130                         ss.sub.append(Statement("samples", scene.ao_samples))
131                         seq_res.statements.append(ss)
132
133                 if scene.use_hdr:
134                         seq_res.statements.append(Statement("bloom"))
135                         ss = Statement("colorcurve")
136                         ss.sub.append(Statement("exposure_adjust", scene.exposure))
137                         ss.sub.append(Statement("srgb"))
138                         seq_res.statements.append(ss)
139                 else:
140                         # Add a colorcurve with linear response to convert into sRGB color space
141                         ss = Statement("colorcurve")
142                         ss.sub.append(Statement("brightness_response", 1.0))
143                         ss.sub.append(Statement("srgb"))
144                         seq_res.statements.append(ss)
145
146                 return seq_res