/*************************************************
OpenSCAD for the Avid Beginner
In this tutorial I will be outlining the basics for object creation using OpenSCAD and the tools you can use to create everything to your hearts desire!
Author: Tony Mannino
Version: December 4th, 2013
*************************************************/
/*************************************************
So you made it this far. I will assume that you’re interested in this incredible creativity and the ability to create any object of your desire. In this tutorial you will find:
Section 1: Object rendering basics.
-Basic OpenSCAD objects
-How to modify objects
Section 2: Mutating Objects
-Basic Mutations
-Advanced Mutations
Section 3: Operations and loops
-Basic Operations
-Basic Loops
Section 4: Bringing it all together
Throughout this tutorial I will be using commands and code that I derived from the OpenSCAD “cheat sheet”. If you would like to follow along, or have a reference for some of the tools I will be describing in this tutorial, feel free to download the image from http://www.openscad.org/cheatsheet/.
Enjoy the tutorial!
Side note: This tutorial provides all my code snippets that provide examples of different commands and processes in OpenSCAD. To view and render the code, simply remove the “/ *” (without the space) at the beginning and “* /”(without the space) at the end of each code snippet. Or if there are double slashes “/ /”(without the spaces) remove those. These little tabs “comment out” the code so that all the different pieces aren’t run at once. After you’re done with the code, simply add the “/ *” and “* /” or “/ /” back where they were (without the spaces of course) and the code will become commented out again. This is just simply for your convenience.
*************************************************/
/*****
Section 1: Object rendering basics:
OpenSCAD comes preinstalled with a few basic objects that you can use (just like tinkercad!) and modify to create more advanced and specific objects. These basic objects include: Cubes, Spheres, Cylinders, and polyhedrons.
The basic buttons in this software that we will be worried about during this tutorial are the “compile” and “compile and render” buttons.
The “compile” button shows just a quick view of what your object will look like. This is the button we will be using the majority of the time until we’re ready to send our object to the frustrator, uh I mean printer.
The “compile and render” button prepares your object to be exported for 3D printing.
These buttons can be found under the Design tab, or for your convenience F5 for compile and F6 for compile and render.
*****/
/**************** Cubes *******************
We can have “cube(size);” or “cube([width, height, depth]);” to create these objects. The semi-colon shows that you’re done typing in lines of code. (so don’t forget them or else you’ll get frustrated). */
//cube(10);
//cube([10,10,10]);
//You’ll notice that those cubes generated were not in the center. If you want them centered, simply add a “, true” after the brackets or size value in the parenthesis. Respectively:
//cube(10, true); //centers the cube
// ***************************************
/************** Spheres: *******************
Spheres in OpenSCAD are very simple. The only value to enter for them is the size of their radius. Here’s an example: */
//sphere(10); //Basic sphere with a radius of 10. You can change the radius and the sphere will be any other size you want.
// ****************************************
/* ******************Cylinders*****************
There is a lot you can do with cylinders in OpenSCAD. They allow you to make basic cylinders only specifying the size, or get more specific with specifying how large the top and bottom radius’s are. For example you can make the bottom of the cylinder ‘0’ and it will make a cone.
cylinder(height, centered?); – this creates a pole like shape, centered can be either “true” or “false”.
cylinder(height, radius of the bottom side, radius of the top side, centered?); – this way of makeing a cylinder allows for more customization in how it appears in the program.
Here are some examples:
*/
//cylinder(10, true); //basic cylinder, centered (, true)
//cylinder(10,10,10,true); //Bigger cylinder, with a larger top and bottom
//cylinder(10,10,0,true); //Basic Cone shape, notice how the top value is 0
// **********************************
/*************************************************************
Section 2: Mutating Objects
Mutations in OpenSCAD are things that change all the aspects of your object in comparison to the x,y,z axises. With these tools we can do things such as move objects side to side, up or down, and in and out. We can also do things as rotate objects in any direction, scale them to sizes, or even mirror them in comparison to other objects.
I have broken this section down into 2 separate parts for you. The first part talks about the more straightforward mutations, such as translating (move the object side to side, up/down etc.) and rotating objects. The second part to this gives you the basic examples of scaling objects, and mirroring them. These tools are really helpful in getting different objects into specific positions for your creations.
*************************************************************/
/**********
Part 1: Translations and Rotations
These functions differ a little from the functions we used previously. When we want to use these types of functions, we must type out the function that we are using before the object, and then inside of a pair of brackets type out the object that we want to mutate.
The translation function is declared as “translate([x,y,z]) { }”. Each one of the x,y,z values allows you to alter the positioning of the object in respect to that axis. For example, if you want to move the object along the x axis, we would have something like “translate([0,5,0]) { }”. That would move your object 5 spaces in the y axis. Here are some examples of the translation functions in use:
(remember that I “commented out” the code. Simple remove the pairs of “/ *” and “* /” (without spacing of course) to view and compile the code.)
*********/
//Here are spheres translated on different axis. Uncomment them all to see them all in comparison to one another.
//sphere(10); //basic sphere
//x axis changed 25 spots.
/*
translate([25,0,0]) {
sphere(10);
}
*/
//y axis changed 25 spots.
/*
translate([0,25,0]) {
sphere(10);
}
*/
//z axis changed 25 spots.
/*
translate([0,0,25]) {
sphere(10);
}
*/
//All axis changed 25 spots.
/*
translate([25,25,25]) {
sphere(10);
}
*/
/**********************
Rotation: In OpenSCAD you can rotate objects using the same “syntax” (grammar) as the translation function but altering the rotation in terms of degrees.
As you already know, a circle has 360 degrees, so the values for the rotation function allow you to turn your object on the specified axis and certain number of degrees up to 360. (any value over 360 will just repeat the circle).
Here are some basic examples of a cube being rotated.
**********************/
//Basic cube
//cube(10, true);
//rotated 45 degrees on the x axis
/*
rotate([45,0,0]) {
cube(10,true);
}
*/
//rotated 45 degrees on the y axis
/*
rotate([0,45,0]) {
cube(10,true);
}
*/
//rotated 45 degrees on the z axis
/*
rotate([0,0,45]) {
cube(10,true);
}
*/
//rotated 45 degrees in all axis
/*
rotate([45,45,45]) {
cube(10,true);
}
*/
/***********************************
Part 2: Scaling and Mirroring
The scale function in OpenSCAD multiplies the size of your object by the given values and changes the size of your object. It allows you to resize objects on the go, for example if you made a complex object already and you want to size the whole thing up, or smaller you can use this function to resize with precision.
I will provide you with one quick example of how this function works.
************************************/
//Scaling a square 10 cube by 5,3,1
/*
scale([5,3,1]) {
cube(10);
}
*/
//Notice how it changed all its properties and the object isn’t a square cube anymore!
/***********************************
Mirroring
The mirror function in OpenSCAD is extremely useful. This function allows you to perfectly mirror an object off any axis you choose. For example if you create a complex object of great detail, and you want to create an exact copy of it in a different direction, you can specify which axis to mirror off of and it will flip the object on that axis. I’ll show some examples of a cube mirrored on each axis:
***********************************/
//x axis mirror
/*
cube(10);
mirror([1,0,0]) {
cube(10);
}
*/
//y axis mirror
/*
cube(10);
mirror([0,1,0]) {
cube(10);
}
*/
//z axis mirror
/*
cube(10);
mirror([0,0,1]) {
cube(10);
}
*/
//all axis mirror
/*
cube(10);
mirror([1,1,1]) {
cube(10);
}
*/
/************************************************************************
Section 3: Operations and Loops
As you have noticed thus far coding these objects is derived a lot from math and the functions that you use with numbers. Well now I will be showing you some of the basic math operations that work the same for these objects. I have broken these section down into two parts where the first part talks about set operations for the objects and the second talks about loops.
Part 1: Operations
OpenSCAD has 3 main operations integrated in their library that aid us during our creation. These operations are Union(), Difference(), and Intersection. As in math’s set theory, these operations have the same sort of properties in what they do to our objects.
The Union function is extremely useful to us. This function is very similar to the “group” function that you can use on TinkerCad. This allows you to combine all the objects within its brackets.
The Difference function is another useful tool. This allows us to make holes in objects and cut away to make sculpted models from the basic generic ones. This function works by first determining the object you want to take parts away from inside the brackets, and then every object created after the first one is “subtracted” away from the original. To make this seem more simple, the first object in the brackets is you main object you want to keep, and all the other objects below that one are “holes” like in TinkerCad. This allows you to create unique and specific objects.
The intersection is the last tool offered in OpenSCAD. The term intersection basically means “keep the same from each”. So what this function does is that it only generates the portions of the objects in its brackets that overlap. This tool is very useful when trying to make abstract shapes from many different objects.
Now I will provide you with examples of each, and an example of using them all together at once.
******************************************************************/
/**********Union**************/
//Basic object with a cube and a sphere connected to it.
/*
union() {
cube(10, true);
translate([10,0,0]) {
sphere(5);
}
}
*/
//Now I’ll rotate that same code 90 degrees. Notice how they stick together
/*
rotate([0,90,0]) {
union() {
cube(10, true);
translate([10,0,0]) {
sphere(5);
}
}
}
*/
/**********Difference*********/
//This is an example of the OpenSCAD logo!
/*
difference() {
sphere(10,true);
translate([0,10,0]) {
rotate([90,0,0]) {
cylinder(20, 5, 5);
}
}
translate([-10,0,0]) {
rotate([0,90,0]) {
cylinder(20,5, 5);
}
}
translate([0,0,-10]) {
rotate([0,0,90]) {
cylinder(20,5,5);
}
}
}
*/
//Notice the way I incorporated rotate and translate into the code. The rotates turn the cylinders, and the translates make sure they’re perfectly in the middle of the circle.
/********Intersection*******/
//Heres a few examples of intersection.
//First off is the intersection of 2 spheres.
/*
intersection() {
translate([5,0,0]) {
sphere(10);
}
translate([-5,0,0]) {
sphere(10);
}
}
*/
//It kinda looks like an M&m or advil. Back on point though!, you see how it just took the parts that overlapped from the two. You can comment out the intersection line along with the end bracket to see the two spheres before the intersection.
/*****All 3 in one********/
//Here’s another way to make the OpenSCAD logo! (looks kinda like a quarter trap too!)
/*
difference() {
union() {
intersection() {
cube(15, true);
sphere(10);
}
}
sphere(9);
}
*/
/************************************************************
Part 2: Looping
Just like how the operations above are major players in mathematics, looping is just as if not more important in creating objects. Looping allows us to generate multiple objects at once without having to type out the same object in different positions.
In OpenSCAD, there a few looping functions that allow us to iterate over a range of values and do specific functions during each iteration. But for this tutorial I will only be focusing on the basic for-loop. This look is instantiated (typed) like this “for (i = [start:end]) {}”. A for loop is a type of loop that allows us to start at a specific value, and end at another specific value. For example we can have “for(i = [1:5]) {}”. This loop starts at i = 1. It then runs everything inside of the brackets and get s back to the top adding 1 to i. After this i would = 2, and it runs again. It continues this process until i = 5. When i finally gets on the 5th loop, it completes, adds another 1 to i, and then compares it to the end number. By this time i = 6 and the program knows that once i > end, it won’t run anymore.
Now I will show you some examples of how this works:
*************************************************************/
//Basic looping
//I’ll break down each loop and show you have happens for different ranges to help you grasp the idea.
//a for loop from range 0:0
/*
for (i = [0:1]) {
rotate([0,60*i,0]) {
cube([10,1,1]);
}
}
*/
//Notice the 60*i in the rotate function. This allows you to have a value grow as you loop though it. So since this loop ran and i=0, the cube didn’t move.
//Now we’ll change the range to 0:1. This time the loop will run for i=0, and i=1. Notice how there are now 2 objects shown.
/*
for (i = [0:1]) {
rotate([0,60*i,0]) {
cube([10,1,1]);
}
}
*/
/* Now that you’ve seen the 2 blocks shown, I’ll show another example that creates a complete circle. Notice how I made the value inside of the rotate function 60. If we loop over a range from [0:5], the values inside the rotate loop will change as follows: i=0 : 0*60 = 0. i=1 : 1*60 = 60. i=2 : 2*60 = 120. i=3 : = 3*60 = 180. i=4 : 4*60 = 240. i=5 : 5*60 = 300. The values shown provide you with equal parts of a circle. We stop at 5 because the 0 value provides the same object as i=60 or 360 degrees.
Here’s the code:
*/
/*
for (i = [0:5]) {
rotate([0, 60*i,0]) {
cube([10,1,1]);
}
}
*/
//This code above creates a start shaped object.
/* Now that you have seen how operations and looping work, we can create objects such as Snowflakes. Here’s a basic snowflake I have made for you that makes use of the Union function and the same looping technique I used above:
*/
/*
for (i = [0:6]) {
rotate([0,0,60 * i]) {
union() {
translate([0,-1,0]) {
cube([25,2,2]);
}
translate([16,-1,0]) {
rotate([0,0,45]) {
cube([10,2,2]);
}
}
translate([15,-1,0]) {
rotate([0,0,-45]) {
cube([10,2,2]);
}
}
}
}
}
*/
/*****************************************************************************
Section 4: Bringing it all together
All the code and tutorials I provided above are enough to get you started with making your very own objects in OpenSCAD. This tutorial was mean’t to teach the basics of how to use this software, and to help understand some of the basic concepts of this language. I really hope you’ve enjoyed it and it has been helpful in understanding OpenSCAD.
If after going through everything above and you’re still completely overwhelmed or programming in general is just above your head, here are links to some other tutorials that may help you grasp the information. At the same time if you’re seeking more information and more indepth explainations on how to go about creating more advanced objects, these links below will be useful.
All these listed below can be found under the www.openscad.org/documentation website.
OpenSCAD Documentation website: Provides you with a cheat sheet to the main tools used above:
http://www.openscad.org/documentation.html
For a basic tutorial: MakerBot Curriculum
http://curriculum.makerbot.com/daily_lessons/february/
http://www.makerbot.com/blog/2012/11/08/openscad-design-tips/
For a comprehensive wiki page:
http://edutechwiki.unige.ch/en/OpenScad_beginners_tutorial
For a deeper study in Operations and Looping: IHeartRobotics tutorial
http://www.iheartrobotics.com/search/label/OpenSCAD
****************************************************************************/