1
$\begingroup$

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)
67

>>> C.active_object
bpy.data.objects['skeleton_human_female']

>>> C.active_bone.name
'upperarm_L'

>>> len(C.selected_bones)
1

>>> C.selected_bones[0].name
'upperarm_L'

>>> bpy.ops.armature.duplicate_move()
{'FINISHED'}

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

>>> len(C.selected_bones)
0

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)

    bpy.ops.armature.bone_primitive_add(name=new_bone_name)

    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
$\endgroup$
2
  • $\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
    Commented Mar 21, 2017 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
    Commented Mar 28, 2017 at 5:27

1 Answer 1

0
$\begingroup$

Try this:

import bpy

arm = bpy.data.objects['Armature']
bpy.ops.object.mode_set(mode='EDIT')
bone_to_duplicate = arm.data.edit_bones['Bone']
arm.data.edit_bones.active = bone_to_duplicate
arm.data.edit_bones.active.select = True
bpy.ops.armature.duplicate_move()
$\endgroup$

You must log in to answer this question.

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