Skip to content

Python API ​

The pipeline is an ordinary Python package, pbrgen. The app, the command line and the MCP server are thin layers over the three functions below.

In memory ​

python
from pbrgen import io, pipeline
from pbrgen.params import Params

image = io.load_image("rock.jpg")              # float32 array in [0, 1]
params = Params.from_dict({"input_kind": "photo", "normal_strength": 8.0, "resize": 1024})

maps = pipeline.run_array(params, image)       # {"height": ..., "normal": ..., "ao": ..., ...}
io.save_png("height.png", maps["height"], bits=16)

run_array returns a dictionary of float32 arrays in the range 0 to 1: grey maps are (H, W), the normal and the albedo (H, W, 3), the packed texture (H, W, 4).

From a file to files ​

python
from pbrgen import pipeline
from pbrgen.params import load_json

params = load_json("presets/photo_rock.json")
maps = pipeline.run(params, "rock.jpg")
written = pipeline.write_outputs(maps, "out/rock", params)

write_outputs writes the maps, the manifest and _params.json, following the export profile in params. It is the function behind every export, so its files are the same as the app's and the command line's.

A procedural height ​

python
params = load_json("presets/proc_bubbles.json")
params.resize = 1024
maps = pipeline.run_array(params, None)        # no input image

Presets, macros and overrides together ​

python
from pbrgen import cli

params = cli.build_params("photo_rock", {"resize": 2048}, macros={"relief_depth": 1.5, "seamless": True})

This resolves parameters exactly like pbrgen generate -p photo_rock --set resize=2048 --macro relief_depth=1.5.

Parameter metadata ​

python
from pbrgen.params import describe

info = describe()["normal_strength"]
print(info["default"], info["range"], info["help_en"], info["affects"])

Reusing work between runs ​

Pass the same dictionary as cache while the image stays the same, and the slow first stages (resize, seamless, photo to height) are reused when only later parameters change. The result is identical with or without it.

python
cache = {}
for strength in (4, 8, 12):
    params.normal_strength = strength
    maps = pipeline.run_array(params, image, cache=cache)