Transforming armature bone by matrix of another bone in pose mode

I am attempting to move an armature bone in edit mode by the inverted matrix of another bone in pose mode. My reasoning for doing this is because I have joined two armatures and have lined the bones of the second armature up with the first armature while the first armature was in pose mode (made it much easier). So what I’m attempting to do is keep the same location/rotation offsets between the two, but transform the second armature’s bones to be relative to the default position of the first armature. I hope that makes sense. I believe this is possible, but my below script does not produce the desired result.

If I’m not mistaken I would just need to apply the inverted matrix of bone1 in pose mode to the bone2 in edit mode (which would move the bone2 the opposite amounts applied to bone1 to orient it to its location/rotation in pose mode)…right? Or am I missing something? I have also done up some screen captures which might better illustrate what I’m looking to accomplish:

…and below is what I have so far code wise, it’s really pretty simple, just get the matrix of poseBone and apply it’s inverse to EditBone:

import bpy
from mathutils import Vector, Matrix
import numpy as np

matFilePath = 'Z:\\models\\myfps\\weapons\\pretty\\arms\\script\\mat.txt'

def convert_matrix_to_string(some_matrix):
    # omitted for brevity

def convert_file_to_matrix(some_path):
    # omitted for brevity
# select the bone in pose mode and run this to save to matFilePath
#for b in bpy.context.selected_pose_bones:
#    f = open(matFilePath, "w")
#    f.write(convert_matrix_to_string(b.matrix))
#    f.close()

# load in the matrix from file
hand_pose_mat = convert_file_to_matrix(matFilePath)

# apply inversion of matrix to bone
for b in bpy.context.selected_bones:

Alright, I think I’m on the right track now. Still working on a solution but I believe my original issues were that I was not removing the existing transformation from bone2 before applying a new transform, as well as thinking that bone1’s transform was relative to it’s position in edit mode instead of the origin of the armature. Will throw up a solution once I have one :slight_smile:

The solution to this was to transform bone2 by the inverse of bone1’s pose mode matrix, then transform bone2 again by bone1’s matrix in edit mode:

for b in bpy.context.selected_bones: