]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_scene.py
Refactor the structure of sequence template files
[libs/gl.git] / blender / io_mspgl / export_scene.py
index 39a7428e146652018c5db011f2e8007e08d0e06c..8666e98beee0ef9489557d28da89fdcf788ee036 100644 (file)
@@ -2,18 +2,12 @@ import math
 import os
 
 class SceneExporter:
-       def __init__(self):
-               self.selected_only = False
-               self.visible_only = True
-               self.collection = True
-               self.skip_existing = True
-
-       def export_to_file(self, context, out_fn):
+       def export_to_file(self, context, out_fn, *, selected_only=False, visible_only=True, collection=True, skip_existing=True):
                from .util import Progress
                progress = Progress(context)
 
                from .scene import create_scene_from_current
-               scene = create_scene_from_current(context, selected_only=self.selected_only, visible_only=self.visible_only)
+               scene = create_scene_from_current(context, selected_only=selected_only, visible_only=visible_only)
 
                resources = {}
                self.export_scene_resources(context, scene, resources, progress)
@@ -23,9 +17,9 @@ class SceneExporter:
                path, base = os.path.split(out_fn)
                base, ext = os.path.splitext(base)
 
-               if self.collection:
+               if collection:
                        existing = None
-                       if self.skip_existing:
+                       if skip_existing:
                                existing = lambda r: not os.path.exists(os.path.join(path, r.name))
                        scene_res.write_collection(out_fn, filter=existing)
                else:
@@ -43,9 +37,35 @@ class SceneExporter:
                from .datafile import Resource, Statement, Token
                scene_res = Resource(scene.name+".scene", "scene")
 
-               scene_res.statements.append(Statement("type", Token(scene.scene_type.lower())))
+               if scene.background_set or (scene.instances and scene.blended_instances):
+                       scene_res.statements.append(Statement("type", Token("ordered")))
+                       if scene.background_set:
+                               scene_res.statements.append(scene_res.create_reference_statement("scene", resources[scene.background_set.name+".scene"]))
+
+                       if scene.instances:
+                               st = Statement("scene")
+                               st.sub.append(Statement("type", Token("simple")))
+                               self.add_instances(scene_res, st.sub, scene.instances, resources)
+                               scene_res.statements.append(st)
+
+                       if scene.blended_instances:
+                               st = Statement("scene")
+                               st.sub.append(Statement("type", Token("zsorted")))
+                               self.add_instances(scene_res, st.sub, scene.blended_instances, resources)
+                               scene_res.statements.append(st)
+               else:
+                       scene_type = "zsorted" if scene.blended_instances else "simple"
+                       scene_res.statements.append(Statement("type", Token(scene_type)))
+
+                       self.add_instances(scene_res, scene_res.statements, scene.instances, resources)
+                       self.add_instances(scene_res, scene_res.statements, scene.blended_instances, resources)
 
-               for i in scene.instances:
+               return scene_res
+
+       def add_instances(self, scene_res, statements, instances, resources):
+               from .datafile import Statement
+
+               for i in instances:
                        obj_res = resources[i.prototype+".object"]
                        st = scene_res.create_reference_statement("object", obj_res, i.name)
 
@@ -65,23 +85,11 @@ class SceneExporter:
                        ss.sub.append(Statement("scale", *tuple(scale)))
 
                        st.sub.append(ss)
-                       scene_res.statements.append(st)
-
-               return scene_res
+                       statements.append(st)
 
        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:
@@ -101,37 +109,72 @@ class SceneExporter:
                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
+               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", "lequal"))
-               ss.sub.append(seq_res.create_reference_statement("lighting", resources[scene.name+".lightn"]))
-               ss.sub.append(seq_res.create_reference_statement("scene", content))
+               ss = Statement("clear")
+               ss.sub.append(Statement("color", 0.0, 0.0, 0.0, 0.0))
+               ss.sub.append(Statement("depth", 1.0))
                seq_res.statements.append(ss)
 
+               scene_res = resources[scene.name+".scene"]
+               seq_res.statements.append(seq_res.create_reference_statement("renderable", "content", scene_res))
+
+               lighting_res = resources[scene.name+".lightn"]
+
+               any_opaque = False
+               any_blended = False
+               s = scene
+               while s:
+                       if s.instances:
+                               any_opaque = True
+                       if s.blended_instances:
+                               any_blended = True
+                       s = s.background_set
+
+               if any_opaque:
+                       ss = Statement("step", "", "content")
+                       ss.sub.append(Statement("depth_test", Token("LEQUAL")))
+                       ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
+                       seq_res.statements.append(ss)
+
+               if any_blended:
+                       ss = Statement("step", "blended", "content")
+                       ss.sub.append(Statement("depth_test", Token("LEQUAL")))
+                       ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
+                       seq_res.statements.append(ss)
+
+               if scene.use_ao:
+                       ss = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("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 = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("bloom")))
+                       seq_res.statements.append(ss)
+
+                       ss = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("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 = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("colorcurve")))
                        ss.sub.append(Statement("brightness_response", 1.0))
                        ss.sub.append(Statement("srgb"))
                        seq_res.statements.append(ss)