Convert matrix_local xyz<->xzy

I’m writing a script that imports and exports object properties that will be used in another program. One of the properties is object’s rotation stored as rotation matrix (the same as bpy.types.Object.matrix_local). The other program that reads object’s properties from a file uses xzy space (meaning y is up/down axis). How can I convert matrix_local in xyz space to matrix_local in xzy space and vice versa ?

matrix_local is a 4x4 matrix storing translation, scale and rotation.


matrix_swap=Matrix([
    [1,0,0,0],
    [0,0,1,0],
    [0,1,0,0],
    [0,0,0,1]
    ])

matrix_local_xzy=matrix_swap*matrix_local*matrix_swap

There is a helper function for this in bpy_extras.io_utils


def axis_conversion(from_forward='Y', from_up='Z', to_forward='Y', to_up='Z'):

Check out the bvh importer for how to use. Basically it returns a conversion matrix to multiply your local_matrix by on import / export.

Blender is -Y forward Z up and a lot of programs use -Z forward Y up.


&gt;&gt;&gt; m = axis_conversion(from_forward = '-Y', from_up='Z', to_forward='-Z', to_up='Y')
&gt;&gt;&gt; m.to_4x4()
Matrix(((-1.0, 0.0, 0.0, 0.0),
        (0.0, 0.0, 1.0, 0.0),
        (0.0, 1.0, 0.0, 0.0),
        (0.0, 0.0, 0.0, 1.0)))

Awesome ! :eyebrowlift: