blob: af2154d1478ed8b259e6b9403f13db7a5a028465 (
plain)
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
|
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy
# FEATURE: Delete Materials not assigned to any verts
class AMTH_OBJECT_OT_material_remove_unassigned(bpy.types.Operator):
"""Remove materials not assigned to any vertex"""
bl_idname = "object.amaranth_object_material_remove_unassigned"
bl_label = "Remove Unassigned Materials"
@classmethod
def poll(cls, context):
return context.active_object.material_slots
def execute(self, context):
scene = context.scene
act_ob = context.active_object
count = len(act_ob.material_slots)
materials_removed = []
act_ob.active_material_index = 0
is_visible = True
if act_ob not in context.visible_objects:
is_visible = False
n = -1
for lay in act_ob.layers:
n += 1
if lay:
break
scene.layers[n] = True
for slot in act_ob.material_slots:
count -= 1
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="DESELECT")
act_ob.active_material_index = count
bpy.ops.object.material_slot_select()
if act_ob.data.total_vert_sel == 0 or \
(len(act_ob.material_slots) == 1 and not
act_ob.material_slots[0].material):
materials_removed.append(
"%s" %
act_ob.active_material.name if act_ob.active_material else "Empty")
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.material_slot_remove()
else:
pass
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="DESELECT")
bpy.ops.object.mode_set(mode="OBJECT")
if materials_removed:
print(
"\n* Removed %s Unassigned Materials \n" %
len(materials_removed))
count_mr = 0
for mr in materials_removed:
count_mr += 1
print(
"%0.2d. %s" %
(count_mr, materials_removed[count_mr - 1]))
print("\n")
self.report({"INFO"}, "Removed %s Unassigned Materials" %
len(materials_removed))
if not is_visible:
scene.layers[n] = False
return {"FINISHED"}
def ui_material_remove_unassigned(self, context):
self.layout.operator(
AMTH_OBJECT_OT_material_remove_unassigned.bl_idname,
icon="X")
# // FEATURE: Delete Materials not assigned to any verts
def register():
bpy.utils.register_class(AMTH_OBJECT_OT_material_remove_unassigned)
bpy.types.MATERIAL_MT_context_menu.append(ui_material_remove_unassigned)
def unregister():
bpy.utils.unregister_class(AMTH_OBJECT_OT_material_remove_unassigned)
bpy.types.MATERIAL_MT_context_menu.remove(ui_material_remove_unassigned)
|