PyQuaternion
PyQuaternion is a fully featured, pythonic library designed for representing and manipulating quaternions. It provides extensive functionality for 3D animation, geometry, and numerical computations, including operations like rotation, interpolation, differentiation, and integration. The current version is 0.9.9, released in October 2020, suggesting a stable but infrequently updated release cadence.
Warnings
- gotcha The library has not seen a new release since October 2020. While it may be stable, users should be aware that active feature development or rapid bug fixes are unlikely.
- gotcha When a quaternion represents a null rotation (angle is 0), its rotation axis is geometrically undefined. `pyquaternion` defaults this axis to `[0, 0, 0]`, which might be unexpected or cause issues in downstream calculations if not handled explicitly. The `get_axis(undefined=[...])` method allows specifying a custom default.
- gotcha Some methods, such as `angle`, `integrate`, and `normalised`, implicitly normalize the Quaternion object to a unit quaternion if it is not already one. While often desirable for rotations, this automatic normalization might be an unexpected side-effect if you are working with non-unit quaternions and expect their norm to be preserved.
- gotcha An older GitHub issue (#21, dating from 2018/2021) indicated potential sign-flipping issues with the i, j, k components during matrix-to-quaternion conversions. While there was a response from the maintainer, the issue's long-standing open status might suggest it was not fully resolved in the latest release or required specific workarounds.
Install
-
pip install pyquaternion
Imports
- Quaternion
from pyquaternion import Quaternion
Quickstart
import pyquaternion
import numpy as np
# Create a quaternion representing a rotation of +90 degrees about the positive y-axis
my_quaternion = pyquaternion.Quaternion(axis=[0, 1, 0], degrees=90)
# Define a vector to rotate
my_vector = np.array([0., 0., 4.])
# Rotate the vector
my_rotated_vector = my_quaternion.rotate(my_vector)
print(f"Original Vector: {my_vector}")
print(f"Performing rotation of {my_quaternion.degrees:.1f} degrees about {my_quaternion.axis}")
print(f"Rotated Vector: {my_rotated_vector}")
# Example of interpolation
null_quaternion = pyquaternion.Quaternion(axis=[0, 1, 0], angle=0)
print("\nInterpolated Rotations:")
for i, q in enumerate(pyquaternion.Quaternion.intermediates(null_quaternion, my_quaternion, 3, include_endpoints=True)):
interpolated_vector = q.rotate(my_vector)
print(f" Step {i}: rotated by {q.degrees:.1f} deg about {q.axis}, resulting in {interpolated_vector}")