Rigid Body Dynamics Library
Python API example

Here is a simple example of the Python API that showcases a subset of the wrapped functions and how to access them from python:

import numpy as np
import rbdl
# Create a new model
model = rbdl.Model()
# Create a joint from joint type
joint_rot_y = rbdl.Joint.fromJointType ("JointTypeRevoluteY")
# Create a body for given mass, center of mass, and inertia at
# the CoM
body = rbdl.Body.fromMassComInertia (
1.,
np.array([0., 0.5, 0.]),
np.eye(3) * 0.05)
xtrans= rbdl.SpatialTransform()
xtrans.r = np.array([0., 1., 0.])
# You can print all types
print (joint_rot_y)
print (model)
print (body)
print (body.mInertia)
print (xtrans)
# Construct the model
body_1 = model.AppendBody (rbdl.SpatialTransform(), joint_rot_y, body)
body_2 = model.AppendBody (xtrans, joint_rot_y, body)
body_3 = model.AppendBody (xtrans, joint_rot_y, body)
# Create numpy arrays for the state
q = np.zeros (model.q_size)
qdot = np.zeros (model.qdot_size)
qddot = np.zeros (model.qdot_size)
tau = np.zeros (model.qdot_size)
# Modify the state
q[0] = 1.3
q[1] = -0.5
q[2] = 3.2
# Transform coordinates from local to global coordinates
point_local = np.array([1., 2., 3.])
point_base = rbdl.CalcBodyToBaseCoordinates (model, q, body_3, point_local)
point_local_2 = rbdl.CalcBaseToBodyCoordinates (model, q, body_3, point_base)
# Perform forward dynamics and print the result
rbdl.ForwardDynamics (model, q, qdot, tau, qddot)
print ("qddot = " + str(qddot.transpose()))
# Compute and print the jacobian (note: the output parameter
# of the Jacobian G must have proper size!)
G = np.zeros([3,model.qdot_size])
rbdl.CalcPointJacobian (model, q, body_3, point_local, G)
print ("G = \n" + str(G))

To build the wrapper one needs both Cython and the development libraries for NumPy installed on the system. If this is the case you can build the wrapper by enabling the CMake option RBDL_BUILD_PYTHON_WRAPPER when configuring RBDL. You can find the wrapper in the subdirectory python/ of your build directory. By running the python interpreter in this directory you can load it within the python shell using

Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rbdl

To install the wrapper you can use the python/setup.py script:

sudo ./setup.py install

This installs the RBDL python module globally and allows you to import it from any python script.