I am trying to programmatically add new FK rig controls to a character which is already rigged with deformation bones. To do this, I duplicate a deformation bone for which I want to add a new control, rename it, give it a custom shape, turn off deformation and add a copy transform constraint on deformation bone targeting this new bone. My problem is that when I use the bpy.ops.armature.duplicate_move() operator to duplicate, nothing happens - except that the selected bone is deselected. Here is an example execution on the blender console (I was in armature edit mode when I ran these commands):

>>> len(D.armatures['skeleton_human_female'].edit_bones)

>>> C.active_object

>>> C.active_bone.name

>>> len(C.selected_bones)

>>> C.selected_bones[0].name

>>> bpy.ops.armature.duplicate_move()

>>> len(D.armatures['skeleton_human_female'].edit_bones)

>>> len(C.selected_bones)

As you can see, the number of bones in the armature doesn't change. I am using blender 2.78c. What could be wrong?

Thanks in advance!

edit: I found that duplicating using duplicate_move() operator is not my favourite way to create new bones in the same position. Instead I just create a new bone, and assign head and tail elements appropriately, using a function like this:

def createNewBone(object, new_bone_name, parent_name, parent_connected, head, tail, roll):
    bpy.ops.object.mode_set(mode='EDIT', toggle=False)


    new_edit_bone = object.data.edit_bones[new_bone_name]
    new_edit_bone.use_connect = parent_connected
    new_edit_bone.parent = object.data.edit_bones[parent_name]    
    new_edit_bone.use_inherit_rotation = True
    new_edit_bone.use_local_location = True
    new_edit_bone.use_inherit_scale = False

    new_edit_bone.head = head
    new_edit_bone.tail = tail
    new_edit_bone.roll = roll
  • $\begingroup$ Running above code on a simple one bone armature (in edit mode) gives me a selected copy of the bone. The original is deselected. I'm wondering if it is a bone layer issue...??? can you post a sample file showing same behaviour or an image showing bone layers. $\endgroup$
    – batFINGER
    Mar 21 '17 at 13:21
  • $\begingroup$ @batFINGER I somehow lost the actual code I was using which caused the issue, but I was changing selected and active bones using the bpy.context, unlike Tak's approach below, which I verified works as expected. $\endgroup$
    – Aman
    Mar 28 '17 at 5:27

Try this:

import bpy

arm = bpy.data.objects['Armature']
bone_to_duplicate = arm.data.edit_bones['Bone']
arm.data.edit_bones.active = bone_to_duplicate
arm.data.edit_bones.active.select = True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.