]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/scene.py
4cb6be5baf63a45d521511a42d28d86de0c86c43
[libs/gl.git] / blender / io_mspgl / scene.py
1 import itertools
2 import bpy
3 import mathutils
4
5 def is_same_object(obj1, obj2):
6         if obj1.data.name!=obj2.data.name:
7                 return False
8         if any(m1.name!=m2.name for m1, m2 in zip(obj1.material_slots, obj2.material_slots)):
9                 return False
10
11         return True
12
13 class Instance:
14         def __init__(self, obj, prototype):
15                 if type(obj)==bpy.types.DepsgraphObjectInstance:
16                         self.name = None
17                         self.matrix_world = mathutils.Matrix(obj.matrix_world)
18                         self.rotation_mode = prototype.rotation_mode
19                         self.prototype = bpy.data.objects[prototype.name]
20                 else:
21                         self.name = obj.name
22                         self.matrix_world = obj.matrix_world
23                         self.rotation_mode = obj.rotation_mode
24                         self.prototype = prototype
25
26 class Scene:
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
32                 self.prototypes = []
33                 self.instances = []
34                 self.blended_instances = []
35                 self.lights = []
36                 self.realtime_sky = False
37                 self.sun_light = None
38                 self.ambient_light = mathutils.Color((0.0, 0.0, 0.0))
39                 self.exposure = scene.view_settings.exposure
40
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
45                 if scene.world:
46                         out_node = next((n for n in scene.world.node_tree.nodes if n.type=='OUTPUT_WORLD'), None)
47                         if out_node:
48                                 from .util import get_linked_node_and_socket
49
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
55
56                         self.use_sky = scene.world.use_sky and scene.world.sun_light
57                         self.sun_light = scene.world.sun_light
58
59                 self.use_shadow = False
60                 self.use_ibl = False
61
62                 objects = scene.objects[:]
63                 objects.sort(key=lambda o:o.name)
64                 if obj_filter:
65                         objects = list(filter(obj_filter, objects))
66
67                 for o in objects:
68                         if o.type=='MESH':
69                                 self.add_instance(Instance(o, o))
70                         elif o.type=='LIGHT':
71                                 self.lights.append(o)
72                                 if o.data.use_shadow:
73                                         self.use_shadow = True
74
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))
78
79                 proto_map = {}
80                 for i in itertools.chain(self.instances, self.blended_instances):
81                         p = proto_map.get(i.prototype)
82                         if p:
83                                 i.prototype = p
84                         else:
85                                 found = False
86                                 for p in proto_map.values():
87                                         if is_same_object(i.prototype, p):
88                                                 proto_map[i.prototype] = p
89                                                 i.prototype = p
90                                                 found = True
91                                                 break
92
93                                 if not found:
94                                         proto_map[i.prototype] = i.prototype
95                                         self.prototypes.append(i.prototype)
96
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:
105                                 self.use_ibl = True
106                 instance_list.append(instance)
107
108         def get_chain(self):
109                 result = []
110                 if self.background_set:
111                         result = self.background_set.get_chain()
112                 result.append(self)
113                 return result
114
115 def get_all_collections(collection):
116         result = [collection]
117         for c in collection.children:
118                 result += get_all_collections(c)
119         return result
120
121 def create_scene_from_current(ctx, *, selected_only=False, visible_only=True):
122         obj_filters = []
123
124         if selected_only:
125                 obj_filters.append(lambda o: o.select_get())
126
127         if visible_only:
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)
133
134         obj_filter = None
135         if len(obj_filters)==1:
136                 obj_filter = obj_filters[0]
137         if obj_filters:
138                 obj_filter = lambda o: all(f(o) for f in obj_filters)
139
140         return Scene(ctx.context.scene, obj_filter)
141
142 def create_scene(scene, *, visible_only=True):
143         obj_filter = None
144
145         if visible_only:
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
151
152         return Scene(scene, obj_filter)
153
154 def create_scene_chain(scene, cache, *, visible_only=True):
155         if cache is None:
156                 cache = {}
157
158         top = None
159         prev = None
160         while scene:
161                 converted = None
162                 if scene.name in cache:
163                         converted = cache[scene.name]
164                 else:
165                         converted = create_scene(scene, visible_only=visible_only)
166                         cache[scene.name] = converted
167
168                 if not top:
169                         top = converted
170                 if prev:
171                         prev.background_set = converted
172
173                 prev = converted
174                 scene = scene.background_set
175
176         return top