forked from sambler/myblendercontrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh_show_vgroup_weights.py
523 lines (434 loc) · 19.3 KB
/
mesh_show_vgroup_weights.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# ***** 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 *****
# <pep8 compliant> (Thanks to CodemanX on IRC)
bl_info = {
"name": "Show Vertex Groups/Weights",
"author": "Jason van Gumster (Fweeb), Bartius Crouch, CoDEmanX",
"version": (0, 7, 2),
"blender": (2, 80, 4),
"location": "3D View > Properties Region > Show Weights",
"description": "Finds the vertex groups of a selected vertex "
"and displays the corresponding weights",
"warning": "",
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
"Scripts/Modeling/Show_Vertex_Group_Weights",
"tracker_url": "https://developer.blender.org/maniphest/task/edit/form/2/",
"category": "Mesh"}
#TODO - Add button for selecting vertices with no groups
import bpy, bmesh, bgl, blf, mathutils
# Borrowed/Modified from Bart Crouch's old Index Visualizer add-on
def calc_callback(self, context):
#polling
if context.mode != "EDIT_MESH" or len(context.active_object.vertex_groups) == 0:
return
# get color info from theme
acol = context.preferences.themes[0].view_3d.editmesh_active
tcol = (acol[0] * 0.85, acol[1] * 0.85, acol[2] * 0.85)
# get screen information
mid_x = context.region.width / 2.0
mid_y = context.region.height / 2.0
width = context.region.width
height = context.region.height
# get matrices
view_mat = context.space_data.region_3d.perspective_matrix
ob_mat = context.active_object.matrix_world
total_mat = view_mat @ ob_mat
# calculate location info
texts = []
locs = []
weights = []
me = context.active_object.data
bm = bmesh.from_edit_mesh(me)
dvert_lay = bm.verts.layers.deform.active
verts_max = context.scene.show_vgroups_weights_limit
verts_used = []
for elem in reversed(bm.select_history):
if not isinstance(elem, bmesh.types.BMVert): #or elem.hide:
continue
if bm.select_history.active == elem:
locs.append([acol[0], acol[1], acol[2], elem.index, elem.co.to_4d()])
else:
locs.append([tcol[0], tcol[1], tcol[2], elem.index, elem.co.to_4d()])
dvert = elem[dvert_lay]
for vgroup in context.active_object.vertex_groups:
if vgroup.index in dvert.keys():
weights += [elem.index, vgroup.index, dvert[vgroup.index]]
verts_used.append(elem)
verts_max -= 1
if verts_max <= 0:
break
for v in bm.verts:
if v.select and v not in verts_used: #XXX Should check v.hide here, but it doesn't work
if isinstance(bm.select_history.active, bmesh.types.BMVert) and bm.select_history.active.index == v.index:
locs.append([acol[0], acol[1], acol[2], v.index, v.co.to_4d()])
else:
locs.append([tcol[0], tcol[1], tcol[2], v.index, v.co.to_4d()])
dvert = v[dvert_lay]
for vgroup in context.active_object.vertex_groups:
if vgroup.index in dvert.keys():
weights += [v.index, vgroup.index, dvert[vgroup.index]]
verts_max -= 1
if verts_max <= 0:
break
for loc in locs:
vec = total_mat @ loc[4] # order is important
# dehomogenise
vec = mathutils.Vector((vec[0] / vec[3], vec[1] / vec[3], vec[2] / vec[3]))
x = int(mid_x + vec[0] * width / 2.0)
y = int(mid_y + vec[1] * height / 2.0)
texts += [loc[0], loc[1], loc[2], loc[3], x, y, 0]
# store as ID property in mesh
context.active_object.data["show_vgroup_verts"] = texts
context.active_object.data["show_vgroup_weights"] = weights
# draw in 3d-view
def draw_callback(self, context):
# polling
if context.mode != "EDIT_MESH" or len(context.active_object.vertex_groups) == 0:
return
# retrieving ID property data
try:
texts = context.active_object.data["show_vgroup_verts"]
weights = context.active_object.data["show_vgroup_weights"]
except:
return
if not texts:
return
bm = bmesh.from_edit_mesh(context.active_object.data)
if bm.select_mode == {'VERT'} and bm.select_history.active is not None:
active_vert = bm.select_history.active
else:
active_vert = None
# draw
blf.size(0, 13, 72)
blf.enable(0, blf.SHADOW)
blf.shadow(0, 3, 0.0, 0.0, 0.0, 1.0)
blf.shadow_offset(0, 2, -2)
for i in range(0, len(texts), 7):
bgl.glColor3f(texts[i], texts[i+1], texts[i+2])
blf.position(0, texts[i+4], texts[i+5], texts[i+6])
blf.draw(0, "Vertex " + str(int(texts[i+3])) + ":")
font_y = texts[i+5]
group_name = ""
for j in range(0, len(weights), 3):
if int(weights[j]) == int(texts[i+3]):
font_y -= 13
blf.position(0, texts[i+4] + 10, font_y, texts[i+6])
for group in context.active_object.vertex_groups:
if group.index == int(weights[j+1]):
group_name = group.name
break
blf.draw(0, group_name + ": %.3f" % weights[j+2])
if group_name == "":
font_y -= 13
blf.position(0, texts[i+4] + 10, font_y, texts[i+6])
blf.draw(0, "No Groups")
# restore defaults
blf.disable(0, blf.SHADOW)
# operator
class ShowVGroupWeights(bpy.types.Operator):
bl_idname = "view3d.show_vgroup_weights"
bl_label = "Show Vertex Group Weights"
bl_description = "Toggle the display of the vertex groups and weights for selected vertices"
_handle_calc = None
_handle_draw = None
@staticmethod
def handle_add(self, context):
ShowVGroupWeights._handle_calc = bpy.types.SpaceView3D.draw_handler_add(
calc_callback, (self, context), 'WINDOW', 'POST_VIEW')
ShowVGroupWeights._handle_draw = bpy.types.SpaceView3D.draw_handler_add(
draw_callback, (self, context), 'WINDOW', 'POST_PIXEL')
@staticmethod
def handle_remove():
if ShowVGroupWeights._handle_calc is not None:
bpy.types.SpaceView3D.draw_handler_remove(ShowVGroupWeights._handle_calc, 'WINDOW')
if ShowVGroupWeights._handle_draw is not None:
bpy.types.SpaceView3D.draw_handler_remove(ShowVGroupWeights._handle_draw, 'WINDOW')
ShowVGroupWeights._handle_calc = None
ShowVGroupWeights._handle_draw = None
@classmethod
def poll(cls, context):
return context.mode == 'EDIT_MESH'
def execute(self, context):
if context.area.type == 'VIEW_3D':
if not context.scene.show_vgroups_weights:
# operator is called and not running, start everything
ShowVGroupWeights.handle_add(self, context)
context.scene.show_vgroups_weights = True
else:
# operator is called again, stop displaying
ShowVGroupWeights.handle_remove()
context.scene.show_vgroups_weights = False
clear_properties(full=False)
context.area.tag_redraw()
return {'FINISHED'}
else:
self.report({'WARNING'}, "View3D not found, can't run operator")
return {'CANCELLED'}
class VGroupsWeights(bpy.types.PropertyGroup):
vgroup: bpy.props.IntProperty()
weight: bpy.props.FloatProperty(min=0.0, max=1.0)
class AssignVertexWeight(bpy.types.Operator):
bl_idname = "mesh.vertex_group_assign"
bl_label = "Assign Weights"
bl_description = "Assign weights for all of the groups on a specific vertex"
index: bpy.props.IntProperty()
vgroup_weights: bpy.props.CollectionProperty(
description="Vertex Group Weights",
type=VGroupsWeights)
@classmethod
def poll(cls, context):
return context.mode == 'EDIT_MESH'
def execute(self, context):
me = context.active_object.data
bm = bmesh.from_edit_mesh(me)
dvert_lay = bm.verts.layers.deform.active
weights = {}
for item in self.vgroup_weights:
weights[item.vgroup] = item.weight
for v in bm.verts:
if v.index == self.index:
dvert = v[dvert_lay]
for vgroup in dvert.keys():
dvert[vgroup] = weights[vgroup]
break
context.area.tag_redraw()
return {'FINISHED'}
class RemoveFromVertexGroup(bpy.types.Operator):
bl_idname = "mesh.vertex_group_remove"
bl_label = "Remove Vertex from Group"
bl_description = "Remove a specific vertex from a specific vertex group"
#XXX abusing vector props here a bit; the first element is the vert index and the second is the group index
vert_and_group: bpy.props.IntVectorProperty(name = "Vertex and Group to remove", size = 2)
@classmethod
def poll(cls, context):
return context.mode == 'EDIT_MESH'
def execute(self, context):
ob = context.active_object
me = ob.data
bm = bmesh.from_edit_mesh(me)
# Save current selection
selected_verts = []
for v in bm.verts:
if v.select is True:
selected_verts.append(v.index)
if v.index != self.vert_and_group[0]:
v.select = False
ob.vertex_groups.active_index = self.vert_and_group[1]
bpy.ops.object.vertex_group_remove_from()
# Re-select vertices
for v in bm.verts:
if v.index in selected_verts:
v.select = True
#XXX Hacky, but there's no other way to update the UI panels
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()
return {'FINISHED'}
class AddToVertexGroup(bpy.types.Operator):
bl_idname = "mesh.vertex_group_add"
bl_label = "Add Vertex to Group"
bl_description = "Add a specific vertex to a specific vertex group"
def avail_vgroups(self, context):
if context is None:
return []
ob = context.active_object
bm = bmesh.from_edit_mesh(ob.data)
dvert_lay = bm.verts.layers.deform.active
items = []
self.vertex = bm.select_history.active.index
dvert = bm.select_history.active[dvert_lay]
#XXX since we need an identifier here, user won't be able to add a vgroup with that name ('-1')
#XXX could check against vgroup names and find an unused name, but it's a rare case after all.
items.append(("-1", "New Vertex Group", "Add a new vertex group to the active object", -1))
for i in ob.vertex_groups:
if i.index not in dvert.keys():
items.append((i.name, i.name, str(i.index), i.index))
return items
vertex: bpy.props.IntProperty()
available_vgroups: bpy.props.EnumProperty(items=avail_vgroups, name="Available Groups")
@classmethod
def poll(cls, context):
return context.mode == 'EDIT_MESH'
def execute(self, context):
ob = context.active_object
me = ob.data
bm = bmesh.from_edit_mesh(me)
#print(self.available_vgroups)
# Save current selection
selected_verts = []
for v in bm.verts:
if v.select is True:
selected_verts.append(v.index)
if v.index != self.vertex:
v.select = False
weight = context.tool_settings.vertex_group_weight
context.tool_settings.vertex_group_weight = 1.0
if self.available_vgroups == "-1":
bpy.ops.object.vertex_group_assign(new=True) #XXX Assumes self.vertex is the active vertex
else:
bpy.ops.object.vertex_group_set_active(group = self.available_vgroups)
bpy.ops.object.vertex_group_assign() #XXX Assumes self.vertex is the active vertex
context.tool_settings.vertex_group_weight = weight
# Re-select vertices
for v in bm.verts:
if v.index in selected_verts:
v.select = True
#XXX Hacky, but there's no other way to update the UI panels
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()
return {'FINISHED'}
class MESH_PT_ShowWeights(bpy.types.Panel):
bl_label = "Show Weights"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Edit"
bl_options = {'DEFAULT_CLOSED'}
@classmethod
def poll(cls, context):
return context.mode == 'EDIT_MESH'
def draw(self, context):
layout = self.layout
ob = context.active_object
me = ob.data
bm = bmesh.from_edit_mesh(me)
dvert_lay = bm.verts.layers.deform.active
row = layout.row(align=True)
# text = "Show" if not context.scene.show_vgroups_weights else "Hide"
# row.operator(ShowVGroupWeights.bl_idname, text=text)
# row.prop(context.scene, "show_vgroups_weights_limit")
if len(ob.vertex_groups) > 0:
# Active vertex
active_vert = bm.select_history.active
sub = layout.box()
col = sub.column(align = True)
if bm.select_mode == {'VERT'} and active_vert is not None:
col.label(text = "Active Vertex")
row = col.row()
row.label(text = "Vertex " + str(active_vert.index) + ":")
row.operator_menu_enum("mesh.vertex_group_add", "available_vgroups", text = "Add Group", icon = 'GROUP_VERTEX')
has_groups = False
vgroup_weights = []
for i in me.vertices:
if i.index == active_vert.index:
vgroup_weights_index = i.index
for j in range(len(i.groups)):
for k in ob.vertex_groups:
if k.index == i.groups[j].group:
has_groups = True
split = col.split(factor = 0.90, align = True)
vgroup_weights.append((k.index, i.groups[j].weight))
row = split.row(align = True)
row.prop(i.groups[j], "weight", text = k.name, slider = True, emboss = not k.lock_weight)
row = split.row(align = True)
row.operator("mesh.vertex_group_remove", text="", icon='X').vert_and_group = (i.index, k.index)
if not has_groups:
col.label(text = " No Groups")
else:
props = col.operator("mesh.vertex_group_assign")
props.index = vgroup_weights_index
for vgroup, weight in vgroup_weights:
item = props.vgroup_weights.add()
item.vgroup = vgroup
item.weight = weight
layout.separator()
else:
col.label(text = "No Active Vertex")
layout.prop(context.window_manager, "show_vgroups_show_all", toggle = True)
# All selected vertices (except for the active vertex)
if context.window_manager.show_vgroups_show_all:
for v in bm.verts:
if v.select:
if active_vert is not None and v.index == active_vert.index:
continue
sub = layout.box()
col = sub.column(align = True)
col.label(text = "Vertex " + str(v.index) + ":")
has_groups = False
vgroup_weights = []
for i in me.vertices:
if i.index == v.index:
vgroup_weights_index = i.index
for j in range(len(i.groups)):
for k in ob.vertex_groups:
if k.index == i.groups[j].group:
has_groups = True
split = col.split(factor = 0.90, align = True)
vgroup_weights.append((k.index, i.groups[j].weight))
row = split.row(align = True)
row.prop(i.groups[j], "weight", text = k.name, slider = True, emboss = not k.lock_weight)
row = split.row(align = True)
row.operator("mesh.vertex_group_remove", text="", icon='X').vert_and_group = (i.index, k.index)
if not has_groups:
col.label(text = " No Groups")
else:
props = col.operator("mesh.vertex_group_assign")
props.index = vgroup_weights_index
for vgroup, weight in vgroup_weights:
item = props.vgroup_weights.add()
item.vgroup = vgroup
item.weight = weight
else:
layout.label(text = "No Groups")
def create_properties():
bpy.types.WindowManager.show_vgroups_show_all = bpy.props.BoolProperty(
name = "Show All Selected Vertices",
description = "Show all vertices with vertex groups assigned to them",
default=False)
bpy.types.Mesh.assign_vgroup = bpy.props.StringProperty()
bpy.types.Scene.show_vgroups_weights = bpy.props.BoolProperty(
name="Show Vertex Groups/Weights",
default=False)
bpy.types.Scene.show_vgroups_weights_limit = bpy.props.IntProperty(
name="Limit",
description="Maximum number of weight overlays to draw",
default=20,
min=1,
max=1000,
soft_max=100)
# removal of ID-properties when script is disabled
def clear_properties(full=True):
if bpy.context.active_object is not None:
me = bpy.context.active_object.data
if hasattr(me, "show_vgroup_verts"):
del me["show_vgroup_verts"]
if hasattr(me, "show_vgroup_weights"):
del me["show_vgroup_weights"]
if full:
del bpy.types.WindowManager.show_vgroups_show_all
del bpy.types.Mesh.assign_vgroup
del bpy.types.Scene.show_vgroups_weights
del bpy.types.Scene.show_vgroups_weights_limit
classes = (
ShowVGroupWeights,
VGroupsWeights,
AssignVertexWeight,
RemoveFromVertexGroup,
AddToVertexGroup,
MESH_PT_ShowWeights
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
create_properties()
def unregister():
ShowVGroupWeights.handle_remove()
clear_properties()
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()