]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_scene.py
Make resource handling in the Blender exporter more flexible
[libs/gl.git] / blender / io_mspgl / export_scene.py
index f6776107413955964d0e3e757bc5594e3a69d03f..b4b9c2365bc22ea14b7f11fec5c06120c74fb870 100644 (file)
@@ -43,42 +43,56 @@ class SceneExporter:
                from .util import Progress
                progress = Progress(self.show_progress and context)
 
-               from .export_object import ObjectExporter
-               object_export = ObjectExporter()
+               resources = {}
+               self.export_scene_resources(context, unique_objects, resources, progress)
+
+               scene_res = self.export_scene(context, objs, progress, prototypes=object_prototypes, resources=resources)
+               refs = scene_res.collect_references()
 
                from .datafile import Statement
                if self.resource_collection:
+                       keywords = { ".mat": "material",
+                               ".mesh": "mesh",
+                               ".object": "object",
+                               ".tech": "technique" }
                        with open(os.path.join(path, base+"_resources.mdc"), "w") as res_out:
-                               for i, o in enumerate(unique_objects):
-                                       progress.push_task_slice(o.name, i, len(unique_objects))
-                                       st = Statement("object", "{}.object".format(o.name))
-                                       st.sub = object_export.export_object(context, o, progress)
+                               for r in refs:
+                                       st = Statement(keywords[os.path.splitext(r.name)[1]], r.name)
+                                       st.sub = r.statements
                                        st.write_to_file(res_out)
-                                       progress.pop_task()
                else:
                        res_dir = os.path.join(path, base+"_resources")
                        if not os.path.exists(res_dir):
                                os.makedirs(res_dir)
-                       for i, o in enumerate(unique_objects):
-                               progress.push_task_slice(o.name, i, len(unique_objects))
-                               st = object_export.export_object(context, o, progress)
-                               with open(os.path.join(res_dir, o.name+".object"), "w") as obj_out:
-                                       for s in st:
-                                               s.write_to_file(obj_out)
-                               progress.pop_task()
-
-               statements = self.export_scene(context, objs, progress, prototypes=object_prototypes)
+                       for r in refs:
+                               with open(os.path.join(res_dir, r.name), "w") as res_out:
+                                       for s in r.statements:
+                                               s.write_to_file(res_out)
 
                with open(out_fn, "w") as out_file:
-                       for s in statements:
+                       for s in scene_res.statements:
                                s.write_to_file(out_file)
 
-       def export_scene(self, context, objs, progress, *, prototypes=None):
-               from .datafile import Statement
-               statements = []
+       def export_scene_resources(self, context, objs, resources, progress):
+               from .export_object import ObjectExporter
+               object_export = ObjectExporter()
+               object_export.separate_mesh = True
+               object_export.separate_tech = True
+
+               for i, o in enumerate(objs):
+                       progress.push_task_slice(o.name, i, len(objs))
+                       object_export.export_object_resources(context, o, resources, progress)
+                       obj_name = o.name+".object"
+                       resources[obj_name] = object_export.export_object(context, o, progress, resources=resources)
+                       progress.pop_task()
+
+       def export_scene(self, context, objs, progress, *, prototypes, resources):
+               from .datafile import Resource, Statement
+               scene_res = Resource("scene.scene")
 
                for o in objs:
-                       st = Statement("object", "{}.object".format(prototypes[o.name].name))
+                       obj_res = resources[prototypes[o.name].name+".object"]
+                       st = scene_res.create_reference_statement("object", obj_res)
                        # XXX Parent relationships screw up the location and rotation
                        st.sub.append(Statement("position", o.location[0], o.location[1], o.location[2]))
                        if o.rotation_mode=="AXIS_ANGLE":
@@ -93,8 +107,8 @@ class SceneExporter:
                                axis = q.axis
                        st.sub.append(Statement("rotation", angle*180/math.pi, axis[0], axis[1], axis[2]))
                        st.sub.append(Statement("scale", o.scale[0], o.scale[1], o.scale[2]))
-                       statements.append(st)
+                       scene_res.statements.append(st)
 
                progress.set_progress(1.0)
 
-               return statements
+               return scene_res