OpenSCAD uses a functional programming language to create 3D objects via code. It is a script-based CAD tool where you define shapes using mathematical primitives and boolean operations rather than drawing by hand.
Core Primitives and Basic Shapes
Every object in OpenSCAD starts with a primitive. These are the building blocks you combine to create complex parts. All dimensions are typically in millimeters unless you change the settings, though the software treats them as generic units.
Cubes and Spheres
The command creates a rectangular prism. If you provide only one number, like cube(10); , it creates a cube with equal sides of 10 units. To center a cube on the origin, use cube([10, 10, 10], center=true); . This is helpful when you want to rotate the object around its own center rather than a corner.
The sphere(r=5); or sphere(d=10); commands create a sphere. You can specify the radius (r) or the diameter (d). Because spheres are curved, OpenSCAD approximates them using many small flat facets. You control the smoothness by setting the $fn variable. For example, $fn=50; makes the sphere look rounder, while $fn=6; turns it into a faceted polyhedron. Higher numbers increase render time and file size.
Cylinders and Polyhedrons
The cylinder(h=20, r=5); command creates a cylinder with a height (h) and radius (r). Like the cube, you can add center=true to balance it across the Z-axis. You can also use r1 and r2 to create a frustum, which is a cone with the top cut off. If r2=0, you have a perfect cone.
For irregular shapes, use polyhedron(). This requires two lists: one for the points (vertices) in 3D space and one for the faces. Each face is a list of indices referring to those points. This is the most complex primitive and is usually used for custom geometric shells or exported data.
Transformations and Positioning
Primitives alone are rarely useful. You must move, rotate, and scale them to build a part. Transformations are applied to the object that immediately follows them in the code.
Translation and Rotation
The command moves an object. For example, translate([10, 0, 0]) cube(5); moves a 5mm cube 10mm along the X-axis. It is important to remember that transformations are cumulative. If you translate an object and then rotate it, the rotation happens around the new position if the translation was inside a module, or around the origin if the translation was applied first.
The command turns an object by degrees. The values represent the angle of rotation around the X, Y, and Z axes respectively. To rotate a cylinder to lie flat on the X-axis, you would use rotate([0, 90, 0]) cylinder(h=10, r=2); . Always check your axis of rotation because a small error here can move your part completely out of view.
Scaling and Mirroring
The command changes the size of an object relative to its current size. A value of [2, 2, 2] doubles the size in all directions. Scaling is often used to stretch a sphere into an oval or a cube into a thin sheet. Be careful when scaling objects that have specific $fn values, as the facets may become stretched and visible.
The command flips an object across a specific plane. Using mirror([1, 0, 0]) flips the object across the YZ plane. This is the fastest way to create a symmetrical left-hand and right-hand version of a part without rewriting the coordinates.
Boolean Operations for Combining Shapes
The power of OpenSCAD comes from Constructive Solid Geometry (CSG). This means you create complex shapes by adding or subtracting simple ones.
Union and Difference
The union() operation merges multiple objects into one single piece of plastic or metal. While objects that touch are automatically unioned during the final render, using the explicit union() command helps organize your code and is necessary when applying a transformation to a group of objects.
The difference() operation subtracts the second (and any subsequent) object from the first object. The first object listed is the base, and everything following it is the “hole” being cut out. For example, to make a bead, you would put a sphere first and a cylinder second inside a difference() block. To avoid “z-fighting” (shimmering surfaces in the preview), make your cutting object slightly larger than the base. Instead of a 10mm hole in a 10mm block, make the hole 10.2mm and translate it by -0.1mm to ensure a clean cut through the surface.
Intersection and Hull
The intersection() operation keeps only the volume where two or more shapes overlap. This is useful for creating complex organic curves or trimming a shape to fit exactly within a specific boundary.
The hull() operation creates a “shrink-wrap” around all objects inside it. It finds the smallest convex shape that contains all the listed primitives. If you place two small spheres 20mm apart inside a hull(), OpenSCAD creates a capsule shape. This is the primary way to create smooth transitions between different sized circles or rectangles.
Variables, Modules, and Loops
Writing raw coordinates is tedious. Using variables and modules allows you to change one number and have the entire model update automatically.
Defining Variables
Variables are defined using the equals sign: box_width = 50;. You can then use box_width throughout your script. This makes your design parametric. If you decide the box is too small, you only change the value at the top of the file rather than searching for every instance of “50” in your code. You can also perform math, such as wall_thickness = 2; hole_size = box_width - (wall_thickness * 2);.
Creating Modules
A module is like a function for geometry. It groups a set of operations under a name. For example, you can create a module screw_hole(depth) { ... }. Whenever you need a hole, you just call screw_hole(10);. Modules can take arguments, allowing you to reuse the same logic for different sizes or positions. This keeps your main code clean and readable.
Using For Loops
The for() loop is used to create repeating patterns. The syntax for (i = [0 : 5]) { translate([i*10, 0, 0]) cube(5); } creates six cubes spaced 10mm apart along the X-axis. You can use loops to create gear teeth, ventilation slats, or arrays of holes. Combining loops with rotate() allows you to place objects in a circle, which is essential for making wheels or dials.
Common Rendering Issues and Fixes
Because OpenSCAD is a mathematical engine, you may encounter visual glitches that do not always affect the final 3D print but can be confusing during design.
Handling Manifold Errors
A “non-manifold” object is a shape that cannot exist in the real world, such as a surface with zero thickness or an edge shared by three faces. This usually happens when a difference() operation leaves a paper-thin wall. To fix this, ensure your cutting objects fully penetrate the walls of the base object. If you see a warning about manifold solids, check for overlapping faces that are exactly in the same position.
Managing Render Times
The Preview (F5) is fast because it simplifies the geometry. The Render (F6) is slow because it calculates the exact mathematical intersection of every surface. If your render is taking too long, reduce the $fn value for parts that do not need to be perfectly round. For example, a large internal support might only need $fn=12, while a visible outer rim needs $fn=64. Using children() inside modules can also help the engine process the geometry more efficiently.
Related
- TK Standards Cheat Sheet: What You Need to Know
- Synonym of Cheat Sheet: Quick Reference Guide
- Technical Writing Cheat Sheet: Clear Steps
General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more
Spotted an error? Tell us and we will correct it (corrections policy).