3 def is_same_object(obj1, obj2):
4 if obj1.data.name!=obj2.data.name:
6 if any(m1.name!=m2.name for m1, m2 in zip(obj1.material_slots, obj2.material_slots)):
12 def __init__(self, obj, prototype):
14 self.matrix_world = obj.matrix_world
15 self.rotation_mode = obj.rotation_mode
16 self.prototype = prototype
19 def __init__(self, scene, obj_filter=None):
20 self.name = scene.name
21 self.export_disposition = scene.export_disposition
22 self.background_set = None
23 self.camera = scene.camera
26 self.blended_instances = []
28 self.realtime_sky = False
30 self.ambient_light = mathutils.Color((0.0, 0.0, 0.0))
31 self.exposure = scene.view_settings.exposure
33 self.use_hdr = scene.use_hdr
34 self.use_ao = scene.eevee.use_gtao
35 self.ao_distance = scene.eevee.gtao_distance
36 self.ao_samples = scene.ao_samples
38 out_node = next((n for n in scene.world.node_tree.nodes if n.type=='OUTPUT_WORLD'), None)
40 from .util import get_linked_node_and_socket
42 surface_node, _ = get_linked_node_and_socket(scene.world.node_tree, out_node.inputs["Surface"])
43 if surface_node and surface_node.type=='BACKGROUND':
44 c = surface_node.inputs["Color"].default_value
45 s = surface_node.inputs["Strength"].default_value
46 self.ambient_light = mathutils.Color(c[:3])*s
48 self.use_sky = scene.world.use_sky and scene.world.sun_light
49 self.sun_light = scene.world.sun_light
51 self.use_shadow = False
54 objects = scene.objects[:]
55 objects.sort(key=lambda o:o.name)
57 objects = list(filter(obj_filter, objects))
61 if o.name in processed:
65 clones = [c for c in objects if is_same_object(o, c)]
66 self.prototypes.append(o)
67 instance_list = self.instances
68 if o.material_slots and o.material_slots[0].material:
69 mat = o.material_slots[0].material
70 if mat.blend_method=='BLEND':
71 instance_list = self.blended_instances
72 if mat.image_based_lighting:
75 instance_list.append(Instance(c, o))
80 self.use_shadow = True
84 if self.background_set:
85 result = self.background_set.get_chain()
89 def get_all_collections(collection):
91 for c in collection.children:
92 result += get_all_collections(c)
95 def create_scene_from_current(context, *, selected_only=False, visible_only=True):
99 obj_filters.append(lambda o: o.select_get())
102 visible_names = set()
103 for c in get_all_collections(context.view_layer.layer_collection):
104 if not c.hide_viewport and not c.collection.hide_viewport:
105 visible_names.update(o.name for o in c.collection.objects)
106 obj_filters.append(lambda o: o.name in visible_names)
109 if len(obj_filters)==1:
110 obj_filter = obj_filters[0]
112 obj_filter = lambda o: all(f(o) for f in obj_filters)
114 return Scene(context.scene, obj_filter)
116 def create_scene(scene, *, visible_only=True):
120 visible_names = set()
121 for c in get_all_collections(scene.collection):
122 if not c.hide_viewport:
123 visible_names.update(o.name for o in c.objects)
124 obj_filter = lambda o: o.name in visible_names
126 return Scene(scene, obj_filter)
128 def create_scene_chain(scene, cache, *, visible_only=True):
136 if scene.name in cache:
137 converted = cache[scene.name]
139 converted = create_scene(scene, visible_only=visible_only)
140 cache[scene.name] = converted
145 prev.background_set = converted
148 scene = scene.background_set