I am using blender to create a human model and i have two models one is rigged with a list of vertex groups and another copy of the same model has another set of vertex groups. I was wondering if there is anyway i can have a copy of the same model with both these sets of vertex groups. Or a way i can copy the vertex groups from one model to another without replacing the already existing vertex groups.

1

Best Answer


There is an addon included with blender that can copy vertex weights from the active object to the other selected objects. As you mention the addon will overwrite any existing vertex weights that have matching names.

By taking the code that does the vertex weight copy from the addon and making a small adjustment so that it uses a new name for the destination vertex group when the name already exists, I get the following small script.

Paste it into blender's text editor and click run script. The vertex groups in the active object are copied to any other selected objects.

import bpyactive = bpy.context.active_objectfor ob in bpy.context.selected_objects:me_source = active.datame_target = ob.data# sanity check: do source and target have the same amount of verts?if len(me_source.vertices) != len(me_target.vertices):print('ERROR: objects {} and {} have different vertex counts.'.format(active.name,ob.name))continuevgroups_IndexName = {}for i in range(0, len(active.vertex_groups)):groups = active.vertex_groups[i]vgroups_IndexName[groups.index] = groups.namedata = {} # vert_indices, [(vgroup_index, weights)]for v in me_source.vertices:vg = v.groupsvi = v.indexif len(vg) > 0:vgroup_collect = []for i in range(0, len(vg)):vgroup_collect.append((vg[i].group, vg[i].weight))data[vi] = vgroup_collect# write data to targetif ob != active:# add missing vertex groupsfor vgroup_idx, vgroup_name in vgroups_IndexName.items():#check if group already exists...already_present = 0for i in range(0, len(ob.vertex_groups)):if ob.vertex_groups[i].name == vgroup_name:vgroup_name = vgroup_name+'_from_'+active.namevgroups_IndexName[vgroup_idx] = vgroup_name# ... if not, then addif already_present == 0:ob.vertex_groups.new(name=vgroup_name)# write weightsfor v in me_target.vertices:for vi_source, vgroupIndex_weight in data.items():if v.index == vi_source:for i in range(0, len(vgroupIndex_weight)):groupName = vgroups_IndexName[vgroupIndex_weight[i][0]]groups = ob.vertex_groupsfor vgs in range(0, len(groups)):if groups[vgs].name == groupName:groups[vgs].add((v.index,),vgroupIndex_weight[i][1], "REPLACE")