How to create mirror rotation for pose bones?

Hi,

like the topic says, how to properly create mirror rotation for pose bone?

I’d like to use mirror rotations similar to what one can do with pose copy paste, but from Python, without using ops. I can get mirrored position and rotation using something like this with matrix, but it will ruin the scale:

def mirror_bone_pose(bone):
    mmtx = Matrix.Scale(-1, 4, (1, 0,0))
    mtx = bone.matrix
    bone.matrix = mmtx @ mtx 

mirror_bone_pose(bpy.context.object.pose.bones[0])

Any ideas how to do only the rotation part?

I don’t specifically need to use matrices to do this, as I’m not very familiar with them, but it used to work fine in 3ds Max for example… with existing helper methods… and in general, it makes it easy to do the mirroring in object space (like flipping legs while walking diagonally relative to world main axes).

I am currently not working or coding in Blender, but here is some gawd-awful function I created several years back for my automatic animation program that maybe you can figure out by playing around with it, it is a short function, the input parameters are pretty clear, so it may fit the purpose:

    # Rotate the easy way
    def rotate(bipedStrName, str_bone_name, rad=0, axis=0):
        bpy.ops.object.mode_set(mode='POSE')
        ob = bpy.context.object
        euler = ob.pose.bones[str_bone_name]
        euler.rotation_mode = 'XYZ'
        bpy.data.objects[bipedStrName].pose.bones[str_bone_name].rotation_euler[axis] = rad

You will notice the “=” sign in the parameters, those are default values, as I recall. Also, one of the tricks I use to use to get the opposite action was just use the same code, but use the negative sign on the rotation. The code for the automatic animation is on my github website, you may find other gems there: https://github.com/Skywola/anim I am currently non-active in this particular line of work, so you are pretty much on your own as far as working out how it all works, I am gearing up for a new project that will use artificial intelligence.

Thanks for the reply

I actually solved this and posted the answer to Stack Exchange a while ago.

Here is my code, I bet someone else might find it useful, although code might be a bit hacky:

def mirrorPose(bone):
    m = bone.matrix.Diagonal((-1,1,1,0))
    bone.matrix = m @ bone.matrix
    bone.scale = (1,1,1)

    bpy.context.view_layer.update()

    z = mathutils.Euler((0,0,math.radians(180))).to_matrix().to_4x4()
    bone.matrix = bone.matrix @ z
    bpy.context.view_layer.update()

    y = mathutils.Euler((0,math.radians(180),0)).to_matrix().to_4x4()
    bone.matrix = bone.matrix @ y

If someone can simplify the code… it would be great but this has worked so far.

this should work using pose copy/paste/paste-flipped operators.

armature = bpy.data.objects['armature']
for p_bone in armature.pose.bones[:]:
    clean = True
    for filter in ["muscle", "IK_" 'reverse', 'helper']: # Use this filter bones/groups
        if filter in p_bone.name:
            clean = False
            break
    if clean: 
        print(p_bone.name)
        p_bone.bone.select = True

bpy.ops.pose.copy()
bpy.ops.pose.paste(flipped=True) # False if not flipped

Thanks, but this was the point I mentioned in the original question ==> “without using ops”