Amaranth Addon
[blender-addons-contrib.git] / amaranth / scene / material_remove_unassigned.py
1 #  This program is free software; you can redistribute it and/or
2 #  modify it under the terms of the GNU General Public License
3 #  as published by the Free Software Foundation; either version 2
4 #  of the License, or (at your option) any later version.
5 #
6 #  This program is distributed in the hope that it will be useful,
7 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
8 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9 #  GNU General Public License for more details.
10 #
11 #  You should have received a copy of the GNU General Public License
12 #  along with this program; if not, write to the Free Software Foundation,
13 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 import bpy
15
16
17 # FEATURE: Delete Materials not assigned to any verts
18 class AMTH_OBJECT_OT_material_remove_unassigned(bpy.types.Operator):
19
20     """Remove materials not assigned to any vertex"""
21     bl_idname = "object.amaranth_object_material_remove_unassigned"
22     bl_label = "Remove Unassigned Materials"
23
24     @classmethod
25     def poll(cls, context):
26         return context.active_object.material_slots
27
28     def execute(self, context):
29
30         scene = context.scene
31         act_ob = context.active_object
32         count = len(act_ob.material_slots)
33         materials_removed = []
34         act_ob.active_material_index = 0
35         is_visible = True
36
37         if act_ob not in context.visible_objects:
38             is_visible = False
39             n = -1
40             for lay in act_ob.layers:
41                 n += 1
42                 if lay:
43                     break
44
45             scene.layers[n] = True
46
47         for slot in act_ob.material_slots:
48             count -= 1
49
50             bpy.ops.object.mode_set(mode="EDIT")
51             bpy.ops.mesh.select_all(action="DESELECT")
52             act_ob.active_material_index = count
53             bpy.ops.object.material_slot_select()
54
55             if act_ob.data.total_vert_sel == 0 or \
56                 (len(act_ob.material_slots) == 1 and not
57                     act_ob.material_slots[0].material):
58                 materials_removed.append(
59                     "%s" %
60                     act_ob.active_material.name if act_ob.active_material else "Empty")
61                 bpy.ops.object.mode_set(mode="OBJECT")
62                 bpy.ops.object.material_slot_remove()
63             else:
64                 pass
65
66         bpy.ops.object.mode_set(mode="EDIT")
67         bpy.ops.mesh.select_all(action="DESELECT")
68         bpy.ops.object.mode_set(mode="OBJECT")
69
70         if materials_removed:
71             print(
72                 "\n* Removed %s Unassigned Materials \n" %
73                 len(materials_removed))
74
75             count_mr = 0
76
77             for mr in materials_removed:
78                 count_mr += 1
79                 print(
80                     "%0.2d. %s" %
81                     (count_mr, materials_removed[count_mr - 1]))
82
83             print("\n")
84             self.report({"INFO"}, "Removed %s Unassigned Materials" %
85                         len(materials_removed))
86
87         if not is_visible:
88             scene.layers[n] = False
89
90         return {"FINISHED"}
91
92
93 def ui_material_remove_unassigned(self, context):
94     self.layout.operator(
95         AMTH_OBJECT_OT_material_remove_unassigned.bl_idname,
96         icon="X")
97
98 # // FEATURE: Delete Materials not assigned to any verts
99
100
101 def register():
102     bpy.utils.register_class(AMTH_OBJECT_OT_material_remove_unassigned)
103     bpy.types.MATERIAL_MT_specials.append(ui_material_remove_unassigned)
104
105
106 def unregister():
107     bpy.utils.unregister_class(AMTH_OBJECT_OT_material_remove_unassigned)
108     bpy.types.MATERIAL_MT_specials.remove(ui_material_remove_unassigned)