Posted on by & filed under hardware, programming.

In the United States, we are just entering the holiday season, so I’m going to tell you a story about me being unbearably geeky. I’m doing this as a service so that you can interact with the geeks in your life and to share what I’ve learned about some technologies that let you design and print gifts and ornaments. Unless the geekiness in this post makes perfect sense to you, in which case I’m doing this in order to let you know you can use code to create physical objects, which is interesting in its own right.

Recently I got it into my head to completely redesign the standard set of polyhedral dice by making them out of planes. For example, the familiar 20-sided icosahedron has all of its corners fully defined by three golden rectangles at right angles to each other. (If you are furrowing your brow right now, a search on golden rectangle icosahedron will make things clearer.) So I spent some time doodling images until I had some designs I liked (although I’m not a designer). The next problem was figuring out how I could turn my graph-paper isometric doodles into a physical object. I’m not at all comfortable in the various modeling programs that seem to be popular for creating 3d-printed objects but I really wanted something that I could hold in my hands and throw in the air (hoping for a 20 on landing every time).

Some isometric doodles.

I wanted a 3d version of PostScript or SVG that could produce printable files, and OpenSCAD is pretty close. It’s a program and a programming language and it can export to a variety of formats used by 3d printers. There are a lot of tutorials out there if you want an in-depth introduction, so I’m just going to provide some bare basics.

OpenSCAD works, like most programming languages, by using operations on low-level data structures and organizing those operations into more complex logical systems. In OpenSCAD, the low-level structures include 2d shapes (circle, square, and polygon) and 3d shapes (sphere, cube, cylinder, and polyhedron). These are defined pretty loosely so that, for example, the parameters of square() can produce rectangles and the parameters of cylinder() can produce cones. The operations include transformations like translate, rotate, and scale and boolean operations like union and difference. Just as you’d expect, there are variables, functions, and flow control (for loops and if statements).

So, for example, I can define a set of points to draw a number:

four = [
[3,0],[2,0],[2,2],[0,2],[0,4],[1,5],[1,3],[2,3],[2,5],[3,5]
];

and pass that set of points to a polygon constructor, which I then convert into a 3d object by extruding it.

linear_extrude(height=5, center=false){
polygon(points=four);
}

Combine enough simple objects like this together, keeping your code structure sane so you’re encapsulating repeated operations in a useful way, and you can end up with something like this:

Be aware that you are probably not going to be printing in color unless you want to produce multiple pieces and assemble them.