1

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.

enter image description here

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?

dimus
  • 3,292
  • 9
  • 32
  • 43

1 Answers1

1

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 :)

dimus
  • 3,292
  • 9
  • 32
  • 43