-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmaterial_slots_order_by_name.py
98 lines (79 loc) · 3.43 KB
/
material_slots_order_by_name.py
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
# mesh_relax.py Copyright (C) 2010, Fabian Fricke
#
# Relaxes selected vertices while retaining the shape as much as possible
#
# ***** BEGIN GPL LICENSE BLOCK *****
#
#
# 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.
#
# ***** END GPL LICENCE BLOCK *****
bl_info = {
"name": "Order Slots by Name",
"author": "Jan Kadeřábek",
"version": (1, 0),
"blender": (2, 7, 0),
"location": "Material Tab > Specials Menu (Down Arrow)",
"description": "Reorders material slots by their name",
"category": "Material",
}
import bpy
slotsNames = []
def draw(self, context):
self.layout.operator(
OrderMaterialSlots.bl_idname,
icon="SYNTAX_ON")
def updateSlotsNames():
global slotsNames
slotsNames = []
slots = bpy.context.active_object.material_slots
for slot in slots:
slotsNames.append(slot.name)
class OrderMaterialSlots(bpy.types.Operator):
"""Order Slots by Name""" # blender will use this as a tooltip for menu items and buttons.
bl_idname = "material.order_material_slots" # unique identifier for buttons and menu items to reference.
bl_label = "Order Slots by Name" # display name in the interface.
bl_options = {'REGISTER', 'UNDO'} # enable undo for the operator.
def execute(self, context):
obj = bpy.context.active_object
updateSlotsNames()
slotsNamesSorted = sorted(slotsNames)
selectedSlotName = slotsNames[obj.active_material_index]
for slotNameInSorted in slotsNamesSorted:
for slotName in slotsNames:
if(slotNameInSorted == slotName):
obj.active_material_index = slotsNames.index(slotNameInSorted)
offset = slotsNamesSorted.index(slotName) - slotsNames.index(slotName)
offsetAbs = abs(offset)
if(offset < 0):
for j in range(offsetAbs):
bpy.ops.object.material_slot_move(direction='UP')
elif(offset < 0):
for k in range(aoffsetAbs):
obpy.ops.object.material_slot_move(direction='DOWN')
updateSlotsNames()
break
obj.active_material_index = slotsNames.index(selectedSlotName)
return {"FINISHED"}
def register():
bpy.utils.register_class(OrderMaterialSlots)
bpy.types.MATERIAL_MT_specials.append(draw)
def unregister():
bpy.utils.unregister_class(OrderMaterialSlots)
bpy.types.MATERIAL_MT_specials.remove(draw)
# This allows you to run the script directly from blenders text editor
# to test the addon without having to install it.
if __name__ == "__main__":
register()