]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/scene.py
079647ce00985a41252084cdfd6a3ca435b06626
[libs/gl.git] / blender / io_mspgl / scene.py
1 def is_same_object(obj1, obj2):
2         if obj1.data.name!=obj2.data.name:
3                 return False
4         if any(m1.name!=m2.name for m1, m2 in zip(obj1.material_slots, obj2.material_slots)):
5                 return False
6
7         return True
8
9 class Instance:
10         def __init__(self, obj, prototype):
11                 self.name = obj.name
12                 self.matrix_world = obj.matrix_world
13                 self.rotation_mode = obj.rotation_mode
14                 self.prototype = prototype.name
15
16 class Scene:
17         def __init__(self, scene, obj_filter=None):
18                 self.name = scene.name
19                 self.scene_type = scene.scene_type
20                 self.prototypes = []
21                 self.instances = []
22
23                 objects = [o for o in scene.objects if o.type=='MESH']
24                 objects.sort(key=lambda o:o.name)
25                 if obj_filter:
26                         objects = list(filter(obj_filter, objects))
27
28                 processed = set()
29                 for o in objects:
30                         if o.name in processed:
31                                 continue
32
33                         clones = [c for c in objects if is_same_object(o, c)]
34                         self.prototypes.append(o)
35                         for c in clones:
36                                 self.instances.append(Instance(c, o))
37                                 processed.add(c.name)
38
39 def get_all_collections(collection):
40         result = [collection]
41         for c in collection.children:
42                 result += get_all_collections(c)
43         return result
44
45 def create_scene_from_current(context, *, selected_only=False, visible_only=True):
46         obj_filters = []
47
48         if selected_only:
49                 obj_filters.append(lambda o: o.select_get())
50
51         if visible_only:
52                 visible_names = set()
53                 for c in get_all_collections(context.view_layer.layer_collection):
54                         if not c.hide_viewport and not c.collection.hide_viewport:
55                                 visible_names.update(o.name for o in c.collection.objects)
56                 obj_filters.append(lambda o: o.name in visible_names)
57
58         obj_filter = None
59         if len(obj_filters)==1:
60                 obj_filter = obj_filters[0]
61         if obj_filters:
62                 obj_filter = lambda o: all(f(o) for f in obj_filters)
63
64         return Scene(context.scene, obj_filter)