Refactor: Node Wrangler: PreviewNode operator
[blender-addons.git] / amaranth / scene / material_remove_unassigned.py
blob4ddc6110b9ef254a9922f628b6aaa251fc86e16f
1 # SPDX-FileCopyrightText: 2019-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import bpy
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"
15 @classmethod
16 def poll(cls, context):
17 return context.active_object.material_slots
19 def execute(self, context):
21 scene = context.scene
22 act_ob = context.active_object
23 count = len(act_ob.material_slots)
24 materials_removed = []
25 act_ob.active_material_index = 0
26 is_visible = True
28 if act_ob not in context.visible_objects:
29 is_visible = False
30 n = -1
31 for lay in act_ob.layers:
32 n += 1
33 if lay:
34 break
36 scene.layers[n] = True
38 for slot in act_ob.material_slots:
39 count -= 1
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(
50 "%s" %
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()
54 else:
55 pass
57 bpy.ops.object.mode_set(mode="EDIT")
58 bpy.ops.mesh.select_all(action="DESELECT")
59 bpy.ops.object.mode_set(mode="OBJECT")
61 if materials_removed:
62 print(
63 "\n* Removed %s Unassigned Materials \n" %
64 len(materials_removed))
66 count_mr = 0
68 for mr in materials_removed:
69 count_mr += 1
70 print(
71 "%0.2d. %s" %
72 (count_mr, materials_removed[count_mr - 1]))
74 print("\n")
75 self.report({"INFO"}, "Removed %s Unassigned Materials" %
76 len(materials_removed))
78 if not is_visible:
79 scene.layers[n] = False
81 return {"FINISHED"}
84 def ui_material_remove_unassigned(self, context):
85 self.layout.operator(
86 AMTH_OBJECT_OT_material_remove_unassigned.bl_idname,
87 icon="X")
89 # // FEATURE: Delete Materials not assigned to any verts
92 def register():
93 bpy.utils.register_class(AMTH_OBJECT_OT_material_remove_unassigned)
94 bpy.types.MATERIAL_MT_context_menu.append(ui_material_remove_unassigned)
97 def unregister():
98 bpy.utils.unregister_class(AMTH_OBJECT_OT_material_remove_unassigned)
99 bpy.types.MATERIAL_MT_context_menu.remove(ui_material_remove_unassigned)