OpenSCAD Cookbook
OpenSCAD Recipes for learning 3D modeling
by John Clark Craig
Dedication
To my mother,
who sparked my curiosity and opened my mind.
For our many talks through the years on the many mysteries of the Universe.
For always being a source of inspiration.
Getting Started
The best way to learn OpenSCAD is to jump right in and start using it. This introduction explains many of the basic syntax rules and other details of creating 3D objects by creating simple shapes and objects, with side explanations to help you firmly grasp the concepts.
You can't really learn to ride a bicycle by reading a book or watching an online video. You need to actually go outside, get in the saddle, and try riding. With a little effort, you'll quickly learn to get balanced and be a true biker. It's the same with OpenSCAD. As you read these recipes, go ahead and fire up OpenSCAD to try them. Change the numbers, experiment freely, see if you can predict what will happen if you make little changes here and there. You'll become a true 3D modeling guru much faster this way.
Above all, have some fun. There's something magical about typing a line or two of commands and suddenly seeing a 3D object appear in space, ready to rotate, zoom, and pan to get a solid feel for its, well, solidness. It's very cool. I've couched the content in this book into "recipes" to help keep things interesting. Just like with a cookbook, try adjusting the recipes a little here and there to see what happens. Also, I might throw in a food pun or two. I can't help it.
Why use OpenSCAD
The main difference between OpenSCAD and other interactive 3D modeling software packages is in the way you create the 3D objects. Most software uses the mouse and keyboard to draw on the fly, with little to no text input. OpenSCAD on the other hand lets you type out explicit text commands, as a script or program, that drive the creation of the objects. In both types of programs you get to see the results, including spinning, scaling, panning, and zooming the view, but the way you create the objects is quite different.
I suggest trying both types of 3D modeling software to see what works best for the way your own brain works. For me, OpenSCAD was by far the better way to work, as it lets me have complete understanding of the effects of changes and modifications as I go. Of course your mileage may vary.
Be aware that OpenSCAD is great for mechanical design, but not so great for what I call artistic 3D design. If your goal is to create usable objects on a 3D printer, even complex systems of parts, OpenSCAD is a great choice. If you are looking to create things like 3D animated figures for gaming, or complex organic shapes of a generally non-mathematical nature, there are other software tools that will work better for you, such as Blender.
Install OpenSCAD
If you haven't installed OpenSCAD yet, go to http://www.openscad.org and look for the download instructions for your operating system. You can use OpenSCAD in Windows, on your Mac, or in Linux. The Windows installation is very easy, very quick, and with very low impact on your system. Of course a decently fast graphics card will help things run better and faster.
Cheat Sheet
Documentation for OpenSCAD is a little hodgepodge, and the quality varies a lot. My favorite starting point for researching features of the language is the official "cheat sheet" page that has links into the primary documentation for each command, key word, and system variable.
http://www.openscad.org/cheatsheet
While trying out the following recipes have this link handy so you can get detailed information when desired.
How to Learn from this Book
Learning by doing is what this book is all about. Each recipe adds a few more knowledge bits to your understanding of OpenSCAD, presented in small bites for easy digestion. Each recipe is as short as possible, without being too short, to make it easier for you to type in, or copy and paste if desired, so you can actually interact with the code and try out new things. Skim through the recipes sequentially, and when you see something interesting, or something even the least bit mysterious to you, then by all means play with that recipe for a while.
I started my programming career with the BASIC programming language, eventually writing a bunch of popular books for Microsoft Press and O Reilly Media. BASIC let you try things out real quick, with the result that learning and absorbing all the syntax and language details happened fast and easy. OpenSCAD has that same great nature to it. Type in a few lines from these recipes, try to guess what will happen when you change some detail, and THEN give it a try. I guarantee you'll learn very fast, while having a lot of fun, and in no time at all you'll be 3D printing some fun, productive, and amazing stuff. Be the guru!
I hope you like the flavor of these recipes!
Recipe 1
Hello World Meatball!
Just about every modern programming language has "Hello World!" as its very simplest example program. This is useful to make sure you have the language up and running correctly, that you are entering text or commands correctly, and to basically get the basics out of the way. You know... did you preheat the oven?
For this recipe type in the following one-line command, making sure to add the semicolon at the end of the command:
This doesn't display the text "Hello World!" like most computer languages do, but this spherical globe looks like a world in space, or perhaps a tasty meatball, so in a way it's much more cool.
If this is the first time you've created something in OpenSCAD, now is a great time to get familiar with the menus, editing window, and the view window. Rather than get sidetracked from our recipes, Appendix A covers the user interface in detail. If you need a refresher on how to spin, pan, or zoom the sphere, check out Appendix A.
Now let's explore some finer details around the sphere() command in the OpenSCAD language.
You can name the default radius parameter with an "r=" if desired, or with "d=" if you want to provide the diameter instead of the radius. The following three commands all create the same sphere as shown above:
Many of OpenSCAD's commands have named parameters, but usually the default parameters without names are simpler and end up being used most often. Just be aware that some commands have extra parameters that might provide some functionality you'll need some day.
You probably noticed the sphere is comprised of many small, flat rectangles, and is not very smooth at all. The default resolution for creating curved surfaces is a compromise between speed and smoothness. It works well using the default setting, but it's easy to change this setting for smoother surfaces. A special variable named $fn can be used to set the "number of fragments" for creating curved surfaces. There are other special variables at play here as well, but in almost all cases just setting $fn will accomplish what you need.