1 # SPDX-FileCopyrightText: 2019-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
8 # FEATURE: Delete Materials not assigned to any verts
9 class AMTH_OBJECT_OT_material_remove_unassigned(bpy
.types
.Operator
):
11 """Remove materials not assigned to any vertex"""
12 bl_idname
= "object.amaranth_object_material_remove_unassigned"
13 bl_label
= "Remove Unassigned Materials"
16 def poll(cls
, context
):
17 return context
.active_object
.material_slots
19 def execute(self
, context
):
22 act_ob
= context
.active_object
23 count
= len(act_ob
.material_slots
)
24 materials_removed
= []
25 act_ob
.active_material_index
= 0
28 if act_ob
not in context
.visible_objects
:
31 for lay
in act_ob
.layers
:
36 scene
.layers
[n
] = True
38 for slot
in act_ob
.material_slots
:
41 bpy
.ops
.object.mode_set(mode
="EDIT")
42 bpy
.ops
.mesh
.select_all(action
="DESELECT")
43 act_ob
.active_material_index
= count
44 bpy
.ops
.object.material_slot_select()
46 if act_ob
.data
.total_vert_sel
== 0 or \
47 (len(act_ob
.material_slots
) == 1 and not
48 act_ob
.material_slots
[0].material
):
49 materials_removed
.append(
51 act_ob
.active_material
.name
if act_ob
.active_material
else "Empty")
52 bpy
.ops
.object.mode_set(mode
="OBJECT")
53 bpy
.ops
.object.material_slot_remove()
57 bpy
.ops
.object.mode_set(mode
="EDIT")
58 bpy
.ops
.mesh
.select_all(action
="DESELECT")
59 bpy
.ops
.object.mode_set(mode
="OBJECT")
63 "\n* Removed %s Unassigned Materials \n" %
64 len(materials_removed
))
68 for mr
in materials_removed
:
72 (count_mr
, materials_removed
[count_mr
- 1]))
75 self
.report({"INFO"}, "Removed %s Unassigned Materials" %
76 len(materials_removed
))
79 scene
.layers
[n
] = False
84 def ui_material_remove_unassigned(self
, context
):
86 AMTH_OBJECT_OT_material_remove_unassigned
.bl_idname
,
89 # // FEATURE: Delete Materials not assigned to any verts
93 bpy
.utils
.register_class(AMTH_OBJECT_OT_material_remove_unassigned
)
94 bpy
.types
.MATERIAL_MT_context_menu
.append(ui_material_remove_unassigned
)
98 bpy
.utils
.unregister_class(AMTH_OBJECT_OT_material_remove_unassigned
)
99 bpy
.types
.MATERIAL_MT_context_menu
.remove(ui_material_remove_unassigned
)