]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/scene.py
Further refactoring of instance handling in the Blender exporter
[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
108                 instance = ObjectInstance(obj, prototype)
109                 instance_list.append(instance)
110                 prototype.instances.append(instance)
111
112         def get_chain(self):
113                 result = []
114                 if self.background_set:
115                         result = self.background_set.get_chain()
116                 result.append(self)
117                 return result
118
119 def get_all_collections(collection):
120         result = [collection]
121         for c in collection.children:
122                 result += get_all_collections(c)
123         return result
124
125 def create_scene_from_current(ctx, *, selected_only=False, visible_only=True):
126         obj_filters = []
127
128         if selected_only:
129                 obj_filters.append(lambda o: o.select_get())
130
131         if visible_only:
132                 visible_names = set()
133                 for c in get_all_collections(ctx.context.view_layer.layer_collection):
134                         if not c.hide_viewport and not c.collection.hide_viewport:
135                                 visible_names.update(o.name for o in c.collection.objects)
136                 obj_filters.append(lambda o: o.name in visible_names)
137
138         obj_filter = None
139         if len(obj_filters)==1:
140                 obj_filter = obj_filters[0]
141         if obj_filters:
142                 obj_filter = lambda o: all(f(o) for f in obj_filters)
143
144         return Scene(ctx.context.scene, obj_filter)
145
146 def create_scene(scene, *, visible_only=True):
147         obj_filter = None
148
149         if visible_only:
150                 visible_names = set()
151                 for c in get_all_collections(scene.collection):
152                         if not c.hide_viewport:
153                                 visible_names.update(o.name for o in c.objects)
154                 obj_filter = lambda o: o.name in visible_names
155
156         return Scene(scene, obj_filter)
157
158 def create_scene_chain(scene, cache, *, visible_only=True):
159         if cache is None:
160                 cache = {}
161
162         top = None
163         prev = None
164         while scene:
165                 converted = None
166                 if scene.name in cache:
167                         converted = cache[scene.name]
168                 else:
169                         converted = create_scene(scene, visible_only=visible_only)
170                         cache[scene.name] = converted
171
172                 if not top:
173                         top = converted
174                 if prev:
175                         prev.background_set = converted
176
177                 prev = converted
178                 scene = scene.background_set
179
180         return top