]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/scene.py
Check the flat qualifier from the correct member
[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 ObjectPrototype:
14         def __init__(self, obj):
15                 self.name = obj.name
16                 self.object = obj
17                 self.instances = []
18                 self.use_array = False
19
20 class ObjectInstance:
21         def __init__(self, obj, prototype):
22                 if type(obj)==bpy.types.DepsgraphObjectInstance:
23                         self.name = None
24                         self.matrix_world = mathutils.Matrix(obj.matrix_world)
25                         self.rotation_mode = prototype.object.rotation_mode
26                 else:
27                         self.name = obj.name
28                         self.matrix_world = obj.matrix_world
29                         self.rotation_mode = obj.rotation_mode
30                 self.prototype = prototype
31
32 class Scene:
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
38                 self.prototypes = []
39                 self.instances = []
40                 self.blended_instances = []
41                 self.lights = []
42                 self.realtime_sky = False
43                 self.sun_light = None
44                 self.ambient_light = mathutils.Color((0.0, 0.0, 0.0))
45                 self.exposure = scene.view_settings.exposure
46
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
51                 if scene.world:
52                         out_node = next((n for n in scene.world.node_tree.nodes if n.type=='OUTPUT_WORLD'), None)
53                         if out_node:
54                                 from .util import get_linked_node_and_socket
55
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
61
62                         self.use_sky = scene.world.use_sky and scene.world.sun_light
63                         self.sun_light = scene.world.sun_light
64
65                 self.use_shadow = False
66                 self.use_ibl = False
67
68                 objects = scene.objects[:]
69                 objects.sort(key=lambda o:o.name)
70                 if obj_filter:
71                         objects = list(filter(obj_filter, objects))
72
73                 proto_map = {}
74                 for o in objects:
75                         if o.type=='MESH':
76                                 self.add_instance(o, o, proto_map)
77                         elif o.type=='LIGHT':
78                                 self.lights.append(o)
79                                 if o.data.use_shadow:
80                                         self.use_shadow = True
81
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)
85
86         def add_instance(self, obj, proto_obj, proto_map):
87                 prototype = proto_map.get(proto_obj)
88                 if not prototype:
89                         for p in proto_map.values():
90                                 if is_same_object(proto_obj, p.object):
91                                         prototype = p
92                                         break
93
94                         if not prototype:
95                                 prototype = ObjectPrototype(proto_obj)
96                                 self.prototypes.append(prototype)
97
98                         proto_map[proto_obj] = prototype
99
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:
106                                 self.use_ibl = True
107                         if mat.instancing:
108                                 prototype.use_array = True
109
110                 instance = ObjectInstance(obj, prototype)
111                 instance_list.append(instance)
112                 prototype.instances.append(instance)
113
114         def get_chain(self):
115                 result = []
116                 if self.background_set:
117                         result = self.background_set.get_chain()
118                 result.append(self)
119                 return result
120
121 def get_all_collections(collection):
122         result = [collection]
123         for c in collection.children:
124                 result += get_all_collections(c)
125         return result
126
127 def create_scene_from_current(ctx, *, selected_only=False, visible_only=True):
128         obj_filters = []
129
130         if selected_only:
131                 obj_filters.append(lambda o: o.select_get())
132
133         if visible_only:
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)
139
140         obj_filter = None
141         if len(obj_filters)==1:
142                 obj_filter = obj_filters[0]
143         if obj_filters:
144                 obj_filter = lambda o: all(f(o) for f in obj_filters)
145
146         return Scene(ctx.context.scene, obj_filter)
147
148 def create_scene(scene, *, visible_only=True):
149         obj_filter = None
150
151         if visible_only:
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
157
158         return Scene(scene, obj_filter)
159
160 def create_scene_chain(scene, cache, *, visible_only=True):
161         if cache is None:
162                 cache = {}
163
164         top = None
165         prev = None
166         while scene:
167                 converted = None
168                 if scene.name in cache:
169                         converted = cache[scene.name]
170                 else:
171                         converted = create_scene(scene, visible_only=visible_only)
172                         cache[scene.name] = converted
173
174                 if not top:
175                         top = converted
176                 if prev:
177                         prev.background_set = converted
178
179                 prev = converted
180                 scene = scene.background_set
181
182         return top