help about pose-bone rotation - need advice

help about pose-bone rotation - need advice

I try to rotate the keyframe of a pose-bone with its fcurve-entries
for location x,y,z and the rotation as quaternion w,x,y,z.

I use the matrix_local of this bone to calculate the new quat and location
and the result seems to be ok - but, i am not shure at all (i am a noob about
these math-things).

this is the calc.way:


XRot = Matrix.Rotation(radians(30), 3, "X")
YRot = Matrix.Rotation(radians(10), 3, "Y")
ZRot = Matrix.Rotation(radians(60), 3, "Z")
BoneMat = bpy.data.objects["Armature"].data.bones["Bone"].matrix_local
BoneMatInvert = BoneMat.copy().invert()
old_quat = Quaternion( fcurve-entry-W, X, Y, Z)
old_location = Vector( fcurve-entry-X, Y, Z)
##now is this calculation the right way?
# first the Invert of the BoneMatrix and then the BoneMatrix again
new_quat = ( (BoneMatInvert*(ZRot*YRot*XRot))*(BoneMat*old_quat.to_matrix()) ).to_quat()
## and here for the location the same way? 
new_location = old_location * BoneMat * (ZRot*YRot*XRot) * BoneMatInvert

Question: do i use the right BoneMatrix for the Invert/not-Invert calculation
and is this the correct way? (the x/y/z Rotations and calculation works for my example, but will it work with different bone-animations and the bone with a different restposition?