如何将顶点组从一个对象添加到另一个对象而不替换它们?

时间:2016-12-05 07:27:18

标签: blender

我正在使用blender来创建一个人体模型,我有两个模型,其中一个是顶点组列表,另一个是同一个模型的副本,另一组是顶点组。我想知道无论如何我都可以使用这两组顶点组获得相同模型的副本。或者我可以将顶点组从一个模型复制到另一个模型而不替换已经存在的顶点组。

1 个答案:

答案 0 :(得分:0)

搅拌机附带一个插件,可以将活动对象的顶点权重复制到其他选定对象。正如您所提到的,插件将覆盖任何具有匹配名称的现有顶点权重。

通过获取从插件执行顶点权重复制的代码并进行小调整,以便在名称已存在时使用目标顶点组的新名称,我得到以下小脚本。

将其粘贴到搅拌机的text editor中,然后点击运行脚本。 active object中的顶点组将复制到任何其他选定对象。

import bpy

active = bpy.context.active_object

for ob in bpy.context.selected_objects:
    me_source = active.data
    me_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))
        continue

    vgroups_IndexName = {}
    for i in range(0, len(active.vertex_groups)):
        groups = active.vertex_groups[i]
        vgroups_IndexName[groups.index] = groups.name
    data = {}  # vert_indices, [(vgroup_index, weights)]
    for v in me_source.vertices:
        vg = v.groups
        vi = v.index
        if 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 target
    if ob != active:
        # add missing vertex groups
        for vgroup_idx, vgroup_name in vgroups_IndexName.items():
            #check if group already exists...
            already_present = 0
            for i in range(0, len(ob.vertex_groups)):
                if ob.vertex_groups[i].name == vgroup_name:
                    vgroup_name = vgroup_name+'_from_'+active.name
                    vgroups_IndexName[vgroup_idx] = vgroup_name
            # ... if not, then add
            if already_present == 0:
                ob.vertex_groups.new(name=vgroup_name)
        # write weights
        for 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_groups
                        for vgs in range(0, len(groups)):
                            if groups[vgs].name == groupName:
                                groups[vgs].add((v.index,),
                                   vgroupIndex_weight[i][1], "REPLACE")