-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
56 lines (41 loc) · 1.73 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Read the collections of a blender file"""
import json
import sys
import bpy
def traverse_tree(current: bpy.types.Collection, *parent_obj):
"""Get object and the parent of the object"""
yield current, parent_obj
for child in current.children:
yield from traverse_tree(child, *parent_obj, current.name)
def find_collection_info(scene_coll: bpy.types.Collection) -> dict:
coll_list = {}
for c, parents in traverse_tree(scene_coll):
coll_list[c.name] = {
"current_parent": list(parents)[-1] if len(list(parents)) > 0 else None,
"parents": list(parents),
"inner_meshes": [obj.name for obj in c.objects if obj.type == "MESH"],
"inner_collections": [obj.name for obj in c.children],
}
return coll_list
def get_parent_collections_of_mesh(coll_list: dict, as_path: bool = False) -> dict:
meshes_for_tags = {}
for collection, values in coll_list.items():
parents = values.get("parents", [])
parents.append(collection)
for mesh in values.get("inner_meshes", []):
if as_path:
meshes_for_tags[mesh] = "/".join(parents)
else:
meshes_for_tags[mesh] = parents
return meshes_for_tags
def main():
bpy.ops.wm.open_mainfile(filepath=sys.argv[1])
scene_coll = bpy.context.scene.collection
coll_list = find_collection_info(scene_coll)
with open("blender_collections.json", "w", encoding="utf-8") as outfile:
json.dump(coll_list, outfile)
meshes_for_tags = get_parent_collections_of_mesh(coll_list, True)
with open("meshes_for_tags.json", "w", encoding="utf-8") as outfile:
json.dump(meshes_for_tags, outfile)
if __name__ == "__main__":
main()