5 def is_same_object(obj1, obj2):
6 if obj1.data.name!=obj2.data.name:
8 if any(m1.name!=m2.name for m1, m2 in zip(obj1.material_slots, obj2.material_slots)):
13 class ObjectPrototype:
14 def __init__(self, obj):
18 self.use_array = False
21 def __init__(self, obj, prototype):
22 if type(obj)==bpy.types.DepsgraphObjectInstance:
24 self.matrix_world = mathutils.Matrix(obj.matrix_world)
25 self.rotation_mode = prototype.object.rotation_mode
28 self.matrix_world = obj.matrix_world
29 self.rotation_mode = obj.rotation_mode
30 self.prototype = prototype
33 def __init__(self, scene, obj_filter=None):
34 self.name = scene.name
35 self.export_disposition = scene.export_disposition
36 self.background_set = None
37 self.camera = scene.camera
40 self.blended_instances = []
42 self.realtime_sky = False
44 self.ambient_light = mathutils.Color((0.0, 0.0, 0.0))
45 self.exposure = scene.view_settings.exposure
47 self.use_hdr = scene.use_hdr
48 self.use_ao = scene.eevee.use_gtao
49 self.ao_distance = scene.eevee.gtao_distance
50 self.ao_samples = scene.ao_samples
52 out_node = next((n for n in scene.world.node_tree.nodes if n.type=='OUTPUT_WORLD'), None)
54 from .util import get_linked_node_and_socket
56 surface_node, _ = get_linked_node_and_socket(scene.world.node_tree, out_node.inputs["Surface"])
57 if surface_node and surface_node.type=='BACKGROUND':
58 c = surface_node.inputs["Color"].default_value
59 s = surface_node.inputs["Strength"].default_value
60 self.ambient_light = mathutils.Color(c[:3])*s
62 self.use_sky = scene.world.use_sky and scene.world.sun_light
63 self.sun_light = scene.world.sun_light
65 self.use_shadow = False
68 objects = scene.objects[:]
69 objects.sort(key=lambda o:o.name)
71 objects = list(filter(obj_filter, objects))
76 self.add_instance(o, o, proto_map)
80 self.use_shadow = True
82 for i in scene.view_layers[0].depsgraph.object_instances:
83 if i.is_instance and i.object.type=='MESH':
84 self.add_instance(i, bpy.data.objects[i.object.name], proto_map)
86 def add_instance(self, obj, proto_obj, proto_map):
87 prototype = proto_map.get(proto_obj)
89 for p in proto_map.values():
90 if is_same_object(proto_obj, p.object):
95 prototype = ObjectPrototype(proto_obj)
96 self.prototypes.append(prototype)
98 proto_map[proto_obj] = prototype
100 instance_list = self.instances
101 if proto_obj.material_slots and proto_obj.material_slots[0].material:
102 mat = proto_obj.material_slots[0].material
103 if mat.blend_method=='BLEND':
104 instance_list = self.blended_instances
105 if mat.image_based_lighting:
108 prototype.use_array = True
110 instance = ObjectInstance(obj, prototype)
111 instance_list.append(instance)
112 prototype.instances.append(instance)
116 if self.background_set:
117 result = self.background_set.get_chain()
121 def get_all_collections(collection):
122 result = [collection]
123 for c in collection.children:
124 result += get_all_collections(c)
127 def create_scene_from_current(ctx, *, selected_only=False, visible_only=True):
131 obj_filters.append(lambda o: o.select_get())
134 visible_names = set()
135 for c in get_all_collections(ctx.context.view_layer.layer_collection):
136 if not c.hide_viewport and not c.collection.hide_viewport:
137 visible_names.update(o.name for o in c.collection.objects)
138 obj_filters.append(lambda o: o.name in visible_names)
141 if len(obj_filters)==1:
142 obj_filter = obj_filters[0]
144 obj_filter = lambda o: all(f(o) for f in obj_filters)
146 return Scene(ctx.context.scene, obj_filter)
148 def create_scene(scene, *, visible_only=True):
152 visible_names = set()
153 for c in get_all_collections(scene.collection):
154 if not c.hide_viewport:
155 visible_names.update(o.name for o in c.objects)
156 obj_filter = lambda o: o.name in visible_names
158 return Scene(scene, obj_filter)
160 def create_scene_chain(scene, cache, *, visible_only=True):
168 if scene.name in cache:
169 converted = cache[scene.name]
171 converted = create_scene(scene, visible_only=visible_only)
172 cache[scene.name] = converted
177 prev.background_set = converted
180 scene = scene.background_set