A C++/Python black hole renderer using pybind11.
It builds a Python extension module (bhsim) from C++ code and renders a black-hole + accretion-disk frame from Python.
Blackhole Simulator is a black hole visualization project focused on physically inspired rendering with a fast C++ core and a simple Python interface.
It traces light paths around a compact mass, samples emission from an accretion disk, and produces cinematic frames that show gravitational lensing, shadow formation, and relativistic intensity shifts.
The renderer is implemented in C++ for performance, exposed to Python through pybind11, and designed for quick experimentation with camera and lensing parameters. This makes it suitable for both educational demos and visual prototyping of black hole imagery.
- Physically inspired black hole rendering with curved light paths and disk sampling
- Pybind11-based C++ extension for fast frame generation from Python
- Interactive camera controls in the demo:
Pitchslider to tilt view angleDistanceslider to zoom in/out
- Configurable lensing and field-of-view parameters
- High-resolution render support (e.g. 1600x900 sample in
assets/)
src/raytrace.cpp: core renderer logic (C++)src/raytrace.hpp: renderer function declarationsrc/bindings.cpp:pybind11Python bindingspython/demo.py: example Python script to render and display a frameCMakeLists.txt: build configuration
- C++17 compiler
- CMake >= 3.20
- Python (recommended: one virtualenv/conda env)
pybind11available to CMake- Python packages:
numpymatplotlib
From repo root:
cmake -S . -B build \
-DPython_EXECUTABLE=$(which python)
cmake --build build -jNotes:
- Always configure and run with the same Python interpreter to avoid ABI mismatch.
- The extension is written into
build/(e.g.bhsim.cpython-312-darwin.so).
PYTHONPATH=build python python/demo.pyThe demo opens a matplotlib window with the rendered frame. Use sliders in the demo window to adjust:
Pitch: vertical camera angleDistance: camera radius scale (zoom)
Current demo defaults (python/demo.py):
- Render size:
400 x 240 fov_deg=50.0bh_angular_radius_deg=4.8lens_strength=0.55- Slider steps:
Distance:0.1Pitch:5.0
An additional GPU-oriented implementation is available:
python/gpu_renderer.py: tensor-based renderer (CUDA/MPS/CPU fallback)python/gpu_demo.py: demo script for GPU path
Install PyTorch in your environment, then run:
python python/gpu_demo.pyNotes:
- Uses
cudawhen available, otherwisempson Apple Silicon, otherwise CPU. - This path is separate from the C++
pybind11extension.
import bhsim
img = bhsim.render_sky_with_bh(
width=400,
height=240,
fov_deg=50.0,
bh_angular_radius_deg=4.8,
lens_strength=0.55,
sky_img=dummy_numpy_uint8_image,
cam_yaw_deg=0.0,
cam_pitch_deg=6.0,
cam_distance_scale=1.0
)Return value:
numpy.ndarrayof shape(height, width, 4)in RGBA (uint8).
Reconfigure with the same Python you use to run:
cmake -S . -B build -DPython_EXECUTABLE=$(which python)
cmake --build build -jEnsure module path is visible:
PYTHONPATH=build python python/demo.pyInstall in your active environment:
python -m pip install numpy matplotlibThis project is licensed under the MIT License. See LICENSE.

