]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/scene.py
7a0fa2f037d290108e8e3d951d8a7617874208c5
[libs/gl.git] / blender / io_mspgl / scene.py
1 import itertools
2 import mathutils
3
4 def is_same_object(obj1, obj2):
5         if obj1.data.name!=obj2.data.name:
6                 return False
7         if any(m1.name!=m2.name for m1, m2 in zip(obj1.material_slots, obj2.material_slots)):
8                 return False
9
10         return True
11
12 class Instance:
13         def __init__(self, obj, prototype):
14                 self.name = obj.name
15                 self.matrix_world = obj.matrix_world
16                 self.rotation_mode = obj.rotation_mode
17                 self.prototype = prototype
18
19 class Scene:
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
25                 self.prototypes = []
26                 self.instances = []
27                 self.blended_instances = []
28                 self.lights = []
29                 self.realtime_sky = False
30                 self.sun_light = None
31                 self.ambient_light = mathutils.Color((0.0, 0.0, 0.0))
32                 self.exposure = scene.view_settings.exposure
33
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
38                 if scene.world:
39                         out_node = next((n for n in scene.world.node_tree.nodes if n.type=='OUTPUT_WORLD'), None)
40                         if out_node:
41                                 from .util import get_linked_node_and_socket
42
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
48
49                         self.use_sky = scene.world.use_sky and scene.world.sun_light
50                         self.sun_light = scene.world.sun_light
51
52                 self.use_shadow = False
53                 self.use_ibl = False
54
55                 objects = scene.objects[:]
56                 objects.sort(key=lambda o:o.name)
57                 if obj_filter:
58                         objects = list(filter(obj_filter, objects))
59
60                 for o in objects:
61                         if o.type=='MESH':
62                                 self.add_instance(Instance(o, o))
63                         elif o.type=='LIGHT':
64                                 self.lights.append(o)
65                                 if o.data.use_shadow:
66                                         self.use_shadow = True
67
68                 proto_map = {}
69                 for i in itertools.chain(self.instances, self.blended_instances):
70                         p = proto_map.get(i.prototype)
71                         if p:
72                                 i.prototype = p
73                         else:
74                                 found = False
75                                 for p in proto_map.values():
76                                         if is_same_object(i.prototype, p):
77                                                 proto_map[i.prototype] = p
78                                                 i.prototype = p
79                                                 found = True
80                                                 break
81
82                                 if not found:
83                                         proto_map[i.prototype] = i.prototype
84                                         self.prototypes.append(i.prototype)
85
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:
94                                 self.use_ibl = True
95                 instance_list.append(instance)
96
97         def get_chain(self):
98                 result = []
99                 if self.background_set:
100                         result = self.background_set.get_chain()
101                 result.append(self)
102                 return result
103
104 def get_all_collections(collection):
105         result = [collection]
106         for c in collection.children:
107                 result += get_all_collections(c)
108         return result
109
110 def create_scene_from_current(ctx, *, selected_only=False, visible_only=True):
111         obj_filters = []
112
113         if selected_only:
114                 obj_filters.append(lambda o: o.select_get())
115
116         if visible_only:
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)
122
123         obj_filter = None
124         if len(obj_filters)==1:
125                 obj_filter = obj_filters[0]
126         if obj_filters:
127                 obj_filter = lambda o: all(f(o) for f in obj_filters)
128
129         return Scene(ctx.context.scene, obj_filter)
130
131 def create_scene(scene, *, visible_only=True):
132         obj_filter = None
133
134         if visible_only:
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
140
141         return Scene(scene, obj_filter)
142
143 def create_scene_chain(scene, cache, *, visible_only=True):
144         if cache is None:
145                 cache = {}
146
147         top = None
148         prev = None
149         while scene:
150                 converted = None
151                 if scene.name in cache:
152                         converted = cache[scene.name]
153                 else:
154                         converted = create_scene(scene, visible_only=visible_only)
155                         cache[scene.name] = converted
156
157                 if not top:
158                         top = converted
159                 if prev:
160                         prev.background_set = converted
161
162                 prev = converted
163                 scene = scene.background_set
164
165         return top