Description Of What I Am Doing

I am building a rig that uses both IK and FK controls. I want this rig to have a script which will set the rotations of my FK control bones to match the pose of the IK bones (similar to rigify).

My method is to do the following:

  • Name the IK and FK bones similarly
  • Use bpy to collect the FK bones I desire and find their corresponding IK counterparts

  • Set the pose matrices of the FK bones to their corresponding IK bones

Here is the code I use:

# ... code will define variables for armature and id for IK-chain   

# move all FK bones to the current IK orientation
for bone in armature.bones:
    if "MCH-finger-fk." + IK_id in bone.name:
        ik_name = bone.name[:11] + "ik" + bone.name[13:]
        ik_bone = armature.bones[ik_name]
        bone.matrix = ik_bone.matrix


This almost works! The first bone in the FK sequence matches the corresponding IK bone perfectly. The remaining bones do not, but if I run it again, the next bone in the sequence will match! That is, the second time I run the script, the second bone in the FK chain will match its corresponding IK bone.

The pictures below will hopefully explain what I mean.

Before Script Execution

Before Script Execution

Script Executed Once

Script Executed Once

Script Executed Twice

Alas, I cannot have more than two links, due to my low reputation. Imagine moving the first separated bone to where it looks like it should go (the remaining bones move relative) in the previous image.


I assume this bug in my script has something to do with the fact that only the second time I execute the script, the bone data takes the parent matrix into account (since the pose is now in place).

My Question

Is there a nicer way to achieve what I am trying? In essence, I want to copy the world rotations of each bone. I tried looking for the source code of the COPY_ROTATION constraint, but I was unsuccessful.

  • $\begingroup$ Armatures can get confusing as there are two sets of bones, it looks like you are altering the edit bones locations, such as bpy.data.armatures['rig'].bones['upper_arm.L']. You find the pose bones at bpy.data.objects['rig'].pose.bones['upper_arm.L']. $\endgroup$ – sambler Aug 10 '16 at 7:48
  • $\begingroup$ Sorry about the lack of previous code. The armature variable is indeed bpy.data.objects['rig'].pose, but the bug unfortunately persists. $\endgroup$ – mvarble Aug 10 '16 at 19:26

See this section of the api documentation. Setting PoseBone.matrix probably does the calculations required for PoseBone.matrix_basis but does not update the value of the underlying DNA world matrix property, which is wierd. Here is a script which uses the scene.update method:

import bpy

class BoneMatrixCopier:
    def __init__(self, obj, scene):
        self.obj = obj
        self.scene = scene
        self.pose_bones = obj.pose.bones

    def update(self):

    def get_ik_bone_name(self, name):
        return name.replace("Bone", "BoneIK")

    def copy_matrix(self, bone):
        bone.matrix = self.pose_bones[

    def copy_matrices(self, root_bone_name):
        root_bone = self.pose_bones[root_bone_name]

        for bone in root_bone.children_recursive:

bmc = BoneMatrixCopier(bpy.data.objects['Armature'], bpy.context.scene)
  • $\begingroup$ Thank you very much! This is exactly what I was looking for. $\endgroup$ – mvarble Aug 15 '16 at 21:16

The Problem

It appears that the matrix of each bone is set in relation to the parent, which at the time of execution, is not matching its corresponding IK bone. This is why the script works after executing multiple times.


Before changing the matrix of each bone, I may update the armature's bone matrices, so that this relative to previous parent position issue will no longer happen. I can do this by calling bpy.ops.pose.visual_transform_apply():

for bone in armature.bones:
    if "MCH-finger-fk." + IK_id in bone.name:
        ik_name = bone.name[:11] + "ik" + bone.name[13:]
        ik_bone = armature.bones[ik_name]
        bone.matrix = ik_bone.matrix
  • 1
    $\begingroup$ Since visual_transform_apply is an operator it will update the scene. You can do this directly via Scene.update. $\endgroup$ – pink vertex Aug 12 '16 at 11:01

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.