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)
9 from .scene import create_scene_from_current
10 scene = create_scene_from_current(context, selected_only=selected_only, visible_only=visible_only)
13 self.export_scene_resources(context, scene, resources, progress)
14 scene_res = self.export_scene(scene, resources)
15 progress.set_progress(1.0)
17 path, base = os.path.split(out_fn)
18 base, ext = os.path.splitext(base)
23 existing = lambda r: not os.path.exists(os.path.join(path, r.name))
24 scene_res.write_collection(out_fn, filter=existing)
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))
30 def export_scene_resources(self, context, scene, resources, progress):
31 from .export import DataExporter
32 data_exporter = DataExporter()
34 data_exporter.export_resources(context, scene.prototypes, resources, None, progress)
36 def export_scene(self, scene, resources):
37 from .datafile import Resource, Statement, Token
38 scene_res = Resource(scene.name+".scene", "scene")
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"]))
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)
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)
57 scene_type = "zsorted" if scene.blended_instances else "simple"
58 scene_res.statements.append(Statement("type", Token(scene_type)))
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)
65 def add_instances(self, scene_res, statements, instances, resources):
66 from .datafile import Statement
69 obj_res = resources[i.prototype+".object"]
70 st = scene_res.create_reference_statement("object", obj_res, i.name)
72 ss = Statement("transform")
74 loc = i.matrix_world.to_translation()
75 ss.sub.append(Statement("position", *tuple(loc)))
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));
82 ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
84 scale = i.matrix_world.to_scale()
85 ss.sub.append(Statement("scale", *tuple(scale)))
90 def export_sequence_resources(self, scene, resources):
91 from .datafile import Resource, Statement, Token
99 from .util import make_unique
100 lights = make_unique(lights)
102 from .export_light import LightExporter
103 light_exporter = LightExporter()
105 light_name = l.name+".light"
106 if light_name not in resources:
107 resources[light_name] = light_exporter.export_light(l)
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)))
114 lighting_res.statements.append(lighting_res.create_reference_statement("light", resources[l.name+".light"]))
116 resources[lighting_name] = lighting_res
118 def export_sequence(self, scene, resources):
119 from .datafile import Resource, Statement, Token
120 seq_res = Resource(scene.name+".seq", "sequence")
123 seq_res.statements.append(Statement("hdr", True))
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)
130 scene_res = resources[scene.name+".scene"]
131 lighting_res = resources[scene.name+".lightn"]
139 if s.blended_instances:
144 ss = Statement("step", "", "content")
145 ss.sub.append(Statement("depth_test", Token("LEQUAL")))
146 ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
147 ss.sub.append(seq_res.create_reference_statement("scene", scene_res))
148 seq_res.statements.append(ss)
151 ss = Statement("step", "blended", "content")
152 ss.sub.append(Statement("depth_test", Token("LEQUAL")))
153 ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
154 ss.sub.append(seq_res.create_reference_statement("scene", scene_res))
155 seq_res.statements.append(ss)
158 ss = Statement("ambient_occlusion")
159 ss.sub.append(Statement("occlusion_radius", scene.ao_distance))
160 ss.sub.append(Statement("samples", scene.ao_samples))
161 seq_res.statements.append(ss)
164 seq_res.statements.append(Statement("bloom"))
165 ss = Statement("colorcurve")
166 ss.sub.append(Statement("exposure_adjust", scene.exposure))
167 ss.sub.append(Statement("srgb"))
168 seq_res.statements.append(ss)
170 # Add a colorcurve with linear response to convert into sRGB color space
171 ss = Statement("colorcurve")
172 ss.sub.append(Statement("brightness_response", 1.0))
173 ss.sub.append(Statement("srgb"))
174 seq_res.statements.append(ss)