r/OpenPythonSCAD • • Nov 05 '25

Auto unwind transformation scopes (using "with" statements in Python)

I’m excited to share something I’ve been building!

TL;DR: By combining monads, Python context managers (with statements), and incremental transform matrices, I can now auto-unwind transforms (translate/rotate/scale) within a scoped block. This eliminates the manual, error-prone process of restoring positions in CSG-style modeling.

Problem: In OpenSCAD/PythonSCAD, many operations (like rotate_extrude()) are origin-centric. When working with solids away from the origin, I’d manually translate, operate, then “undo” transforms. This has been a tedious and brittle process.

Solution: Using a monadic abstraction with Python’s context manager, I record each transform matrix on a stack. When the with block exits, transforms automatically unwind. The system tracks incremental matrices per operation and even allows manual overrides when needed.

Challenges: Calculating correct incremental matrices wasn’t always straightforward. Some operations (like unions) don’t yield predictable transforms. I added an “escape hatch” for manual overrides.

Demo:

3 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/rebuyer10110 Nov 05 '25

BREP definitely does have a huge learning curve compared to typical CSG tools out there. For that complexity there are many benefits though.

Possibly true. I havent found it compelling enough to pay that price.

There is also algebra mode which doesn't use implicit parameters at all.

Nice. I like that more. Expression-wise, my intent with monads aligns with how build123d is using context managers.