]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export.py
Refactor scene export to use inline scenes instead of a wrapper scene
[libs/gl.git] / blender / io_mspgl / export.py
index 96f39320ec7d35e590840573d7d9b7d75b53df8b..6db2c81c6cbb9f718ebec222227182e9e8d7e96b 100644 (file)
@@ -1,14 +1,10 @@
 import os
+import itertools
 
 class DataExporter:
-       def __init__(self):
-               self.show_progress = True
-               self.collection = False
-               self.shared_resources = True
-
-       def export_to_file(self, context, out_fn):
+       def export_to_file(self, context, out_fn, *, collection=False, shared_resources=False):
                from .util import Progress
-               progress = Progress(self.show_progress and context)
+               progress = Progress(context)
 
                objects = context.selected_objects
 
@@ -21,7 +17,7 @@ class DataExporter:
                base, ext = os.path.splitext(base)
 
                refs = dummy_res.collect_references()
-               if not self.shared_resources:
+               if not shared_resources:
                        numbers = {}
                        for r in refs:
                                res_ext = os.path.splitext(r.name)[1]
@@ -32,7 +28,7 @@ class DataExporter:
                                        r.name = base+res_ext
                                numbers[res_ext] = n+1
 
-               if self.collection:
+               if collection:
                        dummy_res.write_collection(out_fn, exclude_self=True)
                else:
                        for r in refs:
@@ -45,34 +41,114 @@ class DataExporter:
                object_exporter = None
                camera_exporter = None
                armature_exporter = None
+               light_exporter = None
 
                from .datafile import Resource
                dummy_res = Resource("dummy", "dummy")
 
                for i, obj in enumerate(objects):
                        progress.push_task_slice(obj.name, i, len(objects))
+                       res_name = None
                        res = None
                        if obj.type=='MESH':
-                               if not object_exporter:
-                                       from .export_object import ObjectExporter
-                                       object_exporter = ObjectExporter()
-                               object_exporter.export_object_resources(context, obj, resources, material_atlases, progress)
-                               res = object_exporter.export_object(obj, resources, progress)
+                               res_name = obj.name+".object"
+                               if res_name not in resources:
+                                       if not object_exporter:
+                                               from .export_object import ObjectExporter
+                                               object_exporter = ObjectExporter()
+                                       object_exporter.export_object_resources(context, obj, resources, material_atlases, progress)
+                                       res = object_exporter.export_object(obj, resources, progress)
                        elif obj.type=='CAMERA':
-                               if not camera_exporter:
-                                       from .export_camera import CameraExporter
-                                       camera_exporter = CameraExporter()
-                               res = camera_exporter.export_camera(obj)
+                               res_name = obj.name+".camera"
+                               if res_name not in resources:
+                                       if not camera_exporter:
+                                               from .export_camera import CameraExporter
+                                               camera_exporter = CameraExporter()
+                                       res = camera_exporter.export_camera(obj)
                        elif obj.type=='ARMATURE':
-                               if not armature_exporter:
-                                       from .export_armature import ArmatureExporter
-                                       armature_exporter = ArmatureExporter()
-                               res = armature_exporter.export_armature(context, obj)
+                               res_name = obj.name+".arma"
+                               if res_name not in resources:
+                                       if not armature_exporter:
+                                               from .export_armature import ArmatureExporter
+                                               armature_exporter = ArmatureExporter()
+                                       res = armature_exporter.export_armature(context, obj)
+                       elif obj.type=='LIGHT':
+                               res_name = obj.name+".light"
+                               if res_name not in resources:
+                                       if not light_exporter:
+                                               from .export_light import LightExporter
+                                               light_exporter = LightExporter()
+                                       res = light_exporter.export_light(obj)
 
                        if res:
-                               resources[res.name] = res
+                               resources[res_name] = res
                                dummy_res.create_reference_statement("ref", res)
 
                        progress.pop_task()
 
                return dummy_res
+
+class ProjectExporter:
+       def export_to_directory(self, context, out_dir):
+               from .util import Progress
+               progress = Progress(context)
+
+               from .scene import create_scene_chain
+
+               scenes = {}
+               sequences = []
+               for s in context.blend_data.scenes:
+                       if s.export_disposition=='IGNORE':
+                               continue
+
+                       if s.export_disposition=='SEQUENCE':
+                               scene = create_scene_chain(s, scenes)
+                               sequences.append(scene)
+                       elif s.name not in scenes:
+                               scene = create_scene(s)
+                               if s.export_disposition=='SCENE':
+                                       scenes[scene.name] = scene
+
+               all_objects = []
+               for s in scenes.values():
+                       all_objects += s.prototypes
+                       all_objects += s.lights
+                       if s.camera:
+                               all_objects.append(s.camera)
+
+               scene_queue = list(scenes.values())
+               ordered_scenes = []
+               while scene_queue:
+                       s = scene_queue.pop(0)
+                       if not s.background_set or s.background_set in ordered_scenes:
+                               ordered_scenes.append(s)
+                       else:
+                               scene_queue.append(s)
+
+               from .util import make_unique
+               all_objects = make_unique(all_objects)
+
+               from .export_scene import SceneExporter
+               scene_exporter = SceneExporter()
+               data_exporter = DataExporter()
+
+               resources = {}
+               dummy_res = data_exporter.export_resources(context, all_objects, resources, None, progress)
+               for s in ordered_scenes:
+                       scene_name = s.name+".scene"
+                       if scene_name not in resources:
+                               scene_res = scene_exporter.export_scene(s, resources)
+                               resources[scene_name] = scene_res
+                               dummy_res.create_reference_statement("ref", scene_res)
+
+               for s in sequences:
+                       seq_name = s.name+".seq"
+                       if seq_name not in resources:
+                               scene_exporter.export_sequence_resources(s, resources)
+                               seq_res = scene_exporter.export_sequence(s, resources)
+                               resources[seq_name] = seq_res
+                               dummy_res.create_reference_statement("ref", seq_res)
+
+               refs = dummy_res.collect_references()
+               for r in refs:
+                       r.write_to_file(os.path.join(out_dir, r.name))