Layout across builds
This script lays out 10 dental arches on as many Form 4 builds as they need, oldest first, and saves each build as a .form file.
View on GitHub Download layout-across-builds.zip
For each build, the script:
- Opens
arches.formwithopen_form_fileand deletes the arches earlier builds took withdelete_models. - Calls
auto_layout_modelswithlock_rotationand a fixedseed. - If the arches don't all fit, deletes the newest and tries again.
- Saves the build with
save_form_file.
Layout modes shows each auto_layout_models option on its own. Pack across builds does the same for SLS builds, where auto_pack_models sets aside the parts that don't fit.

case-2101 to case-2106
case-2107 to case-2110This example contains two files: layout_across_builds.py lays out the builds, and arches.form holds 10 dental arches on a Form 4, named case-2101 to case-2110 in the order they arrived.
Connect to PreForm#
Make sure PreForm is ready to accept connections.
main connects to PreForm at PREFORM_URL:
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 arches.form and deletes the arches earlier builds took:
# 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:
# Parts are named in the order they arrived, so the last one is the newest.
models = sorted(preform.list_models(scene_id=scene_id).models, key=lambda m: m.name)
taken = [m.id for m in models if m.name in placed]
if taken:
preform.delete_models(scene_id=scene_id, model_ids=taken)
models = [m for m in models if m.name not in placed]
if not models:
breakThe script stops when none remain.
Eject the newest arch#
When the arches don't all fit, auto_layout_models fails with OPERATION_FAILED and leaves the scene unchanged. The script then deletes the newest arch and tries again:
while True:
try:
# The arches all face the same way, so they lay out without turning.
preform.auto_layout_models(
scene_id=scene_id, model_ids=[m.id for m in models], lock_rotation=True, seed=42
)
break
except PreFormApiError as exc:
# OPERATION_FAILED here means the parts don't all fit together.
if exc.code != "OPERATION_FAILED" or len(models) == 1:
raise
# Eject the newest part, so the oldest parts print first.
preform.delete_models(scene_id=scene_id, model_ids=[models.pop().id])The deleted arch waits for a later build, so the oldest arches print first. on_layout_failure="EJECT_A_MODEL_AND_RETRY" has PreForm eject models itself, picked at random from the seed.
Save the build#
save_form_file writes each build to its own .form file:
build += 1
out = HERE / f"arches.build-{build}.form"
preform.save_form_file(file=str(out.resolve()), scene_ids=[scene_id])
placed |= {m.name for m in models}
print(f"{out.name}: {', '.join(m.name for m in models)}")
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 first; with uv, uv run installs it from the example's pyproject.toml.
python layout_across_builds.pyuv run layout_across_builds.pyThe script writes arches.build-1.form, arches.build-2.form, and so on next to arches.form. To lay out your own parts, change FORM_FILE.
arches.build-1.form: case-2101, case-2102, case-2103, case-2104, case-2105, case-2106
arches.build-2.form: case-2107, case-2108, case-2109, case-2110