0
$\begingroup$

I'm trying to duplicate some bones but for some reason it's not working at all, there's no error on runtime, it simply won't duplicate the selected bones. Do you have any idea why it's happening?

import bpy

def create_tgt():
    armature = bpy.data.objects['Armature']  # Use your armature name
    bones = armature.data.edit_bones[:]  # This'll give us a list with all bones
    if not bpy.context.mode == 'EDIT_ARMATURE':
        bpy.ops.armature.select_all(action='DESELECT')
        armature.select = True
        bpy.ops.object.mode_set(mode='EDIT')
    for bone in bones:  # Let's loop through them all
        if 'DEF-' in bone.name:
            bone.select = True
    bpy.ops.armature.duplicate()

create_tgt()

After running this script, no bones are selected and no bones are duplicated.

$\endgroup$

2 Answers 2

1
$\begingroup$

I have the same issue, but I have noticed that, when this method is used, the bones are selected, BUT not the heads nor the tails. If you try to select them manually in the outliner or use the "bpy.ops.armature.select_all(action='SELECT')" command line, then everything is selected (bones, tails and heads) and duplicate works properly.

I don´t know if it helps.

$\endgroup$
1
  • $\begingroup$ Your answear helped me get to the correct behaviour I was looking for. To be able to dublicate only the bones selected you only need to select the head and tail as well, and then the bpy.ops.armature.duplicate() code works like a charm. $\endgroup$ Commented Jan 6, 2022 at 16:06
0
$\begingroup$

To solve this problem I only had to select the head and tail of the bones as well. This is the working code now:

import bpy

def create_tgt():
    armature = bpy.data.objects['Armature']  # Use your armature name
    bones = armature.data.edit_bones[:]  # This'll give us a list with all bones
    if not bpy.context.mode == 'EDIT_ARMATURE':
        bpy.ops.armature.select_all(action='DESELECT')
        armature.select = True
        bpy.ops.object.mode_set(mode='EDIT')
    for bone in bones:  # Let's loop through them all
        if 'DEF-' in bone.name:
            bone.select = True
            bone.select_head = True
            bone.select_tail = True
    bpy.ops.armature.duplicate()

create_tgt()
$\endgroup$

You must log in to answer this question.

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