I want to convert a quaternion to a matrix and back again. The result has a different orientation than the quaternion from the beginning.

To reproduce the problem, set a quaternion.

ob = bpy.context.object
bpy.context.object.rotation_quaternion = mathutils.Quaternion((0.7795193195343018, 0.045188724994659424, 0.15823090076446533, 0.048953697085380554))

Then convert the quaternion to a matrix and apply it back to the object.

ob.matrix_world = ob.rotation_quaternion.to_matrix().to_4x4()

The object not only changes its orientation, but its scale.


Your quaternion is not normalized, this can happen during interpolation.

Blender will still only view the quaternion as an orientation, even if it is not pure. However, the to_matrix function will convert the quaternion as it is.

To circumvent this, simply normalize the quaternion before converting it to a matrix.

ob.matrix_world = ob.rotation_quaternion.normalized().to_matrix().to_4x4()

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.