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)):
14 def __init__(self, obj, prototype):
15 if type(obj)==bpy.types.DepsgraphObjectInstance:
17 self.matrix_world = mathutils.Matrix(obj.matrix_world)
18 self.rotation_mode = prototype.rotation_mode
19 self.prototype = bpy.data.objects[prototype.name]
22 self.matrix_world = obj.matrix_world
23 self.rotation_mode = obj.rotation_mode
24 self.prototype = prototype
27 def __init__(self, scene, obj_filter=None):
28 self.name = scene.name
29 self.export_disposition = scene.export_disposition
30 self.background_set = None
31 self.camera = scene.camera
34 self.blended_instances = []
36 self.realtime_sky = False
38 self.ambient_light = mathutils.Color((0.0, 0.0, 0.0))
39 self.exposure = scene.view_settings.exposure
41 self.use_hdr = scene.use_hdr
42 self.use_ao = scene.eevee.use_gtao
43 self.ao_distance = scene.eevee.gtao_distance
44 self.ao_samples = scene.ao_samples
46 out_node = next((n for n in scene.world.node_tree.nodes if n.type=='OUTPUT_WORLD'), None)
48 from .util import get_linked_node_and_socket
50 surface_node, _ = get_linked_node_and_socket(scene.world.node_tree, out_node.inputs["Surface"])
51 if surface_node and surface_node.type=='BACKGROUND':
52 c = surface_node.inputs["Color"].default_value
53 s = surface_node.inputs["Strength"].default_value
54 self.ambient_light = mathutils.Color(c[:3])*s
56 self.use_sky = scene.world.use_sky and scene.world.sun_light
57 self.sun_light = scene.world.sun_light
59 self.use_shadow = False
62 objects = scene.objects[:]
63 objects.sort(key=lambda o:o.name)
65 objects = list(filter(obj_filter, objects))
69 self.add_instance(Instance(o, o))
73 self.use_shadow = True
75 for i in scene.view_layers[0].depsgraph.object_instances:
76 if i.is_instance and i.object.type=='MESH':
77 self.add_instance(Instance(i, i.object))
80 for i in itertools.chain(self.instances, self.blended_instances):
81 p = proto_map.get(i.prototype)
86 for p in proto_map.values():
87 if is_same_object(i.prototype, p):
88 proto_map[i.prototype] = p
94 proto_map[i.prototype] = i.prototype
95 self.prototypes.append(i.prototype)
97 def add_instance(self, instance):
98 obj = instance.prototype
99 instance_list = self.instances
100 if obj.material_slots and obj.material_slots[0].material:
101 mat = obj.material_slots[0].material
102 if mat.blend_method=='BLEND':
103 instance_list = self.blended_instances
104 if mat.image_based_lighting:
106 instance_list.append(instance)
110 if self.background_set:
111 result = self.background_set.get_chain()
115 def get_all_collections(collection):
116 result = [collection]
117 for c in collection.children:
118 result += get_all_collections(c)
121 def create_scene_from_current(ctx, *, selected_only=False, visible_only=True):
125 obj_filters.append(lambda o: o.select_get())
128 visible_names = set()
129 for c in get_all_collections(ctx.context.view_layer.layer_collection):
130 if not c.hide_viewport and not c.collection.hide_viewport:
131 visible_names.update(o.name for o in c.collection.objects)
132 obj_filters.append(lambda o: o.name in visible_names)
135 if len(obj_filters)==1:
136 obj_filter = obj_filters[0]
138 obj_filter = lambda o: all(f(o) for f in obj_filters)
140 return Scene(ctx.context.scene, obj_filter)
142 def create_scene(scene, *, visible_only=True):
146 visible_names = set()
147 for c in get_all_collections(scene.collection):
148 if not c.hide_viewport:
149 visible_names.update(o.name for o in c.objects)
150 obj_filter = lambda o: o.name in visible_names
152 return Scene(scene, obj_filter)
154 def create_scene_chain(scene, cache, *, visible_only=True):
162 if scene.name in cache:
163 converted = cache[scene.name]
165 converted = create_scene(scene, visible_only=visible_only)
166 cache[scene.name] = converted
171 prev.background_set = converted
174 scene = scene.background_set