1
$\begingroup$

I am trying to figure out how I can select multiple objects, and smooth every weight(vertex group) on each object using python, instead of manually doing so for each object. Thanks.

$\endgroup$

1 Answer 1

1
$\begingroup$

That's probably what you need.

import bpy

first_active_obj = bpy.context.view_layer.objects.active
save_mode = first_active_obj.mode

for i in bpy.context.selected_objects:
#    Skip object if vertex group is empty and if object is not a mesh object
    if not i.vertex_groups and i.type != 'MESH': 
        continue
    
#    Set active object and get this object
    active_obj = bpy.context.view_layer.objects.active = i

    bpy.ops.object.mode_set(mode='WEIGHT_PAINT')    
    save_idx = active_obj.vertex_groups.active_index
    
    for idx, _ in enumerate(active_obj.vertex_groups):
        active_obj.vertex_groups.active_index = idx
#        Smooth
        bpy.ops.object.vertex_group_smooth(group_select_mode='ALL', factor=0.5, repeat=5, expand=0)

#   Restore active index
    active_obj.vertex_groups.active_index = save_idx
    
    bpy.ops.paint.weight_paint_toggle()

#   Restore active object, and restore mode
bpy.context.view_layer.objects.active = first_active_obj   
bpy.ops.object.mode_set(mode=save_mode)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .