Welcome to mirror list, hosted at ThFree Co, Russian Federation.

material_remove_unassigned.py « scene « amaranth - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f19bc3d27e7ff1ac4282463b89685b3282c11607 (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
97
98
99
100
101
102
103
104
105
106
107
108
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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)