]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor object prototype discovery in the blender exporter
authorMikko Rasa <tdb@tdb.fi>
Wed, 16 Mar 2022 16:26:12 +0000 (18:26 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 16 Mar 2022 16:26:12 +0000 (18:26 +0200)
blender/io_mspgl/scene.py

index 70dbc8c9a7178d19470c5ba3d0ae5e1c2e142250..7a0fa2f037d290108e8e3d951d8a7617874208c5 100644 (file)
@@ -1,3 +1,4 @@
+import itertools
 import mathutils
 
 def is_same_object(obj1, obj2):
@@ -56,29 +57,43 @@ class Scene:
                if obj_filter:
                        objects = list(filter(obj_filter, objects))
 
-               processed = set()
                for o in objects:
-                       if o.name in processed:
-                               continue
-
                        if o.type=='MESH':
-                               clones = [c for c in objects if is_same_object(o, c)]
-                               self.prototypes.append(o)
-                               instance_list = self.instances
-                               if o.material_slots and o.material_slots[0].material:
-                                       mat = o.material_slots[0].material
-                                       if mat.blend_method=='BLEND':
-                                               instance_list = self.blended_instances
-                                       if mat.image_based_lighting:
-                                               self.use_ibl = True
-                               for c in clones:
-                                       instance_list.append(Instance(c, o))
-                                       processed.add(c.name)
+                               self.add_instance(Instance(o, o))
                        elif o.type=='LIGHT':
                                self.lights.append(o)
                                if o.data.use_shadow:
                                        self.use_shadow = True
 
+               proto_map = {}
+               for i in itertools.chain(self.instances, self.blended_instances):
+                       p = proto_map.get(i.prototype)
+                       if p:
+                               i.prototype = p
+                       else:
+                               found = False
+                               for p in proto_map.values():
+                                       if is_same_object(i.prototype, p):
+                                               proto_map[i.prototype] = p
+                                               i.prototype = p
+                                               found = True
+                                               break
+
+                               if not found:
+                                       proto_map[i.prototype] = i.prototype
+                                       self.prototypes.append(i.prototype)
+
+       def add_instance(self, instance):
+               obj = instance.prototype
+               instance_list = self.instances
+               if obj.material_slots and obj.material_slots[0].material:
+                       mat = obj.material_slots[0].material
+                       if mat.blend_method=='BLEND':
+                               instance_list = self.blended_instances
+                       if mat.image_based_lighting:
+                               self.use_ibl = True
+               instance_list.append(instance)
+
        def get_chain(self):
                result = []
                if self.background_set: