3D Coordinate Transformations

0.4.2 · active · verified Mon Apr 13

transforms3d is a Python library providing a collection of functions and classes for creating and converting 3-dimensional transformations, including rotations, zooms, shears, and reflections. It is largely based on the `transformations.py` module by Christoph Gohlke, with significant restructuring and ongoing maintenance by Matthew Brett. The library is currently at version 0.4.2 and sees active maintenance for bugfixes and compatibility updates, particularly with NumPy.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates converting Euler angles to a rotation matrix, then converting that matrix to a quaternion, and finally reconstructing the matrix from the quaternion. This covers common transformations and module imports from `transforms3d` while leveraging `numpy` for array handling. The `axes` argument in `euler2mat` specifies the order and type of Euler angles (e.g., 'sxyz' for static XYZ).

import numpy as np
from transforms3d.euler import euler2mat, mat2euler
from transforms3d.quaternions import mat2quat, quat2mat

# Define Euler angles (roll, pitch, yaw) in radians
r, p, y = np.deg2rad(30), np.deg2rad(45), np.deg2rad(60)

# Convert Euler angles to a 3x3 rotation matrix
rotation_matrix = euler2mat(r, p, y, axes='sxyz')
print("Rotation Matrix:\n", rotation_matrix)

# Convert rotation matrix to a quaternion (w, x, y, z order)
quaternion = mat2quat(rotation_matrix)
print("Quaternion (w, x, y, z): ", quaternion)

# Convert quaternion back to rotation matrix
reconstructed_matrix = quat2mat(quaternion)
print("Reconstructed Matrix:\n", reconstructed_matrix)

# Verify accuracy
assert np.allclose(rotation_matrix, reconstructed_matrix)

view raw JSON →