Auto-Layout & Automatic Spacers¶
Designing a board game insert requires arranging multiple boxes into the outer game box without collisions, while preventing trays from sliding around when the game box is stored vertically or transported.
pyboxbuilder provides two complementary layout workflows:
Declarative Structural Layout (
columns(),rows(),stack()): State how boxes are arranged relative to each other, and let the library calculate their physical 3D positions.Automatic 3D Bin Packing: Let the guillotine 3D packing engine arrange boxes within the container automatically.
Automatic Spacer Generation (
generate_spacers=True): Automatically detect leftover voids inside the game box and generate printable filler trays to lock the insert securely in place.
On this page
Declarative Arrangement¶
In real board game inserts, trays are tightly structured: player boxes stack in one column, card wells line up in a row, and resource trays fill another column. Specifying absolute XYZ coordinates manually is brittle because resizing any single tray forces recalculating every other box’s coordinates.
The pyboxbuilder.layout module solves this by letting you describe the arrangement hierarchically:
columns(): Arranges children side-by-side along the X-axis (width).rows(): Arranges children front-to-back along the Y-axis (length).stack(): Arranges children bottom-to-top along the Z-axis (height).polygon_grid(): Tessellates regular polygonal trays (such as hexagonalBoxType.POLYGONplayer pods or triangular trays) in a staggered honeycomb or alternating lattice.
Groups can be nested arbitrarily, and you can specify a gap (in mm) between adjacent boxes to ensure clearance for easy removal.
project = Project("ArrangeDemo", game_box_size=(130.0, 90.0, 30.0), generate_spacers=False)
# Define the boxes by size without specifying (x, y, z) positions:
project.box(BoxType.CAP, "P1", size=(40.0, 40.0, 20.0), lid=LidBuilder(text="P1"))
project.box(BoxType.CAP, "P2", size=(40.0, 40.0, 20.0), lid=LidBuilder(text="P2"))
project.box(BoxType.CAP, "Cards", size=(40.0, 80.0, 20.0), lid=LidBuilder(text="CARDS"))
project.box(BoxType.SLIDING, "Tokens", size=(40.0, 80.0, 20.0), lid=LidBuilder(text="TOKENS"))
# Arrange in 3 columns:
# Col 1: Two player boxes along Y
# Col 2: Card box
# Col 3: Sliding token box
project.arrange(
columns(
rows("P1", "P2"),
"Cards",
"Tokens",
gap=2.0,
)
)
project.show(show_lids=True)
Automatic 3D Bin Packing¶
If you do not call arrange() and omit explicit position=(x, y, z) coordinates, pyboxbuilder automatically runs its 3D guillotine bin packer (pyboxbuilder.packing.guillotine).
Multi-Choice Co-Sizing: Unsized multi-compartment boxes generate a Pareto menu of candidate
(W, L, H)envelopes across valid compartment arrangements, letting the 3D solver select the aspect ratio that minimizes wasted space inside the game box.Mixed Pinned & Auto-Packed Layouts: Any boxes given explicit
position=(x, y, z)coordinates act as 3D obstacles; remaining unpositioned boxes automatically pack into the residual free space without overlapping pinned trays.Physical Protrusions & Stack Chains: Exterior handles (such as
BoxType.SLEEVE_DRAWERpulls orBoxType.DISPENSERtrays) and orderedstack_order()chains are accounted for automatically during packing, post-pack expansion, and boss/recess generation.2D Element Auto-Packing: Inside a custom piece tray,
pack_elements()packs looseCompartmentElementpockets into a compact 2D skyline footprint automatically.
The packer optimizes the placement of all boxes to fit inside the declared game_box_size. If the boxes cannot fit, it reports a PackingError identifying the overflow.
Automatic Spacer Generation¶
Commercial game boxes rarely match the exact sum of your components’ dimensions. When an insert leaves unfilled space along a side or corner, components will shift, slide, and open during shelf storage or travel.
When generate_spacers=True is passed to Project (enabled by default), pyboxbuilder:
Computes the geometric difference between the outer
game_box_sizeand the placed boxes.Extracts maximal axis-unbiased 3D empty cuboids, filters out unsupported mid-air voids, and merges adjacent co-planar voids into rectilinear L-, T-, or U-shaped spacer trays (
SpacerSpec), automatically splitting ring-shaped voids around centered trays so no spacer ever collides with an interior box.Generates hollow organizer trays or solid blocks for each void, sizing them to fill the gap while leaving slight clearance for easy insertion.
Includes the spacer trays in 3D previews (
show()), exports (export()), and blueprint layout PDFs.
Here is an example with two trays in a game box; pyboxbuilder automatically calculates and renders the spacer trays filling the vacant space:
project = Project(
"SpacerDemo",
game_box_size=(90.0, 90.0, 25.0),
board_thickness=5.0,
generate_spacers=True,
)
# Place two trays leaving an L-shaped void along the back and right:
project.box(
BoxType.CAP,
"MainTray",
size=(50.0, 50.0, 20.0),
position=(2.0, 2.0, 0.0),
lid=LidBuilder(text="MAIN"),
)
project.box(
BoxType.CAP,
"SideTray",
size=(32.0, 50.0, 20.0),
position=(54.0, 2.0, 0.0),
lid=LidBuilder(text="SIDE"),
)
# project.show() renders both boxes AND the auto-generated spacer pieces!
project.show(show_lids=True)
Note
Use board_thickness to exclude board/rulebook storage height from auto-packing and spacer generation. By default this reserves space at the top of the box; set board_on_bottom=True when the game boards sit at the bottom (z = 0..board_thickness) underneath the trays.
Inspecting Spacer Pieces in Python¶
You can also inspect the generated spacer pieces programmatically via build():
build = project.build()
for piece in build.pieces:
if piece.kind == "spacer":
print(f"Spacer {piece.label}: size={piece.size} at {piece.position}")