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
bpy.context
, unlike Tak's approach below, which I verified works as expected. $\endgroup$