OpenJSCAD

What follows is concise documentation for the core of OpenJSCAD. The official docs can be a bit confusing as they cover both V1 and V2 and include a lot of deprecated information. I recommend opening OpenJSCAD and experimenting with it while referring to these docs. I've used it to produce many objects for 3D printing. While it has its limitations, it's a very quick way to get started, assuming you know JavaScript.

0. 3D

  • x,y,z coordinates
  • Architecture-style (x and y are the surface of a table or floor plan; z is vertical, i.e., out of the page).

1. main

Always have a main function which returns an object

function main() {
  return union(
    difference(cube({ size: 3, center: true }), sphere({ r: 2, center: true })),
    intersection(
      sphere({ r: 1.3, center: true }),
      cube({ size: 2.1, center: true }),
    ),
  )
    .translate([0, 0, 1.5])
    .scale(10)
}

2. Basic Shapes: Cube

cube({ size: 1, center: true })
cube({ size: [1, 2, 3] })
  • size s or [x,y,z]
  • center (location) false by default (at 'bottom' corner)

All shapes follow this pattern: a simple function plus options.

3. Basic Shapes: Cylinder

cylinder({ r: 1, h: 10 })
  • r for radius
  • h for height
  • again can center or not (default)
  • fn to control the level of detail (number of fragments).

4. Transforms: Scale

scale(2, obj)
scale([1, 2, 3], obj)
  • can scale in simple way
  • or individually per x, y, and z axis.

All transformations follow this pattern: a function that takes options first, then the object(s) to be transformed. Note that the official documentation can be confusing as it covers the old OOP-style cube().scale(2) approach, which you shouldn't use.

5. Transforms: Translate

translate([5, 4, 3], obj)
  • Move by [x,y,z]
  • e.g. move up 5 units:
translate([0, 0, 5], obj)

6. Transforms: Rotation

rotate([90, 0, 45], obj)
  • Rotate around the x, y, and z axes by the specified angles (respectively).
  • e.g., rotate about the vertical axis (z-axis) by 90 degrees:
rotate([0, 0, 90], obj)

NB Degrees not radians

7. Operations: Union

  • Glue stuff together
union([obj, another])
union(obj, another)

All composition operations take either an array of shapes or multiple shapes as arguments. You cannot mix these two methods.

8. Basic Shapes: Sphere

sphere({ r: 4 })
  • r for radius
  • center is true by default (unlike other primitive objects).
  • fn to control the level of detail (number of fragments).

9. Basic Shapes: Polygon + linear_extrude

List of points to create a 2D polygon on the x,y plane.

let p1 = polygon([
  [0, 0],
  [3, 0],
  [3, 3],
])
let p2 = polygon({
  points: [
    [0, 0],
    [3, 0],
    [3, 3],
  ],
})

Then extrude with linear_extrude:

linear_extrude({ height: 10 }, p1)
linear_extrude({ height: 1 }, p2)

10. Operations: Intersect

  • The resulting shape is the portion that exists in all the input shapes.
intersection(
  sphere({ r: 1.3, center: true }),
  cube({ size: 2.1, center: true }),
)
  • Can take multiple arguments or an array of shapes.

11. Operations: Difference

  • The resulting shape is the first shape with the subsequent shapes carved out of it.
difference(cube({ size: 3, center: true }), sphere({ r: 2, center: true }))
  • Can take multiple arguments or an array of shapes.

Onward

You now know enough to create nearly any 3D shape (or an approximation of it) with OpenJSCAD.