]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/scene.py
Set color write mask if no blending is used
[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
17
18 class Scene:
19         def __init__(self, scene, obj_filter=None):
20                 self.name = scene.name
21                 self.export_disposition = scene.export_disposition
22                 self.background_set = None
23                 self.camera = scene.camera
24                 self.prototypes = []
25                 self.instances = []
26                 self.blended_instances = []
27                 self.lights = []
28                 self.realtime_sky = False
29                 self.sun_light = None
30                 self.ambient_light = mathutils.Color((0.0, 0.0, 0.0))
31                 self.exposure = scene.view_settings.exposure
32
33                 self.use_hdr = scene.use_hdr
34                 self.use_ao = scene.eevee.use_gtao
35                 self.ao_distance = scene.eevee.gtao_distance
36                 self.ao_samples = scene.ao_samples
37                 if scene.world:
38                         out_node = next((n for n in scene.world.node_tree.nodes if n.type=='OUTPUT_WORLD'), None)
39                         if out_node:
40                                 from .util import get_linked_node_and_socket
41
42                                 surface_node, _ = get_linked_node_and_socket(scene.world.node_tree, out_node.inputs["Surface"])
43                                 if surface_node and surface_node.type=='BACKGROUND':
44                                         c = surface_node.inputs["Color"].default_value
45                                         s = surface_node.inputs["Strength"].default_value
46                                         self.ambient_light = mathutils.Color(c[:3])*s
47
48                         self.use_sky = scene.world.use_sky and scene.world.sun_light
49                         self.sun_light = scene.world.sun_light
50
51                 self.use_shadow = False
52                 self.use_ibl = False
53
54                 objects = scene.objects[:]
55                 objects.sort(key=lambda o:o.name)
56                 if obj_filter:
57                         objects = list(filter(obj_filter, objects))
58
59                 processed = set()
60                 for o in objects:
61                         if o.name in processed:
62                                 continue
63
64                         if o.type=='MESH':
65                                 clones = [c for c in objects if is_same_object(o, c)]
66                                 self.prototypes.append(o)
67                                 instance_list = self.instances
68                                 if o.material_slots and o.material_slots[0].material:
69                                         mat = o.material_slots[0].material
70                                         if mat.blend_method=='BLEND':
71                                                 instance_list = self.blended_instances
72                                         if mat.image_based_lighting:
73                                                 self.use_ibl = True
74                                 for c in clones:
75                                         instance_list.append(Instance(c, o))
76                                         processed.add(c.name)
77                         elif o.type=='LIGHT':
78                                 self.lights.append(o)
79                                 if o.data.use_shadow:
80                                         self.use_shadow = True
81
82         def get_chain(self):
83                 result = []
84                 if self.background_set:
85                         result = self.background_set.get_chain()
86                 result.append(self)
87                 return result
88
89 def get_all_collections(collection):
90         result = [collection]
91         for c in collection.children:
92                 result += get_all_collections(c)
93         return result
94
95 def create_scene_from_current(context, *, selected_only=False, visible_only=True):
96         obj_filters = []
97
98         if selected_only:
99                 obj_filters.append(lambda o: o.select_get())
100
101         if visible_only:
102                 visible_names = set()
103                 for c in get_all_collections(context.view_layer.layer_collection):
104                         if not c.hide_viewport and not c.collection.hide_viewport:
105                                 visible_names.update(o.name for o in c.collection.objects)
106                 obj_filters.append(lambda o: o.name in visible_names)
107
108         obj_filter = None
109         if len(obj_filters)==1:
110                 obj_filter = obj_filters[0]
111         if obj_filters:
112                 obj_filter = lambda o: all(f(o) for f in obj_filters)
113
114         return Scene(context.scene, obj_filter)
115
116 def create_scene(scene, *, visible_only=True):
117         obj_filter = None
118
119         if visible_only:
120                 visible_names = set()
121                 for c in get_all_collections(scene.collection):
122                         if not c.hide_viewport:
123                                 visible_names.update(o.name for o in c.objects)
124                 obj_filter = lambda o: o.name in visible_names
125
126         return Scene(scene, obj_filter)
127
128 def create_scene_chain(scene, cache, *, visible_only=True):
129         if cache is None:
130                 cache = {}
131
132         top = None
133         prev = None
134         while scene:
135                 converted = None
136                 if scene.name in cache:
137                         converted = cache[scene.name]
138                 else:
139                         converted = create_scene(scene, visible_only=visible_only)
140                         cache[scene.name] = converted
141
142                 if not top:
143                         top = converted
144                 if prev:
145                         prev.background_set = converted
146
147                 prev = converted
148                 scene = scene.background_set
149
150         return top