]> 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 079647ce00985a41252084cdfd6a3ca435b06626..cb62a1b0d40401996427a15b1da11e230783c017 100644 (file)
@@ -1,3 +1,7 @@
+import itertools
+import bpy
+import mathutils
+
 def is_same_object(obj1, obj2):
        if obj1.data.name!=obj2.data.name:
                return False
@@ -6,35 +10,111 @@ 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.prototype = prototype.name
+               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:
        def __init__(self, scene, obj_filter=None):
                self.name = scene.name
-               self.scene_type = scene.scene_type
+               self.export_disposition = scene.export_disposition
+               self.background_set = None
+               self.camera = scene.camera
                self.prototypes = []
                self.instances = []
+               self.blended_instances = []
+               self.lights = []
+               self.realtime_sky = False
+               self.sun_light = None
+               self.ambient_light = mathutils.Color((0.0, 0.0, 0.0))
+               self.exposure = scene.view_settings.exposure
+
+               self.use_hdr = scene.use_hdr
+               self.use_ao = scene.eevee.use_gtao
+               self.ao_distance = scene.eevee.gtao_distance
+               self.ao_samples = scene.ao_samples
+               if scene.world:
+                       out_node = next((n for n in scene.world.node_tree.nodes if n.type=='OUTPUT_WORLD'), None)
+                       if out_node:
+                               from .util import get_linked_node_and_socket
+
+                               surface_node, _ = get_linked_node_and_socket(scene.world.node_tree, out_node.inputs["Surface"])
+                               if surface_node and surface_node.type=='BACKGROUND':
+                                       c = surface_node.inputs["Color"].default_value
+                                       s = surface_node.inputs["Strength"].default_value
+                                       self.ambient_light = mathutils.Color(c[:3])*s
 
-               objects = [o for o in scene.objects if o.type=='MESH']
+                       self.use_sky = scene.world.use_sky and scene.world.sun_light
+                       self.sun_light = scene.world.sun_light
+
+               self.use_shadow = False
+               self.use_ibl = False
+
+               objects = scene.objects[:]
                objects.sort(key=lambda o:o.name)
                if obj_filter:
                        objects = list(filter(obj_filter, objects))
 
-               processed = set()
+               proto_map = {}
                for o in objects:
-                       if o.name in processed:
-                               continue
+                       if o.type=='MESH':
+                               self.add_instance(o, o, proto_map)
+                       elif o.type=='LIGHT':
+                               self.lights.append(o)
+                               if o.data.use_shadow:
+                                       self.use_shadow = True
+
+               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
 
-                       clones = [c for c in objects if is_same_object(o, c)]
-                       self.prototypes.append(o)
-                       for c in clones:
-                               self.instances.append(Instance(c, o))
-                               processed.add(c.name)
+               instance_list = self.instances
+               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 = []
+               if self.background_set:
+                       result = self.background_set.get_chain()
+               result.append(self)
+               return result
 
 def get_all_collections(collection):
        result = [collection]
@@ -42,7 +122,7 @@ def get_all_collections(collection):
                result += get_all_collections(c)
        return result
 
-def create_scene_from_current(context, *, selected_only=False, visible_only=True):
+def create_scene_from_current(ctx, *, selected_only=False, visible_only=True):
        obj_filters = []
 
        if selected_only:
@@ -50,7 +130,7 @@ def create_scene_from_current(context, *, selected_only=False, visible_only=True
 
        if visible_only:
                visible_names = set()
-               for c in get_all_collections(context.view_layer.layer_collection):
+               for c in get_all_collections(ctx.context.view_layer.layer_collection):
                        if not c.hide_viewport and not c.collection.hide_viewport:
                                visible_names.update(o.name for o in c.collection.objects)
                obj_filters.append(lambda o: o.name in visible_names)
@@ -61,4 +141,40 @@ def create_scene_from_current(context, *, selected_only=False, visible_only=True
        if obj_filters:
                obj_filter = lambda o: all(f(o) for f in obj_filters)
 
-       return Scene(context.scene, obj_filter)
+       return Scene(ctx.context.scene, obj_filter)
+
+def create_scene(scene, *, visible_only=True):
+       obj_filter = None
+
+       if visible_only:
+               visible_names = set()
+               for c in get_all_collections(scene.collection):
+                       if not c.hide_viewport:
+                               visible_names.update(o.name for o in c.objects)
+               obj_filter = lambda o: o.name in visible_names
+
+       return Scene(scene, obj_filter)
+
+def create_scene_chain(scene, cache, *, visible_only=True):
+       if cache is None:
+               cache = {}
+
+       top = None
+       prev = None
+       while scene:
+               converted = None
+               if scene.name in cache:
+                       converted = cache[scene.name]
+               else:
+                       converted = create_scene(scene, visible_only=visible_only)
+                       cache[scene.name] = converted
+
+               if not top:
+                       top = converted
+               if prev:
+                       prev.background_set = converted
+
+               prev = converted
+               scene = scene.background_set
+
+       return top