Variables and comments
Assign values with =. Variables are constants within a scope: reassigning later in the same scope replaces the value everywhere in that scope.
Open this example in playgroundFind OpenSCAD syntax, copy a complete snippet, and open it in the browser playground to preview it in 3D. For the complete language, see the official OpenSCAD cheat sheet at openscad.org/cheatsheet.
Variables, comments, output, and resolution.
Assign values with =. Variables are constants within a scope: reassigning later in the same scope replaces the value everywhere in that scope.
Open this example in playgroundecho() writes to the console. Use it to inspect computed values; vectors support .x, .y, and .z accessors.
Open this example in playground$fn sets the number of fragments for circles, spheres, and cylinders. Override it per call when one part needs more or less detail.
Open this example in playgroundBOSL2 must be included (not used) because it defines constants. Prefer use <file> for your own module libraries so their top-level geometry is not drawn.
Library files must be in the project or on the OpenSCAD library path. In the playground, enable BOSL2 under Files.
Open this example in playgroundA box from the origin, or centered on the origin with center = true. A single number makes a cube.
Open this example in playgroundA sphere centered on the origin. Use r for radius or d for diameter.
Open this example in playgroundDifferent d1/d2 (or r1/r2) make cones; a low $fn makes polygonal prisms such as hexagons.
Open this example in playgroundA custom solid from points and faces. List each face's points clockwise when viewed from outside the solid.
Open this example in playgroundPreview in 2D mode, export SVG/DXF, or extrude into 3D.
Planar shapes on the XY plane. square() accepts center = true, just like cube().
Open this example in playgroundA closed outline from 2D points. Add a paths argument to cut holes.
Open this example in playgroundText as a 2D shape. Extrude it to emboss or engrave. Available fonts depend on the installation.
Open this example in playgroundoffset(r) rounds corners while growing or shrinking; offset(delta) keeps sharp corners. Negative values shrink.
Open this example in playgroundMove children by an [x, y, z] vector.
Open this example in playgroundRotate in degrees about X, then Y, then Z; or by angle a around axis v.
Open this example in playgroundscale() multiplies each axis; resize() sets absolute final dimensions.
Open this example in playgroundMirror children across the plane through the origin whose normal is the given vector.
Open this example in playgroundColor by name or [r, g, b, alpha]. Colors show in desktop preview; STL export does not store colors.
Open this example in playgroundTransforms apply right to left: the one closest to the object runs first.
Open this example in playgroundCombine children into one solid. Top-level objects are implicitly unioned.
Open this example in playgroundSubtract every later child from the first child.
Make cutters extend past the surfaces they cut (here, -1 and +2 height) to avoid zero-thickness skins.
Open this example in playgroundKeep only the volume shared by all children.
Open this example in playgroundThe convex hull of the children; a quick way to make rounded plates and smooth transitions.
Open this example in playgroundThe Minkowski sum rounds every edge. The result grows by the rounding radius on every side.
minkowski() can be slow on complex shapes; prefer hull() or offset() when they are sufficient.
Open this example in playgroundExtrude a 2D shape upward along Z. twist and scale create twisted or tapered solids.
Open this example in playgroundRevolve a 2D profile on the positive X side around the Z axis. Use angle = ... for a partial revolution.
Open this example in playgroundExtrude text and place it on a surface. Use difference() instead to engrave.
Open this example in playgroundModules are reusable geometry with default arguments. Call them like built-ins, with positional or named arguments.
Open this example in playgroundchildren() renders the geometry passed to a module, so one module can place or transform any shape.
Open this example in playgroundFunctions return values, not geometry. They are single expressions.
Open this example in playgroundUse the conditional operator (?:) for loops in functions.
Open this example in playgroundRanges are [start : end] or [start : step : end], inclusive of end.
Open this example in playgroundList several variables in one for() to loop over every combination.
Open this example in playgroundRotate copies around Z to make bolt circles, spokes, and gears.
Open this example in playgroundInclude geometry conditionally. The condition must be known when the model is evaluated.
Open this example in playgroundBuild lists with [for (...) expr]; let() defines local variables inside expressions.
Open this example in playgroundIntersect every loop iteration instead of unioning them.
Open this example in playground# draws a subtree in translucent red while still using it; % shows it transparent and excludes it from the result.
Modifier colors appear in desktop Preview. The browser playground STL viewport does not render them.
Open this example in playground! renders only that subtree; * disables it completely.
Open this example in playgroundStop with an error message when an assumption is false.
Open this example in playgroundTop-level variables with special comments become form controls.
// [min:step:max] makes a slider, // [a, b] a dropdown, and booleans a checkbox. /* [Tab] */ groups parameters; [Hidden] hides them.
Open this example in playgroundBring external geometry into a model. Paths are relative to the .scad file. surface(file = "terrain.png") similarly turns a heightmap into a solid.
Add the imported file to the project first; in the playground use Files → Import.
Flatten 3D geometry onto the XY plane, or slice it at z = 0 with cut = true. Useful for laser-cut outlines.
Open this example in playground