View all files | ||||
| A Python implementation of the Spatial Math Toolbox for MATLAB® |
Spatial mathematics capability underpins all of robotics and robotic vision where we need to describe the position, orientation or pose of objects in 2D or 3D spaces.
The package provides classes to represent pose and orientation in 3D and 2D space:
| pose | SE3 Twist3 UnitDualQuaternion | SE2 Twist2 |
| orientation | SO3 UnitQuaternion | SO2 |
More specifically:
These classes provide convenience and type safety, as well as methods and overloaded operators to support:
These are layered over a set of base functions that perform many of the same operations but represent data explicitly in terms of numpy arrays.
The class, method and functions names largely mirror those of the MATLAB toolboxes, and the semantics are quite similar.
Check out our ICRA 2021 paper on IEEE Xplore or get the PDF from Peter's website. This describes the Robotics Toolbox for Python as well Spatial Maths.
If the toolbox helped you in your research, please cite
If you are using the Toolbox in your open source code, feel free to add our badge to your readme!
Simply copy the following
Install a snapshot from PyPI
Note that if you are using ROS2, you may run into version conflicts when using rosdep, particularly concerning matplotlib. If this happens, you can enable optional version pinning with
Install the current code base from GitHub and pip install a link to that cloned copy
numpy, scipy, matplotlib, ffmpeg (if rendering animations as a movie)
These classes abstract the low-level numpy arrays into objects that obey the rules associated with the mathematical groups SO(2), SE(2), SO(3), SE(3) as well as twists and quaternions.
Using classes ensures type safety, for example it stops us mixing a 2D homogeneous transformation with a 3D rotation matrix -- both of which are 3x3 matrices. It also ensures that the internal matrix representation is always a valid member of the relevant group.
For example, to create an object representing a rotation of 0.3 radians about the x-axis is simply
while a rotation of 30 deg about the z-axis is
and the composition of these two rotations is
We can find the corresponding Euler angles (in radians)
Frequently in robotics we want a sequence, a trajectory, of rotation matrices or poses. These pose classes inherit capability from the list class
and this can be used in for loops and list comprehensions.
An alternative way of constructing this would be (R1, R2 defined above)
Many of the constructors such as .Rx, .Ry and .Rz support vectorization
which has created, in a single line, a list of rotation matrices.
Vectorization also applies to the operators, for instance
will produce a result where each element is the product of each element of the left-hand side with the right-hand side, ie. R[i] * SO3.Ry(0.5).
Similarly
will produce a result where each element is the product of the left-hand side with each element of the right-hand side , ie. SO3.Ry(0.5) * R[i] .
Finally
will produce a result where each element is the product of each element of the left-hand side with each element of the right-hand side , ie. R[i] * R[i] .
The underlying representation of these classes is a numpy matrix, but the class ensures that the structure of that matrix is valid for the particular group represented: SO(2), SE(2), SO(3), SE(3). Any operation that is not valid for the group will return a matrix rather than a pose class, for example
We can print and plot these objects as well
printline is a compact single line format for tabular listing, whereas print shows the underlying matrix and for consoles that support it, it is colorised, with rotational elements in red and translational elements in blue.
For more detail checkout the shipped Python notebooks:
You can browse it statically through the links above, or clone the toolbox and run them interactively using Jupyter or JupyterLab.
Import the low-level transform functions
We can create a 3D rotation matrix
The results are numpy arrays so to perform matrix multiplication you need to use the @ operator, for example
We also support multiple ways of passing vector information to functions that require it:
There is a single module that deals with quaternions, unit or not, and the representation is a numpy array of four elements. As above, functions can accept the numpy array, a list, dict or numpy row or column vectors.
The functions support various plotting styles
Animation is straightforward
and it can be saved to a file by
At the moment we can only save as an MP4, but the following incantation will covert that to an animated GIF for embedding in web pages
For use in a Jupyter notebook, or on Colab, you can display an animation by
The movie=True option causes tranimate to output an HTML5 fragment which is displayed inline by the HTML function.
Some functions have support for symbolic variables, for example
The resulting numpy array is an array of symbolic objects not numbers – the constants are also symbolic objects. You can read the elements of the matrix
We see that the symbolic constants are converted back to Python numeric types on read.
Similarly when we assign an element or slice of the symbolic matrix to a numeric value, they are converted to symbolic constants on the way in.
On April 28, 2026 this repository moved to the RAI-Opensource GitHub organization. If you are still pulling from the previous bdaiinstitute GitHub organization, please update your git remote:
Also update any links of https://bdaiinstitute.github.io/spatialmath-python to https://spatialmath-python.rai-inst.com/.
If you maintain downstream docs/scripts, update links from this repository to the new org.
This package was originally created by Peter Corke and Jesse Haviland and was inspired by the Spatial Math Toolbox for MATLAB. It supports the textbook Robotics, Vision & Control in Python 3e.
The package is now a collaboration with the Robotics and AI Institute.