forked from bookyakuno/Blender-Scramble-Addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMATERIAL_MT_context_menu.py
238 lines (216 loc) · 7.57 KB
/
MATERIAL_MT_context_menu.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# 「プロパティ」エリア > 「マテリアル」タブ > マテリアルリスト > V ボタン
# "Propaties" Area > "Material" Tab > Material List > V Button
import bpy
################
# オペレーター #
################
class RemoveNoAssignMaterial(bpy.types.Operator):
bl_idname = "material.remove_no_assign_material"
bl_label = "Remove Unassigned Material"
bl_description = "For each selected objects, remove all materials that has not assigned to any faces of the objects"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = context.active_object
if obj:
if (len(obj.material_slots) <= 0):
return False
return True
def execute(self, context):
preActiveObj = context.active_object
for obj in context.selected_objects:
if (obj.type == "MESH"):
bpy.context.view_layer.objects.active = obj
preActiveMaterial = obj.active_material
slots = []
for slot in obj.material_slots:
slots.append((slot.name, 0))
me = obj.data
for face in me.polygons:
slots[face.material_index] = (slots[face.material_index][0], slots[face.material_index][1] + 1)
for name, count in slots:
if (name != "" and count == 0):
i = 0
for slot in obj.material_slots:
if (slot.name == name):
break
i += 1
obj.active_material_index = i
bpy.ops.object.material_slot_remove()
bpy.context.view_layer.objects.active = preActiveObj
return {'FINISHED'}
class RemoveAllMaterialSlot(bpy.types.Operator):
bl_idname = "material.remove_all_material_slot"
bl_label = "Remove All Material Slots"
bl_description = "Remove all material slots of the active object"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = context.active_object
if obj:
if (len(obj.material_slots) <= 0):
return False
return True
def execute(self, context):
activeObj = context.active_object
if 0 < len(activeObj.material_slots):
while True:
if (0 < len(activeObj.material_slots)):
bpy.ops.object.material_slot_remove()
else:
break
#for material in bpy.data.materials:
# if not material.users:
# bpy.data.materials.remove(material)
return {'FINISHED'}
class RemoveEmptyMaterialSlot(bpy.types.Operator):
bl_idname = "material.remove_empty_material_slot"
bl_label = "Remove Empty Material Slots"
bl_description = "Remove all material slots that any material has not been assigned to"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = context.active_object
if obj:
for slot in obj.material_slots:
if (not slot.material):
return True
return False
def execute(self, context):
activeObj = context.active_object
if (activeObj.type == "MESH"):
slots = activeObj.material_slots[:]
slots.reverse()
i = 0
for slot in slots:
active_material_index = i
if (not slot.material):
bpy.ops.object.material_slot_remove()
i += 1
return {'FINISHED'}
class SetTransparentBackSide(bpy.types.Operator):
bl_idname = "material.set_transparent_back_side"
bl_label = "Hide Back Side of Faces"
bl_description = "Eevee: Enable 'backface culling' / Cycles: Add shader nodes to make the back side of mesh transparent"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
act = bpy.context.view_layer.objects.active
if act:
mat = act.active_material
if (not mat):
return False
if (mat.node_tree):
if (len(mat.node_tree.nodes) >= 2):
return True
if (not mat.use_nodes):
return True
return False
def execute(self, context):
mat = context.material
if context.scene.render.engine == "BLENDER_EEVEE":
mat.use_backface_culling = True
elif context.scene.render.engine == "CYCLES":
mat.use_nodes = True
#if (mat.node_tree):
# for node in mat.node_tree.nodes:
# if (node):
# mat.node_tree.nodes.remove(node)
node_out = mat.node_tree.nodes['Material Output']
node_mat = node_out.inputs[0].links[0].from_node
node_trn = mat.node_tree.nodes.new('ShaderNodeBsdfTransparent')
node_geo = mat.node_tree.nodes.new('ShaderNodeNewGeometry')
node_mix = mat.node_tree.nodes.new('ShaderNodeMixShader')
node_mat.location = [node_mat.location[0]-300, node_mat.location[1]]
node_out.location = [node_out.location[0], node_out.location[1]]
node_geo.location = [node_geo.location[0], node_geo.location[1]+600]
node_trn.location = [node_trn.location[0]+100, node_trn.location[1]]
node_mix.location = [node_mix.location[0]+50, node_mix.location[1]+300]
mat.node_tree.links.new(node_mat.outputs[0], node_mix.inputs[1])
mat.node_tree.links.new(node_trn.outputs[0], node_mix.inputs[2])
mat.node_tree.links.new(node_geo.outputs[6], node_mix.inputs[0])
mat.node_tree.links.new(node_mix.outputs[0], node_out.inputs[0])
return {'FINISHED'}
class MoveMaterialSlotTop(bpy.types.Operator):
bl_idname = "material.move_material_slot_top"
bl_label = "Move to Top"
bl_description = "Move active material slot to the top of list"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = context.active_object
if (not obj):
return False
if (len(obj.material_slots) <= 2):
return False
if (obj.active_material_index <= 0):
return False
return True
def execute(self, context):
activeObj = context.active_object
for i in range(activeObj.active_material_index):
bpy.ops.object.material_slot_move(direction='UP')
return {'FINISHED'}
class MoveMaterialSlotBottom(bpy.types.Operator):
bl_idname = "material.move_material_slot_bottom"
bl_label = "Move to Bottom"
bl_description = "Move active material slot to the bottom of list"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = context.active_object
if (not obj):
return False
if (len(obj.material_slots) <= 2):
return False
if (len(obj.material_slots)-1 <= obj.active_material_index):
return False
return True
def execute(self, context):
activeObj = context.active_object
lastSlotIndex = len(activeObj.material_slots) - 1
for i in range(lastSlotIndex - activeObj.active_material_index):
bpy.ops.object.material_slot_move(direction='DOWN')
return {'FINISHED'}
################
# クラスの登録 #
################
classes = [
RemoveNoAssignMaterial,
RemoveAllMaterialSlot,
RemoveEmptyMaterialSlot,
SetTransparentBackSide,
MoveMaterialSlotTop,
MoveMaterialSlotBottom
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
################
# メニュー追加 #
################
# メニューのオン/オフの判定
def IsMenuEnable(self_id):
for id in bpy.context.preferences.addons[__name__.partition('.')[0]].preferences.disabled_menu.split(','):
if (id == self_id):
return False
else:
return True
# メニューを登録する関数
def menu(self, context):
if (IsMenuEnable(__name__.split('.')[-1])):
self.layout.separator()
self.layout.operator(MoveMaterialSlotTop.bl_idname, icon='TRIA_UP_BAR')
self.layout.operator(MoveMaterialSlotBottom.bl_idname, icon='TRIA_DOWN_BAR')
self.layout.separator()
self.layout.operator(RemoveAllMaterialSlot.bl_idname, icon='CANCEL')
self.layout.operator(RemoveEmptyMaterialSlot.bl_idname, icon='PLUGIN')
self.layout.operator(RemoveNoAssignMaterial.bl_idname, icon='PLUGIN')
self.layout.separator()
self.layout.operator(SetTransparentBackSide.bl_idname, icon='PLUGIN')
if (bpy.context.preferences.addons[__name__.partition('.')[0]].preferences.use_disabled_menu):
self.layout.separator()
self.layout.operator('wm.toggle_menu_enable', icon='CANCEL').id = __name__.split('.')[-1]