Programming with OpenSCAD: A Beginner’s Guide to Coding 3D-Printable Objects introduces the versatile, text-based OpenSCAD 3D CAD software. This book guides readers through using arithmetic, variables, loops, modules, and decisions to design a series of increasingly complex 3D designs, all of which are suitable for 3D printing.
OpenSCAD (pronounced Open-S-CAD) is text-based software for creating solid 3D models. It allows you to design these models by writing code, which gives you (the designer) full control over the modeling process and allows for easy changes to any part of your design throughout that process. OpenSCAD also makes it easy to create designs that are defined by configurable parameters, which means you can build designs with change in mind.
OpenSCAD is a descriptive programming language: its coding statements describe the shape, size, and combination of each component of your overall design. OpenSCAD provides two main 3D modeling techniques: you can create complex objects from combinations of simpler shapes (known as constructive solid geometry) or extrude 2D .dxf or .svg outlines into 3D shapes. Unlike most other free software for creating 3D models (such as Blender), OpenSCAD focuses on the mechanical, rather than the artistic, aspects of 3D design. Thus, OpenSCAD is the application to use when you’re planning to create 3D-printable models, but probably not what you’re looking for when you’re more interested in making computer-animated movies.
OpenSCAD is free, open source software that’s available to download for Windows, macOS, and Linux, as well as other systems. Specific system requirements are available at https://openscad.org/downloads. This book covers OpenSCAD 2021.01, which is the latest version at the time of writing.
This book is for beginners who are either new to code, new to 3D design, or new to OpenSCAD. While no prior background with either coding or 3D design is necessary to access the material presented in this book, some experience with introductory algebra would be helpful. No particular math beyond basic arithmetic (adding, subtracting, multiplying, and dividing) is required. However, some previous experience using variables in simple equations would be a useful starting point, as would some experience graphing points on the xy-plane.
In line with our intentions to make this book beginner-friendly, we have deliberately chosen to focus on a subset of OpenSCAD. Our goal is to introduce its most useful elements in an accessible manner. In some cases, that means we briefly introduce a topic earlier in the book that we return to in more depth in later chapters. This spiraling is intended to help you form a strong foundation in the basics before adding layers of nuance. Our examples and projects have been curated to allow for maximum creative utility while still making the topic accessible to readers who are new to coding.
While learning to code can be fun and exciting, it can also be challenging for beginners to figure out the where and why behind the inevitable errors in their coding projects. In contrast to the opaqueness of other text-based programming languages (in which it’s hard to see what’s going on behind the scenes), OpenSCAD’s visual nature gives users immediate feedback regarding the correctness of a particular approach.
Writing text-based code to create a 3D object is a powerful and effective way to learn how to structure long sequences of coding statements. Like more familiar imperative programming languages (JavaScript, Python, and so on), OpenSCAD includes variables and common control structures (such as if
statements and loops) and supports the creation of programmer-defined modules and libraries. Additionally, OpenSCAD employs common syntax elements, such as using curly brackets ({ }) and semicolons (;), to organize statements as well as the familiar set of arithmetic operators and conventions. OpenSCAD not only opens up the world of text-based 3D design, but also teaches skills that are transferable to many other popular programming languages.
Learning to code with OpenSCAD also offers unique advantages for developing computational thinking. This computer-specific approach uses decomposition, abstraction, patterns, and algorithms to solve a problem in a way that makes it easy for a computer to carry out the solution. Developing an intuition for computational thinking can be difficult for beginners in other programming languages, but OpenSCAD makes it easy with algorithms and coding statements that literally take shape. Applying abstraction and patterns means visually identifying the repetitive and predictable elements in a design; decomposition becomes splitting a complex design into well-defined smaller pieces, and algorithms naturally extend from creating a list of steps that need to happen in order to create a design. The tactile feedback that comes from turning an OpenSCAD design into a physical 3D-printed object adds an entirely new dimension to learning to code.
STEM (science, technology, engineering, math) and STEAM (add art into the mix) are two recently popular acronyms that describe learning activities existing at the intersection of two or more of these traditionally separated disciplines. Learning to code with OpenSCAD is like taking a holistic, STEAM-based approach to learning how to code. OpenSCAD coding projects require translating visual shapes into concisely worded textual descriptions, and vice versa. Designs that start as hand-drawn sketches are converted to mathematical coordinate representation, with features that are estimated with proportionality. Designing with OpenSCAD code requires navigating both orthogonal and perspective views of 3D objects, and thinking about 3D shapes in terms of their 2D shadows. 3D-printing an OpenSCAD design develops engineering skills by requiring the consideration of physical tolerances and the adjustment of machine settings. In true STEAM fashion, this book asks you to simultaneously develop, combine, and practice skills typically relegated to the separate disciplines of technology, engineering, art, and math as you learn to code with OpenSCAD.
Learning to code with OpenSCAD has quite a few advantages:
Most people use OpenSCAD to create designs for 3D printing. At its core, 3D printing is a tool for transforming virtual models into actual physical objects. OpenSCAD is a great choice of software to use when you’re creating parts to manufacture with a 3D printer. However, access to a 3D printer is in no way a prerequisite for this book or for learning to use OpenSCAD. We certainly recognize the appeal of seeing and touching your 3D designs, so we’ve sprinkled 3D-printing tips throughout this book, anticipating that many readers will want to interact with their virtual designs in the real world.
3D printing is used in an ever-increasing number of areas: mechanical engineering, product design, animation, architecture, sculpture, medicine, geology, rocketry, and the list goes on. 3D printing first gained popularity for its uses in rapid prototyping, which allows designers to create physical models and receive real-world feedback much faster than previously possible. However, in addition to prototyping early versions of a design, 3D-printing technologies have advanced to the point where it’s now possible to directly manufacture products in a variety of materials. Designers can now use 3D printing to build the final version of their design, using many types of plastic, glass, metal, magnets, cement, porcelain, bio-matter, and even edible foods! In fact, it’s no longer unusual for mechanical engineers to 3D-print metal rocket-engine parts, for dentists to 3D-print porcelain dental implants, for architects to 3D-print residential houses in cement, or for sculptors and jewelers to 3D-print a wax base for a lost-wax casting.
Although many types of 3D-printing technologies exist, fused-filament fabrication remains the cheapest and most readily accessible technology available. Most of the 3D-printing tips in this book are best suited for fused-filament fabrication, which builds a 3D form by melting successive layers of plastic on top of each other.
This book is split into three sections:
A series of Design Time challenges accompany the first six chapters of the book. These exercises provide quick designs to replicate, suitable to the scope of each chapter’s content. A small collection of Big Projects conclude each chapter. These projects, which require substantively more time and effort than the Design Time activities, are deliberately chosen to present a cumulative challenge.
The designs in both the Design Time and Big Project sections are presented without absolute coordinates, as they are intended to inspire you to build toward a general resemblance without focusing too much on details. For these exercises, the big details like proportionality and shape combinations matter more than anything else. All Design Time and Big Project exercises are well suited for 3D printing.
The following list gives a breakdown of the topics presented in each chapter:
Chapter 1: 3D Drawing with OpenSCAD
Introduces the OpenSCAD interfaces and teaches you to draw and place a few of the OpenSCAD primitive 3D shapes: cuboids, spheres, cylinders, and cones. OpenSCAD can also import 3D shapes generated by other applications, and we introduce that here as well. Another important concept covered is how to combine multiple shapes in a few ways. Finally, you’ll learn how to export an OpenSCAD 3D design for 3D printing. The Big Projects in this chapter are designed to help you get to know the settings on your 3D printer’s preparation software.
Chapter 2: More Ways to Transform Shapes
Presents a variety of additional transformation operations that can be applied to the 3D shapes introduced in Chapter 1. You’ll learn how to rotate, mirror, and adjust the proportionality of 3D shapes. You’ll also learn more sophisticated methods of combining shapes, including how to wrap a hull around two shapes and how to spread the properties of one shape along the edges of another shape with the minkowski
operation. 3D-printing tips in this chapter introduce the concepts of infill and shell. The Big Projects ask you to combine multiple topics from Chapters 1 and 2 to produce objects you may actually use: a game die and a desktop organizer.
Chapter 3: 2D Shapes
Discusses an alternate way of approaching 3D design—building up a 3D form from its 2D shadow. You’ll learn how to draw with primitive OpenSCAD 2D shapes, including circles, rectangles, polygons, and text (including emoji). You’ll also learn how to combine those 2D shapes by using most of the same operations you studied in Chapter 2, as well as a new 2D operation called offset. Finally, you’ll see how to bring 2D shapes into the 3D world by extending them along the z-axis with a variety of new operations. 3D-printing tips in this chapter discuss resizing your 3D models for printing, including how to break a large model into multiple pieces so you can grow your 3D prints beyond the limited size of your 3D printer’s build platform. The Big Projects include storytelling dice, a dice holder, and a 3D trophy built from a 2D profile.
Chapter 4: Using Loops and Variables
Introduces a new tool for computational thinking: the for
loop. You’ll learn how to use variables and for
loops to repeat shapes. The best part is that you’ll learn how to vary the characteristics of a shape (such as its size, position, or rotation) as it’s repeatedly drawn by the loop. This chapter also introduces comments and console printing as useful tools for planning and debugging your designs. 3D-printing tips in this chapter relate to exploring a few gotchas that may surprise you when you try to create 3D-printed objects from OpenSCAD designs: the limitations of small-scale features, reconfiguring a design to avoid fusing together parts that are supposed to be separate, and breaking a design into separate .stl files to print different pieces with different-colored filament. The Big Projects include a detail test, a Towers of Hanoi game, and a tic-tac-toe game.
Chapter 5: Modules
Introduces yet another computational thinking tool: decomposing a design into multiple modules. You’ll learn to use OpenSCAD modules to create your own shapes, as well as use separate files to group your new shapes into a reusable (and shareable) library. You’ll also create and use parameters to control characteristics of your shapes, as well as define variables within modules so that updating the design of new shapes is quick and easy. The Big Projects in this chapter include a skyscraper module and a library of new LEGO brick designs.
Chapter 6: Dynamic Designs with if Statements
Introduces the if
statement, which allows you to create dynamic designs that change according to a certain condition. You’ll learn to create a variety of complex conditions using Boolean and logical operators, as well as extended if
statements, and if...else
statements. You’ll automate some of the design configurations suggested in the Big Projects from Chapter 4, as well as incorporate random numbers to add fun and unpredictable elements to your design and make repeated elements more organic and natural. The Big Projects include creating a random forest, a clock face, and a city of random skyscrapers.
Chapter 7: Designing Big Projects
Presents a capstone project that walks through the process behind creating a big, multifile design. You’ll apply formal characteristics of computational thinking by using the iterative design cycle to reinforce and expand the ideas presented in the first six chapters. You’ll leverage the walking skeleton approach to evolve a simple version of the Leaning Tower of Pisa into a 3D model that bears a high resemblance to the actual tower. You can 3D-print this building as a trophy to congratulate yourself for all that you will have learned by following along with the material presented in the book.
If you get stuck on any exercise in this book, suggested solutions to the Design Time and Big Project exercises (along with all chapter examples) are available at https://programmingwithopenscad.github.io/.
Many introductory books on programming and computational thinking are available, and each author makes tough decisions as to how much granular detail is necessary for the audience they are trying to reach. As this book is meant for beginners, we have chosen to keep a high level of abstraction with regard to our vocabulary and conventions. Although some of the following terms have more precise definitions in other circumstances, our philosophy for this book is consistent with “don’t sweat the small stuff.”
We use the following vocabulary in the book:
If you’ve never worked with virtual 3D models before, manipulating the 3D designs you create in this book via the use of a 2D computer screen can be confusing at first. Understanding some of the basics involved in creating the illusion of 3D space on a 2D surface can also help you navigate the transition to a 3D-modeling environment.
3D objects have a width, length, and height, so drawing a representation of 3D shapes requires the use of three separate axes: the x-axis, y-axis, and z-axis (Figure 1). The intersection of all three axes is called the origin and is indicated as the point (0, 0, 0) on the graph. Each axis proceeds in both positive and negative directions from the origin. Although a width, length, or height must be positive, the position of an object on a particular axis may be in the negative direction (which is relative to the location of the origin).
Sometimes a 2D screen makes it hard to determine the exact 3D point being viewed. For instance, in Figure 1, the point (3, 0, 5) could also be interpreted as the point (0, 4, 3.5). When in doubt about the size or position of a particular shape, rotate your design to gain a fuller perspective of the feature. As you rotate your design, a miniature graph legend (circled in red in Figure 2) rotates accordingly to help you keep track of which axis is which.
This legend is helpful because the axes in OpenSCAD aren’t labeled. This graph legend is a good feedback tool for interpreting the changing orientation of the width, length, and height as you rotate your view to understand each part of your design.
OpenSCAD uses a variety of perspectives and color shading (as does other 3D CAD software) to facilitate the representation of 3D shapes on a 2D computer screen. In addition to using a mouse, touchscreen, or trackpad to rotate your design, the OpenSCAD 3D-View toolbar (Figure 3) provides several buttons for quickly rotating the 3D view of your design to an orthogonal 2D view, which can help reveal a shape’s true location and dimensions.
In order, the buttons reveal the following 2D views: right, top, bottom, left, front, and back.
OpenSCAD has many more advanced features and capabilities than are included in this book. Consider these chapters a starting point in your exploration of the design possibilities offered by OpenSCAD. We’ve included an Afterword to provide a context for the development of OpenSCAD as an open source project, and to provide suggestions for further learning once you’ve finished reading the book. We encourage you to consult the documentation resources at https://openscad.org/, as well as the language reference (Appendix A) included in the back of this book to explore the full range of possibilities offered by the language. For a quick view of the basic features of OpenSCAD covered in the first four chapters of the book, we’ve also included a visual reference (Appendix B).
To truly learn how to design and code 3D-printable objects with OpenSCAD, you’ll need to put the book down periodically. Give yourself an opportunity to type and modify our examples, as well as to create your own versions of our Design Time and Big Project exercises. Then, use the book as a starting point for designing and coding your own projects. In fact, once you’ve learned something new, take a break from the book. Remix or extend our projects and examples, or design something entirely new. Try to design something useful, something that will help you apply that new lesson to a project you’re genuinely interested in. Show off and share your designs. Maybe even give your 3D-printed objects as a gift. Learning something new is much easier when you’re genuinely engaged with the topic, so most of all, have fun!
18.189.2.122