In order to maximise our learning we're actually going to tackle 3 things, that is feed 3 birds with one scone.
We're going to learn:
- Some basic programming and the OpenSCAD syntax
- Some best practices so that you can read your own code a couple days after you wrote it
- Get practise designing a real thing with tolerances and all so that you're well equiped to make your next thing
We're going to achieve that by making this cute print-in-place hinge, print in place means that there will be no assembly step, both parts of the hinge will be printed pre-assembled.
This tutorial makes no assumption about previous knowledge, which means it's fine you you haven't done any programming before, we'll walk you through it.
If you came here from "getting started" then you would have already got a shape on screen with
If you came from elsewhere, open the OpenSCAD desktop app or go to our online editor, and add
cube([10,10,10]); to get the following cube:
OpenSCAD has a number of modules available that allow us to make shapes and
cube is one of them.
Modules take the form
cube as an example, it take the first form where the moduleName is
cube and it takes one parameter, though that parameter is an array denoted by the square brackets
, which is basically a series of values.
Try changes the line to
cube([10,10,20]);. You should see the cube grow twice as tall as it was previously
That's because the
[10,10,20] is giving the cube's dimensions in each axis, the order is
[x, y, z], and as we've seen the
z axis is the vertical axis.
Before we go any further, now is a good time to mention a couple of principles that you should always keep in mind for OpenSCAD and programming in general
- Computers are incredibly stupid, but do exactly what you tell them to do, which means if there's even a small part of code that's incorrect, then OpenSCAD will display errors, it's normal for this to happen often and bears no reflection on your ability to program, keep at it! which leads to the next point.
- Never get intimidated, if you don't understand a part of the syntax or code try changing the parameters, see what happens. Playing with the code in this manner is one of the best ways to learn.
- The OpenSCAD cheatsheet is very useful as a reference. You should always have it open in a tab.
- There are multiple ways to call these modules, i.e.
cube([10,10,10]); cube(size=[10,10,10]); cube([10,10,10], false);all have the same result, it depends on if you prefer shorthand or not, to avoid confusion, for this tutorial we'll use longhand
argumentName=valuein circumstances where we're using more than one argument, and shorthand if it's only one, i.e.
[10,10,10]is considered one value because it a single array).
- Don't worry about units. nominally the units are in the metric millimetre, since that is the standard global engineering unit, but because it exists in virtual space they are effectively unit-less and can be scaled later.
- If your code isn't working, 9 out of 10 times it's because you are missing a semi-colon
;, Generally speaking most lines apart from ones with a curly brace
}need to end with a semi-colon.