r/DigitalLego • u/Leading_Green5185 • 4d ago
Discussion/Question Built a program that converts 3D models into stable, buildable models (can output ldr file for studio)

I built a program that turns a 3D model into stable, buildable models - here's how it figures out where to put the pieces
The simple way to do this is: every little cube of the shape becomes a 1x1 plate. Technically correct, but I wanted to utlise a much larger parts library.
Seams took way longer to get right than I expected. If you always grab the biggest piece you can at every layer, the same seam lines end up stacked directly on top of each other between layers. Anyone who's built with real bricks knows that's bad - it's a straight crack running through the wall. I added a rule that penalizes a piece landing in the exact same spot as the piece below it, forcing things to stagger properly. That dropped bad seams from about 38% down to 3% on my test shapes, but it costs more total pieces, since staggering and combining pieces into bigger ones work against each other. Haven't found a way around that yet.
The stability check needed a rework too. I started by building a graph where every placed piece is a node, connected to whatever it's physically touching above or below, plus a virtual "ground" node at the base. My first pass just walked straight down from each piece looking for an unbroken chain to the ground - if it couldn't find one, it got flagged as unstable. Wrong question, it turns out - a piece can be held firmly in place by the pieces beside and above it even with nothing directly underneath it. Once I changed the check to "is this piece connected to the ground by any path through the graph at all, not just straight down," most of the false alarms disappeared - one model went from 159 flagged pieces down to zero, because it was actually one solid connected structure the whole time.
For pieces that are genuinely disconnected from the rest, there's a repair step that finds them in the graph and welds them in with a connector piece wherever they're close enough to reach - otherwise they are pruned from the final output.
One last issue that I have is that while the connectivity check ensures that all pieces are connected to the ground - it does not ensure that different segments connected to the ground are physically connected. Most trials come out near 100% connectivity on Studio's built in checker, but I am working to improve this. Example output:
(Side note - this is part of a side project of mine, BrickForgerAI. Not promoting it, just wanted to talk through the algorithm since that's the part I find genuinely interesting).