-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSortHierarchy.py
More file actions
172 lines (133 loc) · 5.7 KB
/
SortHierarchy.py
File metadata and controls
172 lines (133 loc) · 5.7 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
bl_info = {
"name": "Object Sorting in Collections",
"description": "Automatically sorts objects into collections based on various criteria.",
"author": "Joucaz",
"version": (1, 0, 0),
"blender": (3, 90, 0),
"location": "View3D > Object -> Sort Objects",
"category": "Object",
}
import bpy
# Classes et Fonctions
class OBJECT_OT_sort_objects(bpy.types.Operator):
"""Trie les objets dans des collections basées sur les critères choisis"""
bl_idname = "object.sort_objects"
bl_label = "Trier Objets"
bl_options = {'REGISTER', 'UNDO'}
sort_by: bpy.props.EnumProperty(
name="Trier par",
description="Choisissez le critère de tri",
items=[
('TYPE', "Type d'Objet", "Trier les objets par type d'objet"),
('MATERIAL', "Matériau", "Trier les objets par matériau"),
('DISTANCE', "Distance à la Caméra", "Trier les objets par distance à la caméra active"),
],
default='TYPE',
)
collection_name: bpy.props.EnumProperty(
name="Collection",
description="Choisissez une collection à trier",
items=lambda self, context: [
(" ", " ", "Sélectionnez une collection") # Option "Aucun" au début
] + [
("scene_collection", "Scene Collection", "La collection principale de la scène")
] + [
(col.name, col.name, "") for col in bpy.data.collections if col.name != bpy.context.scene.collection.name
],
)
def execute(self, context):
if self.collection_name != ' ':
if self.sort_by == 'MATERIAL':
sort_objects_by_material(self.collection_name)
elif self.sort_by == 'TYPE':
sort_objects_by_type(self.collection_name)
elif self.sort_by == 'DISTANCE':
sort_objects_by_distance(self.collection_name)
sort_collections_alphabetically()
return {'FINISHED'}
# Fonctions de tri
def sort_objects_by_material(collection_name):
if collection_name == "scene_collection":
target_collection = bpy.context.scene.objects
else:
target_collection = bpy.data.collections.get(collection_name).objects
material_collections = {}
for obj in target_collection:
if obj.type == 'MESH' and obj.active_material:
mat = obj.active_material
if mat not in material_collections:
material_collections[mat] = bpy.data.collections.new(mat.name)
bpy.context.scene.collection.children.link(material_collections[mat])
target_collection = material_collections[mat]
for col in obj.users_collection:
col.objects.unlink(obj)
target_collection.objects.link(obj)
def sort_objects_by_type(collection_name):
if collection_name == "scene_collection":
target_collection = bpy.context.scene.objects
else:
target_collection = bpy.data.collections.get(collection_name).objects
# if not target_collection:
# print(f"La collection '{collection_name}' n'existe pas.")
# return
type_collections = {}
for obj in target_collection:
obj_type = obj.type
if obj_type not in type_collections:
type_collections[obj_type] = bpy.data.collections.new(obj_type)
bpy.context.scene.collection.children.link(type_collections[obj_type])
target_collection = type_collections[obj_type]
for col in obj.users_collection:
col.objects.unlink(obj)
target_collection.objects.link(obj)
def sort_objects_by_distance(collection_name):
camera = bpy.context.scene.camera
if not camera:
print("Pas de caméra active dans la scène.")
return
distance_collections = {
"Avant-plan": bpy.data.collections.new("Avant-plan"),
"Milieu": bpy.data.collections.new("Milieu"),
"Arrière-plan": bpy.data.collections.new("Arrière-plan"),
}
for col in distance_collections.values():
bpy.context.scene.collection.children.link(col)
if collection_name == "scene_collection":
target_collection = bpy.context.scene.objects
else:
target_collection = bpy.data.collections.get(collection_name).objects
for obj in target_collection:
if obj.type == 'MESH':
distance = (camera.location - obj.location).length
if distance < 10:
collection_name = "Avant-plan"
elif distance < 30:
collection_name = "Milieu"
else:
collection_name = "Arrière-plan"
target_collection = distance_collections[collection_name]
for col in obj.users_collection:
col.objects.unlink(obj)
target_collection.objects.link(obj)
def sort_collections_alphabetically():
collections = bpy.context.scene.collection.children
sorted_collections = sorted(collections, key=lambda col: col.name)
for col in collections:
bpy.context.scene.collection.children.unlink(col)
for col in sorted_collections:
bpy.context.scene.collection.children.link(col)
# Enregistrement et Dé-enregistrement
def menu_func(self, context):
self.layout.operator(OBJECT_OT_sort_objects.bl_idname)
def register():
bpy.utils.register_class(OBJECT_OT_sort_objects)
bpy.types.VIEW3D_MT_object.append(menu_func)
def unregister():
bpy.types.VIEW3D_MT_object.remove(menu_func)
bpy.utils.unregister_class(OBJECT_OT_sort_objects)
if __name__ == "__main__":
try:
unregister()
except:
pass
register()