p2or
8/21/2017 - 5:44 PM

blender-merge-vertex-groups.py

import bpy

group_input = {"Group", "Group.001", "Group.002"}
ob = bpy.context.active_object

group_lookup = {g.index: g.name for g in ob.vertex_groups}
group_candidates = {n for n in group_lookup.values() if n in group_input}

# test whether all candidates components of group_lookup
if all(n in group_lookup.values() for n in group_candidates):
    pass

# general tests
if (len(group_candidates) and ob.type == 'MESH' and
    bpy.context.mode == 'OBJECT'):
    
    # iterate through the vertices and sum the weights per group
    vertex_weights = {}
    for vert in ob.data.vertices:
        if len(vert.groups):  
            for item in vert.groups:
                vg = ob.vertex_groups[item.group]
                if vg.name in group_candidates:
                    if vert.index in vertex_weights:    
                        vertex_weights[vert.index] += vg.weight(vert.index)
                    else:
                        vertex_weights[vert.index] = vg.weight(vert.index)
        
    # clamp/slice values above 1.0
    for key in vertex_weights.keys():
        if (vertex_weights[key] > 1.0): vertex_weights[key] = 1.0
    
    # create new vertex group
    vgroup = ob.vertex_groups.new(name="+".join(group_candidates))
    
    # add the values to the group                       
    for key, value in vertex_weights.items():
        vgroup.add([key], value ,'REPLACE') #'ADD','SUBTRACT'