]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_scene.py
Ensure that objects have unique names while exporting a scene
[libs/gl.git] / blender / io_mspgl / export_scene.py
index 245d2d3efeef77db689367a2667e7eb16e341d1d..2ec85ccbddbdef3ed708ddae330d5b713fb98bea 100644 (file)
@@ -3,12 +3,23 @@ import os
 
 class SceneExporter:
        def __init__(self):
+               self.selected_only = False
+               self.visible_collections = True
                self.resource_collection = True
+               self.skip_existing = True
                self.show_progress = True
 
        def export_to_file(self, context, out_fn):
-               objs = [o for o in context.selected_objects if o.type=="MESH" and not o.lod_for_parent]
+               if self.selected_only:
+                       objs = context.selected_objects
+               else:
+                       objs = context.scene.objects
+               if self.visible_collections:
+                       collections = [c.collection for c in context.view_layer.layer_collection.children if not (c.hide_viewport or c.collection.hide_viewport)]
+                       objs = [o for o in objs if any((o.name in c.all_objects) for c in collections)]
+               objs = [o for o in objs if o.type=="MESH" and not o.lod_for_parent]
                objs = [o for o in objs if (not o.compound or o.parent not in objs)]
+               objs.sort(key=lambda x:x.name)
 
                path, base = os.path.split(out_fn)
                base, ext = os.path.splitext(base)
@@ -19,23 +30,21 @@ class SceneExporter:
                object_prototypes = {}
                unique_objects = []
                export_names = {}
+               used_names = set()
                for o in objs:
                        if o.name in object_prototypes:
                                continue
 
                        clones = [o]
-                       if not any(s.link=="OBJECT" for s in o.material_slots):
-                               for u in objs:
-                                       if u is o:
-                                               continue
-                                       if u.data.name!=o.data.name:
-                                               continue
-                                       if u.technique!=o.technique:
-                                               continue
-                                       if any(s.link=="OBJECT" for s in u.material_slots):
-                                               continue
-
-                                       clones.append(u)
+                       for u in objs:
+                               if u is o:
+                                       continue
+                               if u.data.name!=o.data.name:
+                                       continue
+                               if any(m1.name!=m2.name for m1, m2 in zip(o.material_slots, u.material_slots)):
+                                       continue
+
+                               clones.append(u)
 
                        prefix = o.name
                        for c in clones:
@@ -46,12 +55,23 @@ class SceneExporter:
                                        prefix = prefix[:pos]
 
                        if prefix:
-                               export_names[o.name+".object"] = prefix.strip(" .")+".object"
+                               export_names[o.name+".object"] = prefix.strip(" .")
+                       else:
+                               used_names.add(o.name)
 
                        unique_objects.append(o)
                        for c in clones:
                                object_prototypes[c.name] = o
 
+               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)
 
@@ -64,66 +84,58 @@ class SceneExporter:
                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",
-                               ".tex2d": "texture2d" }
-                       with open(os.path.join(path, base+"_resources.mdc"), "w") as res_out:
-                               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)
+                       filter = None
+                       if self.skip_existing:
+                               filter = lambda r: not os.path.exists(os.path.join(path, r.name))
+                       scene_res.write_collection(os.path.join(path, base+"_resources.mdc"), exclude_self=True, filter=filter)
                else:
                        res_dir = os.path.join(path, base+"_resources")
                        if not os.path.exists(res_dir):
                                os.makedirs(res_dir)
                        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)
+                               r.write_to_file(os.path.join(res_dir, r.name))
 
-               with open(out_fn, "w") as out_file:
-                       for s in scene_res.statements:
-                               s.write_to_file(out_file)
+               scene_res.write_to_file(out_fn)
 
        def export_scene_resources(self, context, objs, resources, progress):
                from .export_object import ObjectExporter
                object_export = ObjectExporter()
                object_export.single_file = False
 
-               material_maps = {}
+               material_atlases = {}
 
                for i, o in enumerate(objs):
                        progress.push_task_slice(o.name, i, len(objs))
-                       object_export.export_object_resources(context, o, resources, progress, material_maps=material_maps)
+                       object_export.export_object_resources(context, o, resources, progress, material_atlases=material_atlases)
                        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")
+               scene_res = Resource("scene.scene", "scene")
 
                for o in objs:
                        obj_res = resources[prototypes[o.name].name+".object"]
                        st = scene_res.create_reference_statement("object", obj_res, o.name)
-                       # 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":
-                               angle = o.rotation_axis_angle[0]
-                               axis = o.rotation_axis_angle[1:]
+
+                       ss = Statement("transform")
+
+                       loc = o.matrix_world.to_translation()
+                       ss.sub.append(Statement("position", *tuple(loc)))
+
+                       quat = o.matrix_world.to_quaternion()
+                       if o.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
+                               angles = [a*180/math.pi for a in quat.to_euler()]
+                               ss.sub.append(Statement("euler", *angles));
                        else:
-                               if o.rotation_mode=="QUATERNION":
-                                       q = o.rotation_quaternion
-                               else:
-                                       q = o.rotation_euler.to_quaternion()
-                               angle = q.angle
-                               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]))
+                               ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
+
+                       scale = o.matrix_world.to_scale()
+                       ss.sub.append(Statement("scale", *tuple(scale)))
+
+                       st.sub.append(ss)
                        scene_res.statements.append(st)
 
                progress.set_progress(1.0)