]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_scene.py
Redesign depth and stencil test and blend state management
[libs/gl.git] / blender / io_mspgl / export_scene.py
index e272823ee164d257b1464a6f840ddb8a51e2e7cb..7bb32efeeb927ab6c083d0bc387d7ccbf885e358 100644 (file)
@@ -7,56 +7,21 @@ class SceneExporter:
                self.visible_only = True
                self.collection = True
                self.skip_existing = True
-               self.show_progress = True
 
        def export_to_file(self, context, out_fn):
-               from .scene import create_scene_from_current
-               scene = create_scene_from_current(context, selected_only=self.selected_only, visible_only=self.visible_only)
-
-               path, base = os.path.split(out_fn)
-               base, ext = os.path.splitext(base)
-
-               export_names = {}
-               used_names = set()
-               for p in scene.prototypes:
-                       clones = [i for i in scene.instances if i.prototype==p.name]
-
-                       prefix = p.name
-                       for c in clones:
-                               while not c.name.startswith(prefix):
-                                       pos = max(prefix.rfind(' '), prefix.rfind('.'))
-                                       if pos<0:
-                                               break;
-                                       prefix = prefix[:pos]
-
-                       if prefix:
-                               export_names[p.name+".object"] = prefix.strip(" .")
-                       else:
-                               used_names.add(p.name)
-
-               for n, e in export_names.items():
-                       if e in used_names:
-                               number = 1
-                               while "{}_{}".format(e, number) in used_names:
-                                       number += 1
-                               e += "_{}".format(number)
-                       export_names[n] = e+".object"
-                       used_names.add(e)
-
                from .util import Progress
-               progress = Progress(self.show_progress and context)
+               progress = Progress(context)
 
-               from .export import DataExporter
-               data_exporter = DataExporter()
+               from .scene import create_scene_from_current
+               scene = create_scene_from_current(context, selected_only=self.selected_only, visible_only=self.visible_only)
 
                resources = {}
-               data_exporter.export_resources(context, scene.prototypes, resources, None, progress)
-               for n, r in resources.items():
-                       if r.name in export_names:
-                               r.name = export_names[r.name]
+               self.export_scene_resources(context, scene, resources, progress)
+               scene_res = self.export_scene(scene, resources)
+               progress.set_progress(1.0)
 
-               scene_res = self.export_scene(scene, resources, progress)
-               refs = scene_res.collect_references()
+               path, base = os.path.split(out_fn)
+               base, ext = os.path.splitext(base)
 
                if self.collection:
                        existing = None
@@ -65,10 +30,16 @@ class SceneExporter:
                        scene_res.write_collection(out_fn, filter=existing)
                else:
                        scene_res.write_to_file(out_fn)
-                       for r in refs:
+                       for r in scene_res.collect_references():
                                r.write_to_file(os.path.join(path, r.name))
 
-       def export_scene(self, scene, resources, progress):
+       def export_scene_resources(self, context, scene, resources, progress):
+               from .export import DataExporter
+               data_exporter = DataExporter()
+
+               data_exporter.export_resources(context, scene.prototypes, resources, None, progress)
+
+       def export_scene(self, scene, resources):
                from .datafile import Resource, Statement, Token
                scene_res = Resource(scene.name+".scene", "scene")
 
@@ -96,6 +67,80 @@ class SceneExporter:
                        st.sub.append(ss)
                        scene_res.statements.append(st)
 
-               progress.set_progress(1.0)
-
                return scene_res
+
+       def export_sequence_resources(self, scene, resources):
+               from .datafile import Resource, Statement, Token
+
+               if scene.background_set:
+                       wrapper_name = scene.name+".wrapper.scene"
+                       if wrapper_name not in resources:
+                               wrapper_res = Resource(wrapper_name, "scene")
+                               wrapper_res.statements.append(Statement("type", Token("ordered")))
+                               for s in scene.get_chain():
+                                       wrapper_res.statements.append(wrapper_res.create_reference_statement("scene", resources[s.name+".scene"]))
+
+                               resources[wrapper_name] = wrapper_res
+
+               lights = []
+               s = scene
+               while s:
+                       lights += s.lights
+                       s = s.background_set
+
+               from .util import make_unique
+               lights = make_unique(lights)
+
+               from .export_light import LightExporter
+               light_exporter = LightExporter()
+               for l in lights:
+                       light_name = l.name+".light"
+                       if light_name not in resources:
+                               resources[light_name] = light_exporter.export_light(l)
+
+               lighting_name = scene.name+".lightn"
+               if lighting_name not in resources:
+                       lighting_res = Resource(lighting_name, "lighting")
+                       lighting_res.statements.append(Statement("ambient", *tuple(scene.ambient_light)))
+                       for l in lights:
+                               lighting_res.statements.append(lighting_res.create_reference_statement("light", resources[l.name+".light"]))
+
+                       resources[lighting_name] = lighting_res
+
+       def export_sequence(self, scene, resources):
+               from .datafile import Resource, Statement, Token
+               seq_res = Resource(scene.name+".seq", "sequence")
+
+               if scene.use_hdr:
+                       seq_res.statements.append(Statement("hdr", True))
+
+               content = scene
+               if scene.background_set:
+                       content = resources[scene.name+".wrapper.scene"]
+
+               ss = Statement("pass", "", "content")
+               ss.sub.append(Statement("depth_test", Token("LEQUAL")))
+               ss.sub.append(seq_res.create_reference_statement("lighting", resources[scene.name+".lightn"]))
+               ss.sub.append(seq_res.create_reference_statement("scene", content))
+               seq_res.statements.append(ss)
+
+               if scene.use_ao:
+                       ss = Statement("ambient_occlusion")
+                       ss.sub.append(Statement("occlusion_radius", scene.ao_distance))
+                       ss.sub.append(Statement("samples", scene.ao_samples))
+                       seq_res.statements.append(ss)
+
+               if scene.use_hdr:
+                       seq_res.statements.append(Statement("bloom"))
+                       ss = Statement("colorcurve")
+                       ss.sub.append(Statement("exposure_adjust", scene.exposure))
+                       ss.sub.append(Statement("srgb"))
+                       seq_res.statements.append(ss)
+               else:
+                       # Add a colorcurve with linear response to convert into sRGB color space
+                       ss = Statement("colorcurve")
+                       ss.sub.append(Statement("brightness_response", 1.0))
+                       ss.sub.append(Statement("srgb"))
+                       seq_res.statements.append(ss)
+
+               return seq_res