4 def is_same_object(obj1, obj2):
5 if obj1.data.name!=obj2.data.name:
7 if any(m1.name!=m2.name for m1, m2 in zip(obj1.material_slots, obj2.material_slots)):
13 def __init__(self, obj, prototype):
15 self.matrix_world = obj.matrix_world
16 self.rotation_mode = obj.rotation_mode
17 self.prototype = prototype
20 def __init__(self, scene, obj_filter=None):
21 self.name = scene.name
22 self.export_disposition = scene.export_disposition
23 self.background_set = None
24 self.camera = scene.camera
27 self.blended_instances = []
29 self.realtime_sky = False
31 self.ambient_light = mathutils.Color((0.0, 0.0, 0.0))
32 self.exposure = scene.view_settings.exposure
34 self.use_hdr = scene.use_hdr
35 self.use_ao = scene.eevee.use_gtao
36 self.ao_distance = scene.eevee.gtao_distance
37 self.ao_samples = scene.ao_samples
39 out_node = next((n for n in scene.world.node_tree.nodes if n.type=='OUTPUT_WORLD'), None)
41 from .util import get_linked_node_and_socket
43 surface_node, _ = get_linked_node_and_socket(scene.world.node_tree, out_node.inputs["Surface"])
44 if surface_node and surface_node.type=='BACKGROUND':
45 c = surface_node.inputs["Color"].default_value
46 s = surface_node.inputs["Strength"].default_value
47 self.ambient_light = mathutils.Color(c[:3])*s
49 self.use_sky = scene.world.use_sky and scene.world.sun_light
50 self.sun_light = scene.world.sun_light
52 self.use_shadow = False
55 objects = scene.objects[:]
56 objects.sort(key=lambda o:o.name)
58 objects = list(filter(obj_filter, objects))
62 self.add_instance(Instance(o, o))
66 self.use_shadow = True
69 for i in itertools.chain(self.instances, self.blended_instances):
70 p = proto_map.get(i.prototype)
75 for p in proto_map.values():
76 if is_same_object(i.prototype, p):
77 proto_map[i.prototype] = p
83 proto_map[i.prototype] = i.prototype
84 self.prototypes.append(i.prototype)
86 def add_instance(self, instance):
87 obj = instance.prototype
88 instance_list = self.instances
89 if obj.material_slots and obj.material_slots[0].material:
90 mat = obj.material_slots[0].material
91 if mat.blend_method=='BLEND':
92 instance_list = self.blended_instances
93 if mat.image_based_lighting:
95 instance_list.append(instance)
99 if self.background_set:
100 result = self.background_set.get_chain()
104 def get_all_collections(collection):
105 result = [collection]
106 for c in collection.children:
107 result += get_all_collections(c)
110 def create_scene_from_current(ctx, *, selected_only=False, visible_only=True):
114 obj_filters.append(lambda o: o.select_get())
117 visible_names = set()
118 for c in get_all_collections(ctx.context.view_layer.layer_collection):
119 if not c.hide_viewport and not c.collection.hide_viewport:
120 visible_names.update(o.name for o in c.collection.objects)
121 obj_filters.append(lambda o: o.name in visible_names)
124 if len(obj_filters)==1:
125 obj_filter = obj_filters[0]
127 obj_filter = lambda o: all(f(o) for f in obj_filters)
129 return Scene(ctx.context.scene, obj_filter)
131 def create_scene(scene, *, visible_only=True):
135 visible_names = set()
136 for c in get_all_collections(scene.collection):
137 if not c.hide_viewport:
138 visible_names.update(o.name for o in c.objects)
139 obj_filter = lambda o: o.name in visible_names
141 return Scene(scene, obj_filter)
143 def create_scene_chain(scene, cache, *, visible_only=True):
151 if scene.name in cache:
152 converted = cache[scene.name]
154 converted = create_scene(scene, visible_only=visible_only)
155 cache[scene.name] = converted
160 prev.background_set = converted
163 scene = scene.background_set