Skip to main content
10 of 21
Length 9
Daniel M.
  • 3.9k
  • 2
  • 26
  • 39

OpenSCAD

OpenSCAD is a language used to create parametric and accurate 3d models using nothing but code. Note: I refer to CSG multiple times in this post; this stands for Constructive Solid Geometry. This means constructing a model through a combination of combining objects, subtracting objects, and intersecting objects. Factoid:

Since a .scad file is plain text, it occupies very little space, and you can use tools like diff effectively. Length 1:

Well, there isn't too much you can do with one character, but one very useful character is the debug modifier:

#

This character can be placed in front of any CSG tree to highlight it, even if it would be invisible in the final output (such as if the CSG object was subtracted from another object). I'm not allowed to write any more code to explain this, so I'll use pictures.

Without the modifier:

No modifier

With the modifier:

With modifier

(Image Source: OpenSCAD User Manual, Wikibooks, CC-BY-SA)

Length 2:

In OpenSCAD, you can create simple animations. How, you ask? This is done using a special variable for animation:

$t

This variable repeatedly loops from 0 to 1, which your code can use to change the position of an object, rotate an object, or do pretty much anything when combined with control flow structures. OpenSCAD can dump png frames at user specified intervals to create videos and/or gifs.

Adding a single line of code to a program (in this case, one of the examples, which also happens to be the OpenSCAD logo), and running the output through a gif maker can turn it into an animation like this: Rotating OpenSCAD logo Length 3

There are a wealth of libraries in OpenSCAD, and one way to access them is via the command:

use

There are actually two commands with similar functionality, but the other one has too many bytes. The main difference is that any top-level code (code outside a function/module) isn't executed with use, while it is executed for the other command. Both commands must be placed before any use of imported functions.

There are a couple of widely used libraries (Note that some of these should be added to the project with the other command instead):

  • MCAD- this contains basic utilities (more shapes, gears, some math, etc), and often is installed automatically with OpenSCAD.
  • Write.scad- Many of it's functions have been superseded by built-in functions in OpenSCAD, but it allows you to do a little more than the built-in functions can do (write curved text, for example).
  • BOLTS- This extensive library allows you to use standard hardware parts (and some non-standard hardware) in your projects.
  • The General Library of Relativity- Allows creating and positioning of objects relative to other objects.
  • String Theory- Made by the same person as Relativity, this provides various string utilities, along with regex functionality.

Length 4:

4 bytes is in that weird range where you can't really show off special variables or 1-character modifiers, but it's too short to really demonstrate the usage of a keyword.

However, it is the smallest whole program that isn't empty (or made up of only whitespace and semicolons) that compiles/runs without an error:

a=1;

This just assigns 1 to the constant a. In OpenSCAD, almost everything is a constant within its scope. Recursion, iteration, and defining separate constants are two common ways to circumvent this limitation. Note that OpenSCAD is dynamically typed.

Length 5

Much of OpenSCAD's power comes from its conditional branching. Assuming that a is a boolean value, you can write:

if(a)

Alternatively, you can put in any expression that evaluates to a boolean. This may be followed by a single statement, or by a code block surrounded by braces. The if statement shouldn't be used to conditionally assign variables, however—Any variable re-assignment creates a scope for the new variable which effectively shadows, not replaces the original variable. Conditional variable assignment must be done on declaration of the variable, with the ternary operator. Length 6:

This one is really cool:

hull()

It's the digital equivalent of wrapping a piece of plastic wrap around an object:

Without hull:

No hull

With hull:

Yes hull

Length 7:

This one isn't too exciting, but it is very useful:

[0:2:6]

This evaluates to an array. The first number is the starting value, the last number is the ending value, and the (optional) middle number is the step. For example, the previous line of code would evaluate to [0,2,4,6]. Arrays can be nested (jagged arrays and different levels of nesting are allowed) and they can be assigned to a variable.

Length 8:

This is one of the three main CSG operations in OpenSCAD:

union(){

Now would be a good time to talk about the tree structures in OpenSCAD. In OpenSCAD, every object (in terms of 3D modeling, not OOP; OpenSCAD is definitely not OOP) is a node on a tree, and each can contain sub-trees.

Every OpenSCAD model is built from a handful of built-in primitive objects (these primarily act as end-nodes), and various built-in operations (these typically contain their own sub-trees). Additionally, you can define your own operations that either transform their sub-trees, or create a model on its own—but that's a topic for later on.

Back to union: this operator wraps everything in its sub-tree into one node (but doesn't do anything to the objects themselves), so that they can be treated as one whole unit when used in other operations. It may not do much on its own, but when combined with other operations, it can be a vital tool.

Length 9:

Here's a mouthful:

minkowski

The minkowski function involves taking the minkowski sum of an object. This one is a bit weird, as it is taking one shape, and moving it around the surface of another object. It's hard to explain in words, so an image is definitely helpful.

Non-summed:

Not summed

Summed:

Summed

The same process can also be extrapolated into the third dimension:

3D Non Summed 3D Summed

Note that when using this, it can take a while for it to render, especially for more complex objects.

Daniel M.
  • 3.9k
  • 2
  • 26
  • 39