]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/scene.py
Further refactoring of instance handling in the Blender exporter
[libs/gl.git] / blender / io_mspgl / scene.py
index 7a0fa2f037d290108e8e3d951d8a7617874208c5..cb62a1b0d40401996427a15b1da11e230783c017 100644 (file)
@@ -1,4 +1,5 @@
 import itertools
+import bpy
 import mathutils
 
 def is_same_object(obj1, obj2):
@@ -9,11 +10,23 @@ def is_same_object(obj1, obj2):
 
        return True
 
-class Instance:
-       def __init__(self, obj, prototype):
+class ObjectPrototype:
+       def __init__(self, obj):
                self.name = obj.name
-               self.matrix_world = obj.matrix_world
-               self.rotation_mode = obj.rotation_mode
+               self.object = obj
+               self.instances = []
+               self.use_array = False
+
+class ObjectInstance:
+       def __init__(self, obj, prototype):
+               if type(obj)==bpy.types.DepsgraphObjectInstance:
+                       self.name = None
+                       self.matrix_world = mathutils.Matrix(obj.matrix_world)
+                       self.rotation_mode = prototype.object.rotation_mode
+               else:
+                       self.name = obj.name
+                       self.matrix_world = obj.matrix_world
+                       self.rotation_mode = obj.rotation_mode
                self.prototype = prototype
 
 class Scene:
@@ -57,42 +70,44 @@ class Scene:
                if obj_filter:
                        objects = list(filter(obj_filter, objects))
 
+               proto_map = {}
                for o in objects:
                        if o.type=='MESH':
-                               self.add_instance(Instance(o, o))
+                               self.add_instance(o, o, proto_map)
                        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
+               for i in scene.view_layers[0].depsgraph.object_instances:
+                       if i.is_instance and i.object.type=='MESH':
+                               self.add_instance(i, bpy.data.objects[i.object.name], proto_map)
+
+       def add_instance(self, obj, proto_obj, proto_map):
+               prototype = proto_map.get(proto_obj)
+               if not prototype:
+                       for p in proto_map.values():
+                               if is_same_object(proto_obj, p.object):
+                                       prototype = p
+                                       break
+
+                       if not prototype:
+                               prototype = ObjectPrototype(proto_obj)
+                               self.prototypes.append(prototype)
+
+                       proto_map[proto_obj] = prototype
+
                instance_list = self.instances
-               if obj.material_slots and obj.material_slots[0].material:
-                       mat = obj.material_slots[0].material
+               if proto_obj.material_slots and proto_obj.material_slots[0].material:
+                       mat = proto_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 = ObjectInstance(obj, prototype)
                instance_list.append(instance)
+               prototype.instances.append(instance)
 
        def get_chain(self):
                result = []