Python API Reference
The Python prelude (Scripting/demos_py/prelude.py) provides a complete scripting API for
the physics sandbox over gRPC. It mirrors the F# scripting library and is used by all 22 Python
demo scripts.
Requirements
pip install grpcio>=1.60.0 grpcio-tools>=1.60.0 protobuf>=4.25.0
Quick Start
|
Or as a standalone script using the runner helper:
|
Session Management
Function |
Description |
|---|---|
|
Open a gRPC channel and return a |
|
Close the gRPC channel |
|
Connect, run |
Session dataclass
Field |
Type |
Description |
|---|---|---|
|
|
The underlying gRPC channel |
|
|
The gRPC service stub |
|
|
The server address |
Simulation Control
Function |
Description |
|---|---|
|
Start simulation |
|
Pause simulation |
|
Advance one step |
|
Server-side reset |
|
Set gravity vector |
|
Play for duration, then pause |
|
Full reset: pause, reset, clear IDs, add ground plane, set gravity to (0, -9.81, 0) |
Body Creation
All add_* functions return the body ID string.
Primitive Shapes
Function |
Signature |
|---|---|
|
|
|
|
|
|
|
|
|
|
Command Builders
These return pb.SimulationCommand objects for use with batch_add.
Function |
Signature |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Command Modifiers
These mutate and return the command for chaining.
Function |
Signature |
|---|---|
|
|
|
|
|
|
Kinematic Bodies
Function |
Signature |
|---|---|
|
|
Body Manipulation
Function |
Signature |
|---|---|
|
|
|
|
|
|
Forces & Impulses
Function |
Signature |
|---|---|
|
|
|
|
|
|
|
|
Steering Helpers
Function |
Signature |
|---|---|
|
|
|
|
Direction Enum
Value |
Vector |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Constraints
Function |
Signature |
|---|---|
|
|
|
|
|
|
|
|
All constraint functions return a pb.SimulationCommand for use with batch_add or _send.
Camera & View Commands
Camera Positioning
Function |
Signature |
|---|---|
|
|
|
|
|
|
Camera Tracking
Function |
Signature |
|---|---|
|
|
|
|
|
|
|
|
|
|
Camera Effects
Function |
Signature |
|---|---|
|
|
Display
Function |
Signature |
|---|---|
|
|
|
|
|
|
|
|
|
|
Queries
Raycasting
|
Overlap
|
Sweep
|
Batch Operations
Function |
Signature |
|---|---|
|
|
|
|
|
|
Example:
|
State Inspection
Function |
Signature |
|---|---|
|
|
|
|
|
|
Body Presets
Convenience functions that create common body types with realistic dimensions and masses.
Function |
Shape |
Radius/Size |
Default Mass |
|---|---|---|---|
|
Sphere |
r=0.01 |
0.005 |
|
Sphere |
r=0.11 |
6.35 |
|
Sphere |
r=0.2 |
0.1 |
|
Sphere |
r=0.5 |
200.0 |
|
Box |
0.5³ |
20.0 |
|
Box |
0.2×0.1×0.05 |
3.0 |
|
Box |
0.05³ |
0.03 |
All parameters are optional with sensible defaults.
Generators
Batch-create bodies in common arrangements. All return a list[str] of body IDs.
Function |
Signature |
Description |
|---|---|---|
|
|
Vertical stack of 1m boxes |
|
|
Pyramid of boxes |
|
|
Horizontal row of spheres |
|
|
2D grid of boxes |
|
|
Random spheres with varied size/mass |
Materials
Presets
Constant |
Friction |
Max Recovery |
Spring Freq |
Spring Damping |
|---|---|---|---|---|
|
0.4 |
8.0 |
60.0 |
0.5 |
|
2.0 |
0.5 |
30.0 |
1.0 |
|
0.01 |
2.0 |
30.0 |
1.0 |
Custom Materials
|
Colors
Palette Constants
Constant |
RGB |
|---|---|
|
(1.0, 0.2, 0.1) |
|
(0.3, 0.6, 1.0) |
|
(0.7, 0.7, 0.7) |
|
(1.0, 0.8, 0.0) |
|
(0.2, 0.8, 0.3) |
|
(0.8, 0.4, 1.0) |
|
(1.0, 0.5, 0.0) |
|
(0.0, 1.0, 1.0) |
Custom Colors
|
Utility Functions
Function |
Signature |
Description |
|---|---|---|
|
|
Create protobuf Vec3 |
|
|
Create protobuf Color |
|
|
Create material |
|
|
Auto-increment ID: |
|
|
Reset all ID counters |
|
|
Sleep in milliseconds |
|
|
Context manager that prints elapsed time |
timed example
|
Gotchas
- Static mesh bodies require explicit
with_motion_type(cmd, 2)(Static). Default is Dynamic, and mass=0 + Dynamic is rejected. - Mesh triangles should be ~2m+ per edge for reliable collision. Use heightmap grids instead of narrow strips.
- Proto naming: Use
pb.MeshShape(triangles=[pb.MeshTriangle(...)])— notpb.Triangle(that's a separate shape type). - Batch limit: Server enforces 100 commands per batch. Use
batch_addfor auto-chunking. - Stub generation: Run
Scripting/demos_py/generate_stubs.shto regenerate protobuf bindings after proto changes.
Next Steps
- Getting Started — Build and run the sandbox
- Demo Scripts — 22 physics demos in F# and Python
- Scripting Library — F# scripting API (equivalent functionality)
- MCP Tools — 59 tools for AI-assisted debugging
PhysicsSandbox