LuaCAD

3D Primitives

-- Cube / Cuboid
cube(size)
cube { size = {x, y, z}, center = true }
cube(x, y, z)

-- Sphere
sphere(r)
sphere { r = radius }
sphere { d = diameter }

-- Cylinder / Cone
cylinder { h = height, r = radius, center = false }
cylinder { h = height, r1 = bottom_r, r2 = top_r }
cylinder { h = height, d = diameter }

-- Polyhedron
polyhedron { points = {...}, faces = {...} }

-- Pyramid (square base of `length`, apex at `height`)
pyramid(x, y, z, length, height)

-- Torus
torus { R = major_radius, r = minor_radius }
torus { major = major_radius, minor = minor_radius }
torus { R = major, r = minor, segments = 64, segments_minor = 32 }

-- Octahedron
octahedron { r = radius }

-- Icosahedron
icosahedron { r = radius }

-- Ellipsoid
ellipsoid { rx = rx, ry = ry, rz = rz }

2D Primitives

-- Circle
circle(r)
circle { r = radius }
circle { d = diameter }

-- Rectangle / Square
rect { size = {width, height}, center = false }
square { size = width, center = false }

-- Polygon
polygon { points = {{x1,y1}, {x2,y2}, ...} }

-- Text (outlines a system font into a sketch)
text("string", { size = 10, font = "Liberation Sans" })
text("string", { size = 10, halign = "center", valign = "top" })

-- Text 3D (text plus linear_extrude in one step)
text3d("string", { size = 10, depth = 5 })

-- 2D shapes are output in their own right: they export to SCAD and
-- preview flat. Mesh formats need an extrusion first.
render(square { 60, 40 } - circle { r = 8 })
render(circle { r = 10 }:linear_extrude(3))  -- solid, exports to STL

Boolean Operations

-- Union
a + b

-- Difference
a - b

-- Intersection
a * b

Transformations

-- Translate
obj:translate(x, y, z)

-- Rotate (Euler angles in degrees)
obj:rotate(x, y, z)

-- Rotate around center point
obj:rotate(cx, cy, cz, rx, ry, rz)

-- Scale
obj:scale(sx, sy, sz)

-- Resize
obj:resize(x, y, z)

-- Mirror
obj:mirror(x, y, z)

-- Transformation matrix (4x4)
obj:multmatrix(matrix)

Extrusions & Hulls

-- Extrude 2D shape into 3D
shape:linear_extrude(height)
shape:linear_extrude(height, { twist = 90, slices = 10, scale = 2 })

-- Revolve 2D shape around Z axis
shape:rotate_extrude()
shape:rotate_extrude(360, 64)  -- angle, segments

-- Convex hull
obj:hull()

-- Minkowski sum
obj:minkowski(other)

-- Offset (2D)
shape:offset(delta)
shape:offset(delta, true)        -- chamfer the corners
shape:offsetradius(radius)       -- round the corners instead
shape:offsetradius(radius, true)

Color

-- By name
obj:color("red")

-- By RGB (0-1)
obj:color(r, g, b)

-- By RGBA
obj:color(r, g, b, a)

-- Alias
obj:setcolor(...)

Material

-- Kinds: matte, plastic, metal, glass, emissive
obj:material("metal")

-- With parameter overrides
obj:material("glass", { ior = 1.5, roughness = 0.1 })
obj:material({ kind = "emissive", strength = 4 })

-- Presets with a built-in color:
-- steel, chrome, gold, copper, brass, rubber, wood, ivory
obj:material("gold")

Modifier Characters

obj:skip()         -- * disable (hide in preview)
obj:only()         -- ! show only
obj:debug()        -- # highlight
obj:transparent()  -- % transparent / background

Other Operations

-- Clone / copy
obj:clone()
obj:copy()

-- Projection (3D to 2D)
obj:projection()
obj:projection(true)  -- cut mode

-- Force render
obj:render_node(convexity)

Utility Functions

-- Add geometry to output
render(geometry)

-- Name an object (used as the 3MF object name)
obj:name("bracket")

-- Import a mesh (3MF, STL, OBJ, PLY, OFF, AMF) as a solid
import("model.stl", convexity)

-- Import SVG or DXF as a 2D sketch (DXF needs --via-openscad to export)
import("logo.svg")

-- Import surface heightmap (PNG or DAT)
surface{ "heightmap.png", center = true, invert = true }

-- Insert verbatim OpenSCAD code
scad("cylinder(h=10, r=5, center=true);")

-- OpenSCAD customizer variable (returns value as-is)
var("name", value)

-- Empty CAD object
cad()

-- Random numbers
rands(min, max, count)
rands(min, max, count, seed)

-- Linear interpolation lookup
lookup(key, {{k1,v1}, {k2,v2}, ...})

-- Version
version()  -- returns {major, minor, patch}

-- Print to console
print(...)

BOSL2

-- The Belfry OpenSCAD Library v2, reimplemented in LuaCAD.
-- No OpenSCAD or BOSL2 installation needed.

-- 3D shapes, with rounding, chamfering and anchor/spin/orient
bosl.cuboid { {40, 40, 40}, rounding = 2 }
bosl.cuboid { {40, 40, 40}, anchor = {0, 0, 1} }
bosl.cyl { h = 20, r = 10, chamfer = 2 }
bosl.prismoid { size1 = {40, 40}, size2 = {20, 20}, h = 30 }
bosl.regular_prism { 5, r = 10, h = 25 }

-- 2D shapes return a sketch, so extrusions apply to them
bosl.rect { {20, 10}, rounding = 2 }
bosl.star { n = 5, r = 10, ir = 5 }:linear_extrude(3)

-- Parts libraries
bosl.spur_gear { circ_pitch = 5, teeth = 20, thickness = 5 }
bosl.screw { "M6x1", length = 20 }

-- Transforms take the object as `p`
bosl.zrot { 45, p = cube(4) }

-- The pure functions return real Lua values
bosl.lerp(0, 10, 0.5)
bosl.unit({3, 4, 0})

Vector Operations

-- Create vector
local v = vector(x, y, z)
local v = vec(x, y)

-- Arithmetic
v1 + v2          -- addition
v1 - v2          -- subtraction
5 * v  or  v * 5 -- scalar multiplication
-v               -- negation

-- Methods
v:getx()  v:gety()  v:getz()
v:len()     -- length / magnitude
v:unit()    -- unit vector
v:normal()  -- 2D normal
v:scalar(v2)  -- dot product
v:cross(v2)   -- cross product
v:rot(angle)  -- rotate vector

-- Global functions
cross(v1, v2)
norm(v)

Math Functions

-- Available as bare globals (degrees for trig):
sin(deg)  cos(deg)  tan(deg)
asin(x)   acos(x)   atan(x)   atan2(y, x)
abs(x)    floor(x)  ceil(x)   round(x)
sqrt(x)   pow(x,y)  exp(x)    log(x)  ln(x)
min(...)  max(...)  sign(x)
PI  -- 3.14159...

-- Type checking
is_bool(v)   is_num(v)    is_str(v)
is_table(v)  is_list(v)   is_func(v)

-- List operations
concat(a, b)  -- concatenate two tables

Settings

settings.fa       -- minimum angle (default 12)
settings.fs       -- minimum size (default 2)
settings.fn       -- number of fragments (default 0)
settings.t        -- animation step (default 0)
settings.vpr      -- viewport rotation
settings.vpt      -- viewport translation
settings.vpd      -- viewport distance
settings.vpf      -- viewport field of view
settings.preview  -- true in preview mode