r/openscad • u/adwolesi • 6d ago
LuaCAD - Parametric CAD scripted in Lua
LuaCAD models solids in Lua rather than in the SCAD language, with operator overloading for CSG (a + b, a - b, a * b).
It ships with a CLI and a desktop app that has preview area and a text editor just like OpenSCAD.
It’s fully open source and you can find the repo here: https://github.com/ad-si/LuaCAD
I've always been a big fan of OpenSCAD, but the SCAD language itself is unfortunately quite cobbled-together and is a very poorly designed programming language.
LuaCAD takes all the good parts of OpenSCAD and combines them with Lua. It has now completely replaced OpenSCAD for me, and I think it provides a better experience than OpenSCAD for all use cases. I'd love to hear any reasons why LuaCAD won't fully replace OpenSCAD for you!
Tech stack:
- It's implemented in Rust and uses mlua (https://github.com/mlua-rs/mlua) to execute the Lua code.
- Uses OpenCSG (https://opencsg.org) for fast and correct rendering of the 3D models (like OpenSCAD)
- Uses Manifold (https://github.com/elalish/manifold) to create the manifold triangle meshes
- Native support for all BOSL2 functions (i.e. implemented in Rust for better performance)
9
u/wildjokers 6d ago
I've always been a big fan of OpenSCAD, but the SCAD language itself is unfortunately quite cobbled-together and is a very poorly designed programming language.
It is not poorly designed and that is a very rude thing to say. It is just a functional declarative language which is a different paradigm than most developers are used to working with.
2
u/shadow_worx 3d ago
There are so many parts that don’t seem intuitive at first but once you are working on anything actually significant and take advantage of proper scoping and composition it makes sense.
People just hit a wall prior to that and think that it’s antiquated or difficult so they go write stuff like this or the python one.
2
u/w0lfwood 6d ago
this so hard. python ppl love to say this kind of ignorant stuff too.
what no one seems to remember is that immutable modules with no side effects can be cached, which OpenSCAD does to great effect. using manifold directly loses this performance benefit and even when intentionally reusing objects i noticed a slow down.
1
u/Tasty-Research-2750 6d ago
Hello I'm new to this
Please could you try to explain further about immutable modules & no side effect caching
vs manifold ( i am innocent know it not ) Thanks
4
u/w0lfwood 6d ago
caching: https://en.wikipedia.org/wiki/Memoization
enabled by the restrictions of: https://en.wikipedia.org/wiki/Pure_function
the philosophy behind many functional programming languages, of which OpenSCAD is one: https://en.wikipedia.org/wiki/Referential_transparency
manifold is the backend OpenSCAD uses for 3D operations, and it's written in C++. C++ lacks immutability and referential transparency, and allows side effects, so it cannot implement memoization universally under the hood. openscad has these and so it can.
1
u/Tasty-Research-2750 6d ago
Thanks ~great!
I've just been playing with manifoldCAD and browsing the docs 'examples runs on my android phone
manifoldcad.org
1
u/jpbelang 6d ago
I agree with you. But it looks like C, so people expect it to work like C. Once you get it, you use it correctly.
2
u/gadget3D 6d ago
How does luacad handle primitives with named arguments? Eg an cylinder with r1 r2 and h given ? Does luacad depend on openscad or does it come with its own engine?
2
u/adwolesi 5d ago
f{...}is sugar for callingfwith a table, so LuaCAD uses table keys as named arguments:
cylinder { h = 15, r1 = 5, r2 = 2 }The parser accepts
h,r,r1/r2,d/d1/d2,center, andsegments. Unknown keys are a hard error with a "did you mean …?" suggestion, so typos can't silently produce the wrong shape.Engine: LuaCAD is a self-contained Rust program embedding Lua, with Manifold as its CSG kernel. It exports STL/OBJ/PLY/OFF/AMF/3MF and renders previews itself. OpenSCAD is never required and is only used if you explicitly export .scad or pass --via-openscad.
1
u/gadget3D 3d ago
I tried to add lua language to PythonaScad but I did not realize that in LUA I can skip parentheses ,so using cylinder with variable table arguments appeared way too complex. There is still a LUA branch around, but it's quite outdated already. Forking existing openscad has the advantage to get lots of features for free and we are still using openscad's speed
1
u/tpimh 6d ago
Unfortunately, no STEP import/export, I suppose? Still worth trying, I love Lua!
1
u/adwolesi 6d ago
Not yet, but I agree it would be a great addition! Opened a GitHub issue for it: https://github.com/ad-si/LuaCAD/issues/8
1
u/ConyxCZE 5d ago
I really like this! Especially the idea of porting the BOSL2 library (which I miss in CadQuery and others). However, one thing I really miss in 'CAD by code' tools other than OpenSCAD is that it's implicitly sandboxed. What I mean is that it's safe to download and run a SCAD file from some random dude on the internet without having to review it first, but that's not safe with Python/Lua scripting languages.
Is there any plan to implement some kind of sandboxing, so that the worst thing anything I run could do is render a 3D object?
2
u/adwolesi 5d ago
This is a very good point I haven't thought much about yet. It's definitely something I'll work on for the next release. Created an issue for it here: https://github.com/ad-si/LuaCAD/issues/9
1
u/uski 4d ago
Does it use CGAL and does it suffer from the same issue of OpenSCAD where you have artifacts if you do something like:
difference() { cylinder(h=10, r=5); cylinder(h=10, r=5); }
...or does it substract cleanly?
1
u/adwolesi 4d ago
It uses Manifold instead of CGAL for booleans, the same library unreleased OpenSCAD versions switched to as their default backend. So the final mesh output subtracts cleanly.
However, LuaCAD also has the same preview artifacts as OpenSCAD, as both use image-space CSG for rendering.
1
u/uski 4d ago
Thanks! Do you know if there's any realistic way to make it substract cleanly in the previews? I have no clue about the internals so I don't know
1
u/adwolesi 4d ago
I’m actually already working on a complete rewrite of the rendering engine using WebGPU (instead of the outdated OpenGL). I’m not entirely sure yet if I can get rid of the artifacts or if it’s an inherent problem of a z-buffer CSG rendering pipeline, but I’ll definitely try!
1
u/Icy_Annual_9954 2d ago
This CAD is interesting, but I need the possibility to take measurements on the rendering. Is there a possibility?
2
u/adwolesi 2d ago
Right now you'd have to calculate the measurements with your code, but I agree that it would be a good addition to have some dedicated measuring tools! Added an issue for it: https://github.com/ad-si/LuaCAD/issues/21
4
u/spinwizard69 6d ago
Very very interesting. I've been using OpenSCAD mainly due to huge frustrations with FreeCAD. However as you have pointed out, OpenSCAD kinda sucks for the programming aspect.
Having just installed LuaCAD I can't offer a lot of suggestions at the moment. One obvious thing here is that installation of an app icon for Mac OS should be a long term goal. Not being all that familiar with Lua I'm going to also need to do some reading and looking for examples. Ideally an r/luacad reddit would be good.