## Get Started with OpenSCAD 3D

Learn the basics of OpenSCAD 3D in this fun lab

A while back in our article about free and open-source 3D modeling, we mentioned alternatives to Blender including OpenSCAD. This is excellent software that allows you to create 3D designs by typing code. This might sound complex, but it makes sense. As with any Computer Aided Design (CAD) environment, you will find that you repeatedly have to type in dimensions, coordinates, and more. So, if you tinker with code, OpenSCAD is quite simple. It takes time to become an expert user, but never fear. OpenSCAD has a large, friendly community of users and lots of helpful online tutorials.

Head to https://openscad.org/ and download the current version of OpenSCAD. Once installed, launch the software and you’ll see a window asking if you want to create a new project or select an example from the menu on the right. Look at some of the examples to see OpenSCAD capabilities, although they tend to be quite complex.

Open a new project. There’s a code input area on the left and a preview window where your model appears on the right. Let’s make a simple cube shape. In the code area type:

``````//code//
cube(5,true);
//code//
``````

Note that this line of code has a semicolon at the end. Most lines of code in OpenSCAD will require this semi-colon. Once you have typed in the code, press “enter” and you will move to the next line in the code window, however, you won’t see your cube. To create a preview of your code, press F5. You should now see a cube. You can probably guess that the “5” inside the brackets refers to 5mm as the size of the edges of our cube.

The “true” defines that we want the cube’s origin point to be the center of the cube structure. If you swap this to “false” and again press F5, you will see the origin point of the cube object change to one of the corners. In the preview window, experiment by holding the left and right mouse or trackpad buttons to move and rotate the model. Hold the center button down scrolling forward and backward to zoom in and out.

Often commands work a few ways.We can use the “cube” command to make a cuboid with different length edges rather than a regular cube. Change the code to the following:

``````//code//
cube([5,10,15],false);
//code//``````

When you press F5 you’ll see that the cuboid is 5*10*15mm in size. The
dimensions are added in the usual alphabetic order of axis so the X axis dimension is 5, the Y axis is 10, and the Z axis is 15.

We can move items and rotate items within our code. Use the “translate” command in front of the item in the code. Try this addition to our line of code to move our cuboid 10mm along the X axis:

``````//code//
translate([10,0,0])cube([5,10,15],false);
//code//
``````

Rotating objects is a similar command but when we use it in combination with the Translate command we must consider the order in which commands are written. Try both the following commands separately to see the difference in results.

``````//code//
rotate([0,90,0])translate([10,0,0]) cube([5,10,15],false);
//code//
//code//
translate([10,0,0])rotate([0,90,0]) cube([5,10,15],false);
//code//
``````

There are numerous objects that work in similar ways to the cube command, like spheres, cylinders (which can have different diameters at either end to create cones), polyhedrons, and more. Let’s create a cylinder object, move it to a position where it collides with our cube and then use a “boolean” operation to cut the cylinder out of our cuboid to create a hole.

In a blank sketch, add the following code to create a cylinder and a cuboid similar to the earlier one.

``````//code//
translate([0,5,7.5])rotate([0,90,0])cylinder(20,3,3,false);
translate([10,0,0]) cube([5,10,15],false);
//code//``````
``` ```

The cylinder appears more like a polygon because, as a default, OpenSCAD is set to a low resolution for the number of faces an object has. At this small scale, it becomes quite blocky. We can add a line to our code that forces OpenSCAD to use more faces. Move the two lines of code down and add the following first line like this.

``````//code//
\$fn=60;
translate([0,5,7.5])rotate([0,90,0])cylinder(20,3,3,false);
translate([10,0,0]) cube([5,10,15],false);
//code//``````
``` ```

With our cylinder crossing our cuboid, they look like one object, however, this isn’t strictly true. If we exported this for 3D printing, the printer would print the internal parts of the cylinder inside the cuboid. In OpenSCAD (and many other CAD packages) we use Boolean operations to combine objects into whole single items, or we can use other Boolean operations to subtract one item from another to create holes and voids, or other operations for just the overlapping sections of objects.

If we combine our cylinder and cuboid, we won’t see a difference as the changes will be inside the resultant object. For this, we will use the “union” operation. Adapt your code like this:

``````//code//
\$fn=60;
union(){
translate([0,5,7.5])rotate([0,90,0])cylinder(20,3,3,false);
translate([10,0,0]) cube([5,10,15],false);
}
//code//``````
``` ```

Indenting the lines inside the curly brackets can help you see if code is sitting inside a function or operation.

Let's remove the cylinder from the cuboid to create a hole by using the “difference” Boolean operation. The order of the objects in the code makes a big difference in the output. Just changing “union” from the previous example to “difference” removes the cuboid from the cylinder. We'll have two small sections of the cylinder and a gap between them. To correctly remove the cylinder from the cuboid creating a hole, re-order the code lines:

``````//code//
\$fn=60;
difference(){
translate([10,0,0])cube([5,10,15],false);
translate([0,5,7.5])rotate([0,90,0])cylinder(20,3,3,false);
}
//code//``````
``` ```

In the previous examples when we have been clicking F5, we have been creating previews. If we want to export our object as an STL file for 3D printing, we first need to click F6 to fully render the object. We can then click the STL icon to export our object. This doesn’t save our code though. To save, click “File – Save As”, name the project, and save it to a preferred location.

If you have made it through the above examples, you can already make a lot of interesting and useful parts for projects. But we've only scratched the surface and there are loads more OpenSCAD approaches to explore.

A really simple and useful resource is this online “cheatsheet” with basic functions: http://openscad.org/cheatsheet/ . Anything you click in the cheatsheet opens up a relevant part of the OpenSCAD online manual which allows you to explore functions' wider usages.

#### Computer-Aided-Design

https://en.wikipedia.org/wiki/Computer-aided_design

#### Computer-Aided-Design Facts for Kids

https://kids.kiddle.co/Computer-aided_design

## Author

### Also In The December 2022 Issue

The electronic toys your parents may have grown up with!

Meet the point where your love of hiking and technology meet.

Learn how kids are falling in love with computer science in Africa

Move over Krabby Paddy, algae burger is coming through!