-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh_select_vertex_groups.py
234 lines (202 loc) · 8.48 KB
/
mesh_select_vertex_groups.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
# ##### 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 LICENSE BLOCK #####
bl_info = {
"name": "Select Vertex Groups",
"author": "Martin Ellison",
"version": (1, 0),
"blender": (2, 71, 0),
"location": "Toolbox",
"description": "Finds all the vertex groups that chosen verts are in, & any verts that are not in any group",
"warning": "Buggy", # used for warning icon and text in addons panel
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
"Scripts/Modeling/Select_Vertex_Groups",
"tracker_url": "https://developer.blender.org/T22025",
"category": "Mesh"}
"""
This script finds all the vertex groups that chosen vertexes are in, and any vertexes that are not in any vertex group.
This is useful for cleaning up a mesh if vertex groups under animation go off to the wrong place (because they are in a vertex group that they should not be in, or not in the vertex group that they should be in).
How to use:
1. select a mesh and get into edit mode.
2. by default it will use all vertexes; alternatively, select vertexes of interest and click on 'use selected vertexes'. Note that subsequent selections and deselections of vertexes will not change the set of vertexes to be used, you need to click on these buttons again to do that.
3. click on 'select' and 'deselect' buttons for listed vertex groups, and for no vertex group, to select and deselect vertexes.
This only lists vertex groups that have used vertexes.
You may want to use the mesh select/deselect all (keyboard A) operator to start.
Once you have the right vertexes selected, you can use the standard vertex groups property editor to add them to or remove them from the desired vertex groups.
"""
import bpy
from bpy.props import *
global use_selected_only, used_vertexes, the_mesh, vertex_usage
use_selected_only = False
used_vertexes = set()
the_mesh = None
vertex_usage = ''
class UseAll(bpy.types.Operator):
bl_idname = "mesh.primitive_fvg_useall"
bl_label = "Use all Vertexes"
bl_register = True
bl_undo = True
#limit = FloatProperty(name="limit", description="Ignore weights under this limit.", default= 0.01, min = 0.0, max = 1.0, soft_min=0.0, soft_max=1.0)
def execute(self, context):
global use_selected_only
use_selected_only = False
bpy.ops.object.editmode_toggle()
set_used()
bpy.ops.object.editmode_toggle()
return {'FINISHED'}
class UseSelected(bpy.types.Operator):
bl_idname = "mesh.primitive_fvg_useselected"
bl_label = "Use Selected Vertexes"
bl_register = True
bl_undo = True
def execute(self, context):
global use_selected_only
use_selected_only = True
bpy.ops.object.editmode_toggle()
set_used()
bpy.ops.object.editmode_toggle()
return {'FINISHED'}
class SelectFound(bpy.types.Operator):
bl_idname = "mesh.primitive_fvg_selfound"
bl_label = "Select"
bl_register = True
bl_undo = True
vertexgroup = bpy.props.StringProperty(name = 'vertexgroup', description = 'vertexgroup', default = '', options = set())
def execute(self, context):
global the_mesh
bpy.ops.object.editmode_toggle()
vertexgroup = self.properties.vertexgroup
fv = found_verts(vertexgroup)
for v in fv: v.select = True
bpy.ops.object.editmode_toggle()
return {'FINISHED'}
class DeselectFound(bpy.types.Operator):
bl_idname = "mesh.primitive_fvg_deselfound"
bl_label = "Deselect"
bl_register = True
bl_undo = True
vertexgroup = bpy.props.StringProperty(name = 'vertexgroup', description = 'vertexgroup', default = '', options = set())
def execute(self, context):
global the_mesh
bpy.ops.object.editmode_toggle()
vertexgroup = self.properties.vertexgroup
fv = found_verts(vertexgroup)
for v in fv: v.select = False
bpy.ops.object.editmode_toggle()
return {'FINISHED'}
def set_used():
global use_selected_only, used_vertexes, the_mesh, vertex_usage
obj = bpy.context.active_object
used_vertexes = set()
if use_selected_only:
for v in obj.data.vertices:
if v.select: used_vertexes.add(v.index)
else:
for v in obj.data.vertices: used_vertexes.add(v.index)
the_mesh = obj
vertex_usage = '%d vertexes used' % (len(used_vertexes))
def make_groups(limit):
global used_vertexes
vgp = []
vgdict = {}
vgused = {}
obj = bpy.context.active_object
all_in_group = True
for vg in obj.vertex_groups:
vgdict[vg.index] = vg.name
for v in obj.data.vertices:
in_group = False
if v.index in used_vertexes:
for g in v.groups:
gr = g.group
w = g.weight
if w > limit:
if not gr in vgused: vgused[gr] = 0
vgused[gr] += 1
in_group = True
if not in_group: all_in_group = False
if not all_in_group:
vgp.append(("no group", "(No group)"))
for gn in vgused.keys():
name = vgdict[gn]
vgp.append((name, '%s has %d vertexes' % (name, vgused[gn]) ))
print("%d groups found\n" % len(vgp))
return vgp
def found_verts(vertex_group):
global used_vertexes
vgfound = []
obj = bpy.context.active_object
if vertex_group == 'no group':
for v in obj.data.vertices:
if v.index in used_vertexes and (not v.groups):
vgfound.append(v)
else:
vgnum = obj.vertex_groups.find(vertex_group)
for v in obj.data.vertices:
if v.index in used_vertexes:
for g in v.groups:
if g.group == vgnum:
vgfound.append(v)
break
print('%d vertexes found for %s' % (len(vgfound), vertex_group))
return vgfound
class VIEW3D_PT_FixVertexGroups(bpy.types.Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_label = "Select Vertex Groups"
bl_category = 'Tools'
bl_options = {'DEFAULT_CLOSED'}
@classmethod
def poll(self, context):
if bpy.context.active_object:
obj = bpy.context.active_object
if obj.type == 'MESH' and obj.mode == 'EDIT': return True
return False
def draw(self, context):
global use_selected_only, used_vertexes, the_mesh, vertex_usage
if bpy.context.active_object:
obj = bpy.context.active_object
if obj.type == 'MESH' and obj.mode == 'EDIT':
layout = self.layout
use_all = layout.operator("mesh.primitive_fvg_useall", "Use all vertexes")
layout.operator("mesh.primitive_fvg_useselected", "Use selected vertexes")
if use_selected_only:
layout.label(text = 'Using selected vertexes.')
else:
layout.label(text = 'Using all vertexes.')
layout.label(vertex_usage)
if len(used_vertexes) == 0 or obj is not the_mesh: set_used()
#layout.prop(use_all, 'limit', slider = True)
#groups = make_groups(use_all.limitval)
groups = make_groups(0.01)
for gp in groups:
layout.label(text = gp[1])
row = layout.row()
sel_op = row.operator("mesh.primitive_fvg_selfound", "Select")
sel_op.vertexgroup = gp[0]
desel_op = row.operator("mesh.primitive_fvg_deselfound", "Deselect")
desel_op.vertexgroup = gp[0]
classes = [UseAll, UseSelected, SelectFound, DeselectFound]
def register():
bpy.utils.register_module(__name__)
pass
def unregister():
bpy.utils.unregister_module(__name__)
pass
if __name__ == "__main__":
print('------ executing --------')
register()