Show HN: LuaCAD – Parametric CAD Scripted in Lua
LuaCAD: Parametric CAD Scripted in Lua
Welcome to LuaCAD, a powerful tool for creating parametric 2D and 3D models using the Lua programming language. Whether you need to export to standard 3D formats or render a quick preview, LuaCAD provides a flexible, scriptable environment for geometric design.
🛠️ Core Architecture
LuaCAD integrates Lua 5.4 within a high-performance Rust engine. It processes Constructive Solid Geometry (CSG) operations in two ways: directly via the Manifold library or by generating code for OpenSCAD.
🚀 Why LuaCAD?
While it shares a similar philosophy with OpenSCAD, LuaCAD aims to be just a clone a significant evolution.
"LuaCAD offers a more consistent, faster, and more powerful experience. Because it is easily embeddable and supports operator overloading, the syntax for CSG is far more natural."
Natural Syntax Comparison
In LuaCAD, you can use standard mathematical operators for CSG operations, such as (Union) represented as a + b.
LuaCAD Implementation:
my_cube = cube { size = { 1, 2, 3 } }
function my_sphere(radius)
return sphere({ r = radius }):translate(5, 0, 0)
end
model = my_cube + my_sphere(2)
render(model)
The OpenSCAD Equivalent:
module my_cube() {
cube(size=[1,2,3]);
}
module my_sphere(radius) {
translate([5,0,0]) sphere(r = radius);
}
union() {
my_cube();
my_sphere(2);
}
📦 Installation Guide
To get started, ensure you have a C++ compiler and CMake installed.
- Install the CLI:
cargo install luacad - Install the GUI (Studio):
cargo install luacad-studio(Includes live 3D preview)
Alternatively, build from the source:
git clone https://github.com/ad-si/LuaCAD.git
cd LuaCAD
make install
Note on Dependencies:
While C/C++ dependencies are vendored, luacad-studio requires OpenGL development headers. On Debian/Ubuntu systems, install:
libgl1-mesa-dev, libx11-dev, libxcb1-dev, libxkbcommon-dev, libxrandr-dev, and libwayland-dev.
💻 Command Line Interface (CLI)
The luacad CLI is versatile. Most commands infer the file format from the output extension, though you can override this with --format fmt. To use an external OpenSCAD binary instead of Manifold, use --via-openscad.
| Command | Purpose | Example |
|---|---|---|
convert | Transform script to mesh/SCAD | luacad convert model.lua output.stl |
watch | Auto-rebuild on file save | luacad watch model.lua output.3mf |
render | Create a PNG image | luacad render model.lua preview.png |
info | Show bounding box & triangle count | luacad info model.lua |
lint | Static analysis via selene | luacad lint model.lua |
run | Execute script for side-effects | luacad run model.lua |
🌟 Advanced Features
BOSL2 Integration
LuaCAD includes a full Lua reimplementation of the Belfry OpenSCAD Library v2. You can create complex shapes without needing OpenSCAD installed:
bosl.cuboid { {40, 40, 40}, rounding = 2 }bosl.regular_prism { 5, r = 10, h = 25 }bosl.spur_gear { circ_pitch = 5, teeth = 20, thickness = 5 }
Note: Exporting to .scad preserves the BOSL2 calls, keeping the output concise.
Text and External Imports
- Text: Use
text()to create a font outline (sketch) ortext3d()for immediate extrusion.- Example:
render(text("LuaCAD", { size = 12, halign = "center" }):linear_extrude(2))
- Example:
- Imports: The
import()function supports all writable mesh formats. SVG and DXF files are imported as 2D sketches ready for extrusion.
📤 Export Formats
| Format | Name | Description |
|---|---|---|
.scad | OpenSCAD | Native OpenSCAD script |
.3mf | 3D Manufacturing Format | Supports multiple objects via :name(...) |
.stl | Standard Triangle Language | Standard for 3D printing (flattened) |
.obj | Wavefront | General 3D object format (flattened) |
.ply | Polygon File Format | Point cloud/mesh format (flattened) |
.off | Object File Format | Geometric representation (flattened) |
.amf | Additive Manufacturing Format | XML-based 3D format (flattened) |
Pro Tip: When exporting to .3mf, every value returned by your script is treated as a distinct object, allowing slicers to move parts individually. All other formats merge everything into a single mesh.