---
title: "Pack across builds"
description: "Pack more parts than fit in one SLS build volume with auto_pack_models and PARK_OUTSIDE_BUILD_VOLUME, carrying the rest to the next build."
url: https://developer.formlabs.com/examples/pack-across-builds/
---

# Pack across builds

This script packs 60 3DBenchy boats and 100 cubes into as many Fuse 1 builds as they need, and saves
each build as a `.form` file.

[View on GitHub](https://github.com/Formlabs/formlabs-api-python/tree/v1.0.0-alpha.1/examples/pack-across-builds) [Download pack-across-builds.zip](https://github.com/Formlabs/formlabs-api-python/releases/download/v1.0.0-alpha.1/pack-across-builds.zip)

For each build, the script:

1. Opens `parts.form` with `open_form_file` and deletes the parts earlier builds took with
   `delete_models`.
2. Calls `auto_pack_models` with `PARK_OUTSIDE_BUILD_VOLUME`, which packs what fits and reports the
   rest.
3. Deletes the parts that didn't fit and saves the build with `save_form_file`.

[Web SLS Packer](https://developer.formlabs.com/examples/web-sls-packer.md) runs a similar loop in a browser app, on uploaded STL files.
[Layout across builds](https://developer.formlabs.com/examples/layout-across-builds.md) does the same for SLA builds, where the script picks
the part to eject.

![Boats and cubes packed through the height of a Fuse 1 build volume](https://developer.formlabs.com/examples/pack-across-builds-1.png)

*Build 1: 125 parts, 35 left over*

![The parts left over from build 1, packed in the bottom of the build volume](https://developer.formlabs.com/examples/pack-across-builds-2.png)

*Build 2: the 35 parts left over*

This example contains three files: `pack_across_builds.py` packs the builds, `parts.form` holds 60
3DBenchy boats and 100 15 mm cubes on a Fuse 1, and `ATTRIBUTION.md` credits the boat.

The #3DBenchy model, from [3DBenchy.com](https://www.3dbenchy.com/), is released under
[CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/).

## Connect to PreForm

Make sure [PreForm is ready to accept connections](https://developer.formlabs.com/getting-started/install.md#connect-to-a-running-preform).

`main` connects to PreForm at `PREFORM_URL`:

```python
with FormlabsClient() as client:
    preform = client.preform.connect(base_url=PREFORM_URL)
```

`PREFORM_URL` defaults to `http://localhost:44388`. Set `FL_LOCAL_BASE_URL` to reach PreForm on
another port or host.

## Start each build

Each build reopens `parts.form` and deletes the parts earlier builds took:

```python
# Each build starts from every part, less the ones earlier builds took.
# PreForm resolves paths from its own working directory.
scene_id = preform.open_form_file(file=str(FORM_FILE.resolve())).scenes[0].scene_id
try:
    models = preform.list_models(scene_id=scene_id).models
    taken = [m.id for m in models if m.name in placed]
    if taken:
        preform.delete_models(scene_id=scene_id, model_ids=taken)
    names = {str(m.id): m.name for m in models if m.name not in placed}
    if not names:
        break
```

The script stops when none remain.

## Pack the build

`auto_pack_models` packs every model in the scene:

```python
# PARK_OUTSIDE_BUILD_VOLUME packs what fits and moves the rest outside the
# build volume, for the next build.
packed = preform.auto_pack_models(
    scene_id=scene_id, seed=7, on_pack_failure=OnPackFailure.PARK_OUTSIDE_BUILD_VOLUME
)
if not packed.packed_model_ids:
    sys.exit(f"None of these fit in an empty build: {', '.join(sorted(names.values()))}")
if packed.unpacked_model_ids:
    preform.delete_models(scene_id=scene_id, model_ids=packed.unpacked_model_ids)
```

With `PARK_OUTSIDE_BUILD_VOLUME`, the pack lists the models it couldn't place in
`unpacked_model_ids`. The script deletes them, and they go on the next build. If nothing fits in an
empty build, the script stops.

## Save the build

`save_form_file` writes each build to its own `.form` file:

```python
    build += 1
    out = HERE / f"parts.build-{build}.form"
    preform.save_form_file(file=str(out.resolve()), scene_ids=[scene_id])
    placed |= {names[str(i)] for i in packed.packed_model_ids}
    print(f"{out.name}: {len(packed.packed_model_ids)} parts, {len(packed.unpacked_model_ids)} left over")
finally:
    preform.delete_scene(scene_id=scene_id)
```

## Run the example

In a terminal, run the script from the folder you unzipped. With pip, [install the Formlabs SDK](https://developer.formlabs.com/getting-started/install.md)
first; with [uv](https://docs.astral.sh/uv/getting-started/installation/), `uv run` installs it from the
example's `pyproject.toml`.

**pip:**

```bash
python pack_across_builds.py
```

**uv:**

```bash
uv run pack_across_builds.py
```

The script writes `parts.build-1.form`, `parts.build-2.form`, and so on next to `parts.form`. To pack
your own parts, change `FORM_FILE`.

```
parts.build-1.form: 125 parts, 35 left over
parts.build-2.form: 35 parts, 0 left over
```

## What's next

- [Web SLS Packer](https://developer.formlabs.com/examples/web-sls-packer.md)
- [Layout across builds](https://developer.formlabs.com/examples/layout-across-builds.md)
- [Pack modes](https://developer.formlabs.com/examples/pack-modes.md)
