PyBullet

3.2.7 · active · verified Thu Apr 16

PyBullet is the official Python interface for the Bullet Physics SDK, offering real-time collision detection and multi-physics simulation. It is widely used in robotics simulation, reinforcement learning, and virtual reality research and applications. PyBullet allows loading articulated bodies from various file formats like URDF, SDF, and MJCF, and provides functionalities for forward dynamics, inverse dynamics, forward and inverse kinematics, and collision detection. The library is actively maintained with frequent updates to its underlying C++ Bullet Physics engine.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes a PyBullet simulation in graphical mode, sets gravity, loads a ground plane and a simple robot model (R2D2), then runs the simulation for a short duration before disconnecting. It demonstrates the basic lifecycle of a PyBullet application.

import pybullet as p
import pybullet_data
import time

# Connect to the physics engine in GUI mode
# Use p.DIRECT for non-graphical (headless) mode
physicsClient = p.connect(p.GUI)

# Set additional search path for URDF files (e.g., plane.urdf, r2d2.urdf)
p.setAdditionalSearchPath(pybullet_data.getDataPath())

# Set gravity
p.setGravity(0, 0, -10)

# Load a plane
planeId = p.loadURDF("plane.urdf")

# Load a simple robot (e.g., R2D2)
robotStartPos = [0, 0, 1]
robotStartOrientation = p.getQuaternionFromEuler([0, 0, 0])
robotId = p.loadURDF("r2d2.urdf", robotStartPos, robotStartOrientation)

# Run the simulation for a few steps
print("Simulating...")
for i in range(1000):
    p.stepSimulation()
    time.sleep(1./240.) # PyBullet's default timestep is 1/240 seconds

# Disconnect from the simulator
p.disconnect()
print("Simulation finished.")

view raw JSON →