Skip to main content

Basic utilities for PyBullet, including collision detection, ghost (i.e. visual-only) objects, and cameras.

Project description

pyb_utils: utilities for PyBullet

This is a collection of utilities I've found useful for working with PyBullet, including:

  • Collision detection: conveniently set up shortest distance computations and collision checking between arbitrary objects in arbitrary configurations with PyBullet. See the accompanying blog post.
  • Ghost objects: add purely visual objects to the simulation, optionally attached to another body.
  • Camera: virtual camera from which to get RGBA, depth, segmentation, and point cloud data. Also provides video recording using OpenCV.
  • Convenience class for easily creating rigid bodies.
  • Versions of some PyBullet functions that return named tuples, for easy field access.
  • Basic quaternion functions.

Install and run

This package requires Python 3.7+. It has been tested on Ubuntu 16.04, 18.04, and 20.04.

From pip

pip install pyb_utils

From source

Clone the repo:

git clone https://github.com/adamheins/pyb_utils
cd pyb_utils

Install using poetry:

poetry install
poetry run python examples/collision_detection_example.py  # for example

Or using pip:

python -m pip install .

Usage and examples

This package provides a few basic quality of life utilities. First, PyBullet represents rotations using quaternions (in [x, y, z, w] order). We provide a few helper routines to convert to rotation matrices and rotate points (using spatialmath under the hood):

>>> import pyb_utils
>>> q = (0, 0, np.sqrt(2) / 2, np.sqrt(2) / 2)  # 90 deg rotation about z-axis

>>> pyb_utils.quaternion_to_matrix(q)  # convert to rotation matrix
array([[ 1.,  0.,  0.],
       [ 0., -0., -1.],
       [ 0.,  1., -0.]])

>>> pyb_utils.quaternion_multiply(q, q)  # rotate two quaternions together
array([0, 0, -1, 0])                     # 180 deg rotate about z

>>> pyb_utils.quaternion_rotate(q, [1, 0, 0])  # rotate a point
array([0, 1, 0])

Second, we provide a simple class to quickly create rigid bodies programmatically, which is useful for adding basic objects to manipulate or act as obstacles:

>>> import pybullet as pyb
>>> import pyb_utils

>>> pyb.connect(pyb.GUI)

# create a 1x1x1 cube at the origin
>>> box = pyb_utils.BulletBody.box(position=[0, 0, 0], half_extents=[0.5, 0.5, 0.5])

# put a ball on top
>>> ball = pyb_utils.BulletBody.sphere(position=[0, 0, 1.5], radius=0.5)

# now put it somewhere else
>>> ball.set_pose(position=[2, 0, 0.5])

Third, we wrap some PyBullet functions to return named tuples, rather than normal tuples. When the tuples have 10+ fields in them, it is rather helpful to have names! The names and parameters of these functions are exactly the same as the underlying PyBullet ones, to make swapping effortless. Continuing our previous example:

# built-in PyBullet method
# the output is not easy to read!
>>> pyb.getDynamicsInfo(box.uid, -1)
(1.0,
 0.5,
 (0.16666666666666666, 0.16666666666666666, 0.16666666666666666),
 (0.0, 0.0, 0.0),
 (0.0, 0.0, 0.0, 1.0),
 0.0,
 0.0,
 0.0,
 -1.0,
 -1.0,
 2,
 0.001)

# switch to the pyb_utils version
# now we can access fields by name
>>> info = pyb_utils.getDynamicsInfo(box.uid, -1)
>>> info.mass
1.0
>>> info.localInertiaPos
(0.0, 0.0, 0.0)

And there's more! You can find example scripts of all of this package's utilities in the examples/ directory:

Known issues

Feel free to open issues (or better yet, a pull request!) if you find a problem. Currently known issues:

  • Video recording does not output MP4 videos correctly. The AVI format works, however.
  • Ghost objects sometimes flicker (spooky, but undesirable).

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyb_utils-0.3.1.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyb_utils-0.3.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file pyb_utils-0.3.1.tar.gz.

File metadata

  • Download URL: pyb_utils-0.3.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.8.10 Linux/5.15.79-rt54

File hashes

Hashes for pyb_utils-0.3.1.tar.gz
Algorithm Hash digest
SHA256 561b22ff10a3bdeb6d38abc08151ed69b4b1f1cd1ce958a2e78427a8242d2c4a
MD5 3340ba21737eb663c33285f1dcf2e14a
BLAKE2b-256 d8eed1b363ac52f195e1de5fa1cd6734aa6bd91ba8c8915e47db1b4bc1f6ba71

See more details on using hashes here.

File details

Details for the file pyb_utils-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pyb_utils-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.8.10 Linux/5.15.79-rt54

File hashes

Hashes for pyb_utils-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a705c6b54635d97c440ca731bf50ad9c31ba780d70445e1ad2a93504a0bb2d27
MD5 2442ede55d9d4ee4af18a147a4edbac4
BLAKE2b-256 e003d3be580b30883efe3c30d3904fca92e664599a64d9fe5fc35b2bb38c0b0e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page