Quick reference
OpenSCAD cheat sheet
Every OpenSCAD primitive, transformation, boolean operation, function, operator, and special variable on a single page. Bookmark it, search it (Ctrl/⌘ + F), and paste any snippet straight into the AI CAD Modeler to see it render live.
Based on OpenSCAD v2021.01 — the version bundled with the AI CAD Modeler.
Everything below works in the browser WebAssembly build. For deeper dives, jump to the per-topic reference pages linked from the docs hub.
Syntax
Variables, ternaries, functions, modules, includesvar = value;Assignmentvar = cond ? a : b;Ternaryvar = function (x) x + x;Function literalmodule name(...) { ... }Define a modulefunction name(...) = ...;Define a functioninclude <file.scad>Inline a fileuse <file.scad>Import modules/functions onlyConstants
Built-in valuesundefThe undefined valuePI≈ 3.14159265358979true / falseBooleansOperators
Arithmetic, comparison, logical| Operator | Meaning | Operator | Meaning |
|---|---|---|---|
n + m | Addition | n < m | Less than |
n - m | Subtraction | n <= m | Less or equal |
n * m | Multiplication | a == b | Equal |
n / m | Division | a != b | Not equal |
n % m | Modulo | n >= m | Greater or equal |
n ^ m | Exponentiation | n > m | Greater than |
a && b | Logical AND | a || b | Logical OR |
!a | Logical NOT | v[i] | Vector index |
Special variables
Dollar-prefixed runtime knobs| Variable | What it controls |
|---|---|
$fn | Number of facets a circle/sphere is approximated by. Overrides $fa / $fs. |
$fa | Minimum facet angle (default 12°). Bigger ⇒ coarser, faster. |
$fs | Minimum facet size (default 2 mm). Smaller ⇒ smoother on small parts. |
$t | Animation time, 0.0 → 1.0. |
$vpr / $vpt / $vpd / $vpf | Viewport rotation / translation / camera distance / FOV. |
$children | Number of children passed into the current module. |
$preview | true during F5 preview, false during F6 render. |
Modifier characters
Prefix any statement to debug*Disable (skip this subtree)!Show only this subtree#Highlight (debug)%Background (transparent / not in final mesh)2D primitives
Shapes that live in the XY plane| Call | Description |
|---|---|
circle(r | d=...) | Circle. Use $fn to control facets. |
square(size, center=false) | Square or rectangle. size = number or [w,h]. |
polygon(points, [paths]) | Arbitrary 2D polygon from a list of points. |
text(text, size, font, halign, valign, ...) | Generate 2D text using an installed font. |
import("file.dxf|svg", convexity) | Import a 2D shape from DXF or SVG. |
projection(cut=false) | Flatten a 3D shape to 2D (top-down silhouette). |
3D primitives
Solid building blocks| Call | Description |
|---|---|
sphere(r | d=...) | Sphere centered on origin. |
cube(size, center=false) | Box. size = number or [w,d,h]. |
cylinder(h, r | d, center=false) | Right circular cylinder. |
cylinder(h, r1, r2 | d1, d2) | Cone / truncated cone. |
polyhedron(points, faces, convexity) | Arbitrary closed mesh from points + faces. |
import("file.stl|off|amf|3mf") | Import a mesh. |
linear_extrude(height, center, twist, slices, scale) | Extrude a 2D shape straight up (with optional twist). |
rotate_extrude(angle, convexity) | Sweep a 2D shape around the Z axis. |
surface(file="map.png|dat", center, convexity) | Build a heightmap from a PNG or DAT file. |
Transformations
Move, rotate, scale, mirror, color| Call | Description |
|---|---|
translate([x, y, z]) | Move by a vector. |
rotate([x, y, z]) | Rotate around X, then Y, then Z (in degrees). |
rotate(a, [x, y, z]) | Rotate a° around an arbitrary axis. |
scale([x, y, z]) | Scale per axis. Use 1.0 to leave an axis alone. |
resize([x, y, z], auto, convexity) | Resize to fit a target bounding box. |
mirror([x, y, z]) | Mirror across the plane normal to the vector. |
multmatrix(m) | Apply an arbitrary 4×4 transform matrix. |
color("name" | "#hex" | [r,g,b,a]) | Color a subtree for preview. |
offset(r | delta, chamfer) | 2D offset (round or chamfered). |
hull() | Convex hull of all children. |
minkowski(convexity) | Minkowski sum of children — great for fillets & rounding. |
Boolean operations
Combine shapesunion() { ... }Merge children (default)difference() { A; B; C; }A minus B minus Cintersection() { ... }Keep only shared volumeLists & vectors
Indexing, slicing, dot-notationlist = [a, b, c];Create a listv = list[2];Index (0-based)v = list.z;Dot notation (x / y / z)len(list)Lengthconcat(a, b)Concatenate[for (i = range) expr]List comprehensionList comprehensions
Build lists declaratively[for (i = range_or_list) expr]Generate[for (i = ...) if (cond) expr]Filter[for (i = ...) if (c) a else b]Branch[for (i = ...) let (j = ...) expr]With let[each list]Flatten[for (i = 0; i < n; i = i+1) i]C-style forFlow control
for, if, let, intersection_forfor (i = [start : end]) { ... }Rangefor (i = [start : step : end]) { ... }Range with stepfor (i = [a, b, c]) { ... }Listfor (i = ..., j = ...) { ... }Nestedintersection_for(i = ...) { ... }Intersect iterationsif (cond) { ... } else { ... }Conditionallet (x = expr) { ... }Local bindingType tests
Predicates for runtime typesis_undef(x)True iff x is undefis_bool(x)True iff booleanis_num(x)True iff numberis_string(x)True iff stringis_list(x)True iff list/vectoris_function(x)True iff function literalOther built-ins
Strings, search, version, debugecho(...)Print to consoleassert(cond, msg)Halt on failurerender(convexity)Force CGAL render of subtreechildren([idx])Emit module childrenconcat(a, b, c)Concatenate lists/stringslookup(key, table)Interpolated table lookupstr(a, b, ...)Build a stringchr(n)Code point → stringord(s)String → first code pointsearch(p, s)Substring / list searchversion()OpenSCAD version vectorparent_module(idx)Caller stack inspectionMathematical functions
Trig + scalar + vector math| Trigonometry | Numbers | Lists / vectors |
|---|---|---|
sin(a) cos(a) tan(a) |
abs(x) sign(x) |
len(v) |
asin(x) acos(x) atan(x) |
floor(x) round(x) ceil(x) |
norm(v) |
atan2(y, x) |
ln(x) log(x) exp(x) |
cross(a, b) |
pow(b, e) · sqrt(x) · min(...) · max(...) · rands(min, max, n, seed)
|
||
Trig functions take degrees, not radians.
sin(30) returns 0.5, not -0.988.
If you need radians, convert: angle_rad * 180 / PI.
Compact examples
Common shapes
Rounded plate (Minkowski)
minkowski() {
cube([40, 30, 4]);
cylinder(r=3, h=0.1);
}
Hollow tube
difference() {
cylinder(h=40, d=20, $fn=64);
translate([0,0,-1])
cylinder(h=42, d=16, $fn=64);
}
Hex grid (list comprehension)
for (x = [0:10:50], y = [0:10:50])
translate([x, y + (x/10%2?5:0), 0])
cylinder(h=3, d=8, $fn=6);
Vase via rotate_extrude
rotate_extrude($fn=120)
polygon([
[0,0], [20,0], [15,20],
[25,50], [12,60], [0,60]
]);
Want any of these as a real STL?
Paste the snippet into the AI CAD Modeler — or describe what you want and the agent will write a parametric version for you, then export it to STL, 3MF, or STEP.