# How to clean weight groups by script?

After transferring weights from one object to another vertices might get multiple groups with very low weights. In weight painting view there is a tool 'Clean' that allows to remove all the groups with low weights.

To automate the process I am trying a script like

for obj in bpy.context.visible_objects:
if obj.name[-2:] == '-l':
obj.vertex_group_clean(group_select_mode='ALL', limit=0.005, keep_single=True)


In this snippet I go through visible objects, and if their name ends with "-l" I want to run vertex_group_clean method, but the script fails with AttributeError: 'Object' object has no attribute 'vertex_group_clean'

What do I do wrong?

I did come up with a solution, but probably not the most elegant one. The vertex_group_clean method is an accessor to a menu, it is not a method to an object. So in the solution I select and activate an object, then switch to weight view, select all the vertices, do cleaning, and switch back to object view.

def active(obj):
bpy.ops.object.select_all(action='DESELECT')
obj.select=True
bpy.context.scene.objects.active = obj
bpy.ops.object.mode_set(mode = 'OBJECT')

for obj in bpy.context.visible_objects:
if obj.name[-2:] == '-l':
active(obj)
bpy.ops.paint.weight_paint_toggle()
bpy.ops.paint.vert_select_all(action="SELECT")
bpy.ops.object.vertex_group_clean(group_select_mode='ALL', limit=0.005, keep_single=True)
bpy.ops.paint.vert_select_all(action="DESELECT")
bpy.ops.paint.weight_paint_toggle()


It does the job, but I hope there is a better way to write this :)

• Do you mean bpy.ops.object.vertex_group_clean(...) ? Have a look at this post – batFINGER May 30 '18 at 12:02
• oups, @batFINGER, yes that was a typo, fixing it – dimus May 30 '18 at 19:40