-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasy_Purge.py
92 lines (79 loc) · 3.12 KB
/
Easy_Purge.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
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
bl_info = {
"name": "Purge Unused Data",
"author": "Mightyiest",
"version": (1, 3),
"blender": (3, 6, 0),
"location": "View3D > UI > Purge Unused Data",
"description": "Remove all unused data-blocks from the project and display purge statistics in the Info Area",
"category": "Scene",
}
import bpy
def count_data_blocks():
"""Count all data-blocks in the current file."""
data_blocks = {
'meshes': len(bpy.data.meshes),
'materials': len(bpy.data.materials),
'textures': len(bpy.data.textures),
'images': len(bpy.data.images),
'armatures': len(bpy.data.armatures),
'actions': len(bpy.data.actions),
'cameras': len(bpy.data.cameras),
'lights': len(bpy.data.lights),
'node_groups': len(bpy.data.node_groups),
'collections': len(bpy.data.collections),
}
return data_blocks
class PURGE_OT_unused_data(bpy.types.Operator):
"""Tooltip"""
bl_idname = "scene.purge_unused_data"
bl_label = "Purge Unused Data"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
# Count data-blocks before purging
before = count_data_blocks()
# Detect Blender version and handle accordingly
if bpy.app.version >= (4, 3, 0):
# Blender 4.3+ uses a simplified version of the operator
bpy.ops.outliner.orphans_purge()
elif bpy.app.version >= (4, 0, 0):
# Blender 4.0 to 4.2 uses do_local_ids and do_library_ids
purge_params = {
'do_local_ids': True,
'do_library_ids': True
}
bpy.ops.outliner.orphans_purge(**purge_params)
else:
# Blender 3.6 does not use parameters
bpy.ops.outliner.orphans_purge()
# Count data-blocks after purging
after = count_data_blocks()
# Calculate the difference
purged = {key: before[key] - after[key] for key in before}
# Display the results in the Info Area
self.report({'INFO'},
f"Purged: "
f"Meshes: {purged['meshes']}, "
f"Materials: {purged['materials']}, "
f"Textures: {purged['textures']}, "
f"Images: {purged['images']}, "
f"Armatures: {purged['armatures']}, "
f"Actions: {purged['actions']}, "
f"Cameras: {purged['cameras']}, "
f"Lights: {purged['lights']}, "
f"Node Groups: {purged['node_groups']}, "
f"Collections: {purged['collections']}"
)
return {'FINISHED'}
# Add a menu entry to access the operator
def menu_func(self, context):
self.layout.operator("scene.purge_unused_data", text="Purge Unused Data")
def register():
bpy.utils.register_class(PURGE_OT_unused_data)
# Add the menu entry
bpy.types.VIEW3D_MT_editor_menus.append(menu_func)
def unregister():
bpy.utils.unregister_class(PURGE_OT_unused_data)
# Remove the menu entry
bpy.types.VIEW3D_MT_editor_menus.remove(menu_func)
if __name__ == "__main__":
register()