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, includes
var = value;Assignment
var = cond ? a : b;Ternary
var = function (x) x + x;Function literal
module name(...) { ... }Define a module
function name(...) = ...;Define a function
include <file.scad>Inline a file
use <file.scad>Import modules/functions only

Constants

Built-in values
undefThe undefined value
PI≈ 3.14159265358979
true / falseBooleans

Operators

Arithmetic, comparison, logical
OperatorMeaningOperatorMeaning
n + mAdditionn < mLess than
n - mSubtractionn <= mLess or equal
n * mMultiplicationa == bEqual
n / mDivisiona != bNot equal
n % mModulon >= mGreater or equal
n ^ mExponentiationn > mGreater than
a && bLogical ANDa || bLogical OR
!aLogical NOTv[i]Vector index

Special variables

Dollar-prefixed runtime knobs
VariableWhat it controls
$fnNumber of facets a circle/sphere is approximated by. Overrides $fa / $fs.
$faMinimum facet angle (default 12°). Bigger ⇒ coarser, faster.
$fsMinimum facet size (default 2 mm). Smaller ⇒ smoother on small parts.
$tAnimation time, 0.0 → 1.0.
$vpr / $vpt / $vpd / $vpfViewport rotation / translation / camera distance / FOV.
$childrenNumber of children passed into the current module.
$previewtrue 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
CallDescription
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
CallDescription
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
CallDescription
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 shapes
union() { ... }Merge children (default)
difference() { A; B; C; }A minus B minus C
intersection() { ... }Keep only shared volume

Lists & vectors

Indexing, slicing, dot-notation
list = [a, b, c];Create a list
v = list[2];Index (0-based)
v = list.z;Dot notation (x / y / z)
len(list)Length
concat(a, b)Concatenate
[for (i = range) expr]List comprehension

List 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 for

Flow control

for, if, let, intersection_for
for (i = [start : end]) { ... }Range
for (i = [start : step : end]) { ... }Range with step
for (i = [a, b, c]) { ... }List
for (i = ..., j = ...) { ... }Nested
intersection_for(i = ...) { ... }Intersect iterations
if (cond) { ... } else { ... }Conditional
let (x = expr) { ... }Local binding

Type tests

Predicates for runtime types
is_undef(x)True iff x is undef
is_bool(x)True iff boolean
is_num(x)True iff number
is_string(x)True iff string
is_list(x)True iff list/vector
is_function(x)True iff function literal

Other built-ins

Strings, search, version, debug
echo(...)Print to console
assert(cond, msg)Halt on failure
render(convexity)Force CGAL render of subtree
children([idx])Emit module children
concat(a, b, c)Concatenate lists/strings
lookup(key, table)Interpolated table lookup
str(a, b, ...)Build a string
chr(n)Code point → string
ord(s)String → first code point
search(p, s)Substring / list search
version()OpenSCAD version vector
parent_module(idx)Caller stack inspection

Mathematical functions

Trig + scalar + vector math
TrigonometryNumbersLists / 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.

Open the agent