]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/scene.py
Export the scene's background color as ambient light color
[libs/gl.git] / blender / io_mspgl / scene.py
1 import mathutils
2
3 def is_same_object(obj1, obj2):
4         if obj1.data.name!=obj2.data.name:
5                 return False
6         if any(m1.name!=m2.name for m1, m2 in zip(obj1.material_slots, obj2.material_slots)):
7                 return False
8
9         return True
10
11 class Instance:
12         def __init__(self, obj, prototype):
13                 self.name = obj.name
14                 self.matrix_world = obj.matrix_world
15                 self.rotation_mode = obj.rotation_mode
16                 self.prototype = prototype.name
17
18 class Scene:
19         def __init__(self, scene, obj_filter=None):
20                 self.name = scene.name
21                 self.scene_type = scene.scene_type
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.lights = []
28                 self.ambient_light = mathutils.Color((0.0, 0.0, 0.0))
29                 self.exposure = scene.view_settings.exposure
30
31                 self.use_hdr = False
32                 if scene.world:
33                         self.use_hdr = scene.world.use_hdr
34
35                         out_node = next((n for n in scene.world.node_tree.nodes if n.type=='OUTPUT_WORLD'), None)
36                         if out_node:
37                                 from .util import get_linked_node_and_socket
38
39                                 surface_node, _ = get_linked_node_and_socket(scene.world.node_tree, out_node.inputs["Surface"])
40                                 if surface_node and surface_node.type=='BACKGROUND':
41                                         c = surface_node.inputs["Color"].default_value
42                                         s = surface_node.inputs["Strength"].default_value
43                                         self.ambient_light = mathutils.Color(c[:3])*s
44
45                 objects = scene.objects[:]
46                 objects.sort(key=lambda o:o.name)
47                 if obj_filter:
48                         objects = list(filter(obj_filter, objects))
49
50                 processed = set()
51                 for o in objects:
52                         if o.name in processed:
53                                 continue
54
55                         if o.type=='MESH':
56                                 clones = [c for c in objects if is_same_object(o, c)]
57                                 self.prototypes.append(o)
58                                 for c in clones:
59                                         self.instances.append(Instance(c, o))
60                                         processed.add(c.name)
61                         elif o.type=='LIGHT':
62                                 self.lights.append(o)
63
64         def get_chain(self):
65                 result = []
66                 if self.background_set:
67                         result = self.background_set.get_chain()
68                 result.append(self)
69                 return result
70
71 def get_all_collections(collection):
72         result = [collection]
73         for c in collection.children:
74                 result += get_all_collections(c)
75         return result
76
77 def create_scene_from_current(context, *, selected_only=False, visible_only=True):
78         obj_filters = []
79
80         if selected_only:
81                 obj_filters.append(lambda o: o.select_get())
82
83         if visible_only:
84                 visible_names = set()
85                 for c in get_all_collections(context.view_layer.layer_collection):
86                         if not c.hide_viewport and not c.collection.hide_viewport:
87                                 visible_names.update(o.name for o in c.collection.objects)
88                 obj_filters.append(lambda o: o.name in visible_names)
89
90         obj_filter = None
91         if len(obj_filters)==1:
92                 obj_filter = obj_filters[0]
93         if obj_filters:
94                 obj_filter = lambda o: all(f(o) for f in obj_filters)
95
96         return Scene(context.scene, obj_filter)
97
98 def create_scene(scene, *, visible_only=True):
99         obj_filter = None
100
101         if visible_only:
102                 visible_names = set()
103                 for c in get_all_collections(scene.collection):
104                         if not c.hide_viewport:
105                                 visible_names.update(o.name for o in c.objects)
106                 obj_filter = lambda o: o.name in visible_names
107
108         return Scene(scene, obj_filter)
109
110 def create_scene_chain(scene, cache, *, visible_only=True):
111         if cache is None:
112                 cache = {}
113
114         top = None
115         prev = None
116         while scene:
117                 converted = None
118                 if scene.name in cache:
119                         converted = cache[scene.name]
120                 else:
121                         converted = create_scene(scene, visible_only=visible_only)
122                         cache[scene.name] = converted
123
124                 if not top:
125                         top = converted
126                 if prev:
127                         prev.background_set = converted
128
129                 prev = converted
130                 scene = scene.background_set
131
132         return top