forked from bookyakuno/Blender-Scramble-Addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVIEW3D_MT_edit_mesh_context_menu.py
266 lines (239 loc) · 9.6 KB
/
VIEW3D_MT_edit_mesh_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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# 「3Dビュー」エリア > メッシュの「編集」モード > 「コンテクストメニュー」 (Wキー)
# "3D View" Area > "Edit" Mode with Mesh > "Context Menu" (W Key)
import bpy
from bpy.props import *
################
# オペレーター #
################
class PaintSelectedVertexColor(bpy.types.Operator):
bl_idname = "mesh.paint_selected_vertex_color"
bl_label = "Fill Selected Vertices' Vertex Color"
bl_description = "Fill selected vertices with designated color at active vertex color"
bl_options = {'REGISTER', 'UNDO'}
color : FloatVectorProperty(name="Color", default=(1, 1, 1), step=1, precision=3, subtype='COLOR_GAMMA', min=0, max=1, soft_min=0, soft_max=1)
def execute(self, context):
activeObj = context.active_object
me = activeObj.data
if not me.vertex_colors.active:
me.vertex_colors.active = me.vertex_colors.new()
bpy.ops.object.mode_set(mode='OBJECT')
i = 0
for poly in me.polygons:
for vert in poly.vertices:
if (me.vertices[vert].select):
me.vertex_colors.active.data[i].color = (self.color[0], self.color[1], self.color[2], 1.0)
i += 1
bpy.ops.object.mode_set(mode='EDIT')
return {'FINISHED'}
class SelectTopShape(bpy.types.Operator):
bl_idname = "mesh.select_top_shape"
bl_label = "Show Base Shape"
bl_description = "Show base shape of active object's shape keys in viewport"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
context.active_object.active_shape_key_index = 0
return {'FINISHED'}
class ToggleShowCage(bpy.types.Operator):
bl_idname = "mesh.toggle_show_cage"
bl_label = "Switch Display Method of Modifiers' Results"
bl_description = "Switch display method of meshes or their vertices which are created by modifiers"
bl_options = {'REGISTER', 'UNDO'}
mode_item = [
("NON","Vertices: Hide, Meshes: Hide","",1),("EDIT","Vertices: Hide, Meshes: Show","",2),("BOTH","Vertices: Show, Meshes: Show","",3)
]
mode : EnumProperty(name="Target", items=mode_item)
def __init__(self):
mods = bpy.context.active_object.modifiers
edit_bools = [mod.show_in_editmode for mod in mods]
cage_bools = [mod.show_on_cage for mod in mods]
if sum(edit_bools)==0 and sum(cage_bools)==0:
self.mode = self.mode_display = 'EDIT'
elif sum(edit_bools)>=1 and sum(cage_bools)>=1:
self.mode = 'NON'
else:
self.mode = 'BOTH'
def draw(self, layout):
self.layout.prop(self, 'mode', expand=True)
def execute(self, context):
dic = {
'NON':[False,False,"Adjusting edit cage: Disabled, Display in edit mode: Disabled"],
'EDIT':[False,True,"Adjusting edit cage: Disabled, Display in edit mode: Enabled"],
'BOTH':[True,True,"Adjusting edit cage: Enabled, Display in edit mode: Enabled"]
}
activeObj = context.active_object
items = dic[self.mode]
for modi in activeObj.modifiers:
modi.show_on_cage = items[0]
modi.show_in_editmode = items[1]
self.report(type={'INFO'}, message=items[2])
return {'FINISHED'}
class ToggleMirrorModifier(bpy.types.Operator):
bl_idname = "mesh.toggle_mirror_modifier"
bl_label = "Switch Display of Mirror Modifiers"
bl_description = "Switch display state of mirror modifiers (and add it if not exist)"
bl_options = {'REGISTER', 'UNDO'}
use_x : BoolProperty(name="X Axis", default=True)
use_y : BoolProperty(name="Y Axis", default=False)
use_z : BoolProperty(name="Z Axis", default=False)
use_mirror_merge : BoolProperty(name="Merge", default=True)
merge_threshold : FloatProperty(name="Merge Distance", default=0.001, min=0, max=1, soft_min=0, soft_max=1, step=0.01, precision=6)
use_clip : BoolProperty(name="Clipping", default=False)
use_mirror_u : BoolProperty(name="Mirror U", default=False)
use_mirror_v : BoolProperty(name="Mirror V", default=False)
use_mirror_vertex_groups : BoolProperty(name="Vertex Groups", default=True)
is_top : BoolProperty(name="Add at Top", default=True)
toggle : BoolProperty(name="Toggle", default=False)
all_show : BoolProperty(name="Show All", default=False)
all_hide : BoolProperty(name="Hide All", default=False)
is_add : BoolProperty(name="Add or not", default=False, options={'HIDDEN'})
def draw(self, context):
row = self.layout.row()
row.prop(self, 'all_show', icon='HIDE_OFF', toggle=1)
row.prop(self, 'all_hide', icon='HIDE_ON', toggle=1)
row.prop(self, 'toggle', toggle=1)
if self.is_add:
self.layout.separator()
row = self.layout.row()
row.use_property_split = True
row.prop(self, 'is_top')
box = self.layout.box()
row = box.row()
for p in ['use_x','use_y','use_z',]:
row.prop(self, p)
box.prop(self, 'use_clip')
row = box.row()
row.prop(self, 'use_mirror_merge')
row.prop(self, 'merge_threshold')
row = box.row()
row.prop(self, 'use_mirror_u')
row.prop(self, 'use_mirror_v')
box.prop(self, 'use_mirror_vertex_groups')
def execute(self, context):
self.toggle = True
modis = context.active_object.modifiers
mir_mods = [mod.name for mod in modis if mod.type=='MIRROR']
if mir_mods:
for nam in mir_mods:
if modis[nam].show_in_editmode:
modis[nam].show_in_editmode = True
if self.all_hide:
modis[nam].show_viewport = False
elif self.all_show or not modis[nam].show_in_editmode:
modis[nam].show_viewport = True
elif self.toggle:
modis[nam].show_viewport = not modis[nam].show_viewport
self.is_add = False
else:
new_mod = modis.new("Mirror", 'MIRROR')
new_mod.use_axis = [self.use_x, self.use_y, self.use_z]
new_mod.use_mirror_merge = self.use_mirror_merge
new_mod.use_clip = self.use_clip
new_mod.use_mirror_vertex_groups = self.use_mirror_vertex_groups
new_mod.use_mirror_u = self.use_mirror_u
new_mod.use_mirror_v = self.use_mirror_v
new_mod.merge_threshold = self.merge_threshold
if (self.is_top):
for i in range(len(modis)):
bpy.ops.object.modifier_move_up(modifier=new_mod.name)
self.is_add = True
self.toggle = self.all_show = self.all_hide = False
return {'FINISHED'}
class SelectedVertexGroupAverage(bpy.types.Operator):
bl_idname = "mesh.selected_vertex_group_average"
bl_label = "Average Selected Vertices' Weight"
bl_description = "Change selected vertices' weight to their average value"
bl_options = {'REGISTER', 'UNDO'}
all_group : BoolProperty(name="Apply to All Groups", default=True)
target : StringProperty(name="Target", default="")
strength : FloatProperty(name="Original Values' Effect", default=0, min=0, max=1, soft_min=0, soft_max=1, step=10, precision=3)
@classmethod
def poll(cls, context):
obj = context.active_object
if obj:
if len(obj.vertex_groups) == 0:
return False
if not obj.type == "MESH":
return False
return True
def __init__(self):
idx = bpy.context.active_object.vertex_groups.active_index
self.target = bpy.context.active_object.vertex_groups[idx].name
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
self.layout.prop(self, 'all_group')
row = self.layout.row()
row.enabled = not self.all_group
row.prop_search(self, 'target', context.active_object, "vertex_groups", text="Target", translate=True, icon='GROUP_VERTEX')
row = self.layout.split(factor=0.55)
row.label(text="Original Values' Effect")
row.prop(self, 'strength', text="")
def execute(self, context):
obj = context.active_object
pre_mode = obj.mode
bpy.ops.object.mode_set(mode='OBJECT')
vg_dic = {idx: dict() for idx, vg in enumerate(obj.vertex_groups)}
selected_verts = [v for v in obj.data.vertices if v.select]
if (len(selected_verts) <= 0):
bpy.ops.object.mode_set(mode=pre_mode)
self.report(type={'ERROR'}, message="Need to select at least one vertex")
return {'CANCELLED'}
for v in selected_verts:
for vge in v.groups:
belonged_vg_dic = vg_dic[vge.group]
belonged_vg_dic[v.index] = vge.weight
if self.all_group:
keys = list(vg_dic.keys())
else:
keys = [obj.vertex_groups.find(self.target)]
for key in keys:
v_group = obj.vertex_groups[key]
counts = len(list(vg_dic[key].keys()))
weight = sum(list(vg_dic[key].values()))
average = weight/counts
for vert_idx in vg_dic[key].keys():
pre_weight = vg_dic[key][vert_idx]
new_weight = pre_weight*self.strength + average*(1-self.strength)
v_group.add([vert_idx], new_weight, 'REPLACE')
bpy.ops.object.mode_set(mode=pre_mode)
return {'FINISHED'}
################
# クラスの登録 #
################
classes = [
PaintSelectedVertexColor,
SelectTopShape,
ToggleShowCage,
ToggleMirrorModifier,
SelectedVertexGroupAverage
]
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.operator(SelectTopShape.bl_idname, icon="PLUGIN")
self.layout.separator()
self.layout.prop(context.object.data, "use_mirror_x", icon="PLUGIN", text="X-Axis Mirror")
self.layout.operator(ToggleMirrorModifier.bl_idname, icon="PLUGIN")
self.layout.operator(ToggleShowCage.bl_idname, icon="PLUGIN")
self.layout.separator()
self.layout.operator(SelectedVertexGroupAverage.bl_idname, icon="PLUGIN")
self.layout.operator(PaintSelectedVertexColor.bl_idname, icon="PLUGIN")
if (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]