+import itertools
+import bpy
import mathutils
def is_same_object(obj1, obj2):
class Instance:
def __init__(self, obj, prototype):
- self.name = obj.name
- self.matrix_world = obj.matrix_world
- self.rotation_mode = obj.rotation_mode
- self.prototype = prototype
+ if type(obj)==bpy.types.DepsgraphObjectInstance:
+ self.name = None
+ self.matrix_world = mathutils.Matrix(obj.matrix_world)
+ self.rotation_mode = prototype.rotation_mode
+ self.prototype = bpy.data.objects[prototype.name]
+ 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.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
s = surface_node.inputs["Strength"].default_value
self.ambient_light = mathutils.Color(c[:3])*s
+ 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()
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 and o.material_slots[0].material.blend_method=='BLEND':
- instance_list = self.blended_instances
- 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
+
+ for i in scene.view_layers[0].depsgraph.object_instances:
+ if i.is_instance and i.object.type=='MESH':
+ self.add_instance(Instance(i, i.object))
+
+ 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 = []
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:
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)
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