SoFunction
Updated on 2025-03-02

Example of implementation of Euler angle and rotation matrix transformation in python

1. Configure PIP image source

pip config set -url /simple

2. Download numpy and other libraries

pip install scipy

3. Euler angle rotation matrix (right-hand coordinate system, ZXY order)

# coding=UTF-8
from  import Rotation as R
 
# Define Euler angle XYZ ordereuler_angles = [90, 3, 3]

if (90 - abs(euler_angles[0])) < 0.5:
    euler_angles[0] = 89.5
euler_angles_zxy = [0, 0, 0]
euler_angles_zxy[0] = euler_angles[2]
euler_angles_zxy[1] = euler_angles[0]
euler_angles_zxy[2] = euler_angles[1]
euler_angles_zxy = [-x for x in euler_angles_zxy]
 
# Create a Rotation objectr = R.from_euler('zxy', euler_angles_zxy, degrees=True)
 
# Get the rotation matrixrotation_matrix = r.as_dcm()
print('[')
for row in rotation_matrix:
    msg = ", ".join(map(str, row))
    msg = '[' + msg + '],'
    print(msg)
print(']')

4. Rotation matrix to Euler angle (right-hand coordinate system, ZXY order)

# coding=UTF-8
import numpy as np
from  import Rotation as R
 
# Suppose we have a rotation matrix#80 -7 45
rotation_matrix = ([
[0.9945219996629926, 0.10452647320585942, -0.00045671157999537665],
[-0.00045671157999537665, 0.008714576084760173, 0.9999619230641713],
[0.10452647320585942, -0.9944839227271639, 0.008714576084760173]
])
# Convert Euler angles using SciPy's Rotation objectrotation = R.from_dcm(rotation_matrix)
euler_angles_zxy = rotation.as_euler('zxy', degrees=True)  # Sco storage uses 'zxy' order, if the degree is required, set degrees=True
#Replace positive and negative values ​​of left and right reference coordinate systemeuler_angles_zxy = [-x for x in euler_angles_zxy]
euler_angles = [0, 0, 0]
euler_angles[0] = euler_angles_zxy[1]
euler_angles[1] = euler_angles_zxy[2]
euler_angles[2] = euler_angles_zxy[0]
print(euler_angles)

This is the article about the implementation examples of Python Euler angle and rotation matrix transformation. For more related contents of Python Euler angle and rotation matrix transformation, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!