Skip to content

PythonSCAD Cheat Sheet

3D Primitives

cube([x, y, z], center)
Create a box with specified dimensions
cube([10, 20, 30]).show()
sphere(r | d)
Create a sphere with radius r or diameter d
sphere(15).show()
cylinder(h, r|d, center)
Create a cylinder or cone with height h and radii
cylinder(h=20, r1=10, r2=5).show()
polyhedron(points, faces)
Create a 3D solid from vertices and face indices
polyhedron(pts, tris).show()
surface(file, center)
Generate a 3D surface from a data file or image
surface(file="terrain.dat").show()
sheet(func, imin, imax, ...)
Generate a 3D surface from a Python function
sheet(myfunc, 0, 10, 0, 10).show()

2D Primitives

square([x, y], center)
Create a 2D rectangle
square([10, 20]).show()
circle(r | d, angle)
Create a 2D circle or arc sector
circle(r=5, angle=90).show()
polygon(points, paths)
Create a 2D polygon from a list of points
polygon([[0,0], [10,0], [5,10]]).show()
polyline(points)
Create an open 2D polyline (e.g. for laser cutting)
polyline([[0,0], [10,0], [5,10]]).show()
spline(points, fn)
Create a smooth 2D curve through given points
spline([[0,6],[10,-5],[20,10]], fn=20).show()
text(t, size, font, ...)
Create 2D text geometry
text("Hello", size=10).show()
textmetrics(t, size, font, ...)
Get bounding box metrics for text without creating geometry
m = textmetrics("Hello", size=10)

1D Primitives

edge(size, center)
Create a 1D edge with a given length
edge(size=10, center=True).show()

Transformations

translate(obj, [x,y,z])
Move object by x, y, z
cube(5).translate([10, 0, 0]).show()
rotate(obj, [x,y,z])
Rotate object by angles in degrees
cube(5).rotate([45, 0, 0]).show()
scale(obj, [x,y,z])
Scale object by factors along each axis
cube(5).scale([1, 2, 1]).show()
mirror(obj, [x,y,z])
Mirror object across a plane defined by the normal vector
cube(5).mirror([1, 0, 0]).show()
resize(obj, [x,y,z], auto)
Resize object to exact dimensions
sphere(5).resize([10, 10, 20]).show()
multmatrix(obj, m)
Apply a 4x4 transformation matrix
cube(5).multmatrix(mat).show()
divmatrix(obj, m)
Apply the inverse of a 4x4 transformation matrix
cube(5).divmatrix(mat).show()
color(obj, c, alpha)
Apply color to object by name, hex, or [r,g,b,a]
cube(5).color("Tomato").show()
offset(obj, r | delta, chamfer)
Offset a 2D shape inward or outward
square(10).offset(r=2).show()
pull(obj, src, dst)
Pull apart an object by inserting void at a point
cube(5).pull([1,1,3], [4,-2,5]).show()
wrap(obj, target, r|d)
Wrap a flat object around a cylinder
square(10).wrap(cylinder(r=5,h=10)).show()
align(obj, refmat, objmat, flip)
Align object to a handle / reference matrix
cyl.align(cyl.right_center, cyl.origin).show()
roof(obj, method)
Create a roof from a 2D polygon (experimental)
polygon(pts).roof().show()

Convenience Transforms

right / left / front / back / up / down
Translate along a single axis
cube(5).right(10).up(3).show()
rotx / roty / rotz
Rotate around a single axis
cube(5).rotx(45).show()

Boolean Operations

union(obj1, obj2, r, fn)
Combine objects; optionally fillet edges with r
union(cube(5), sphere(3)).show()
difference(obj1, obj2, r, fn)
Subtract obj2 from obj1; optionally fillet with r
difference(cube(10), sphere(7)).show()
intersection(obj1, obj2)
Keep only the overlapping volume
intersection(cube(10), sphere(7)).show()
hull(obj1, obj2, ...)
Create the convex hull of objects
hull(cube(3), sphere(2).right(10)).show()
fill(obj1, obj2, ...)
Fill concavities in a 2D shape
fill(polygon(pts)).show()
minkowski(obj1, obj2)
Minkowski sum of two objects
minkowski(cube(10), sphere(2)).show()
concat(obj1, obj2, ...)
Concatenate meshes without boolean operations
concat(part1, part2, part3).show()

Extrusions

linear_extrude(obj, height, twist, ...)
Extrude a 2D shape or function linearly
circle(5).linear_extrude(height=10).show()
rotate_extrude(obj, angle, v, ...)
Extrude a 2D shape by rotating; supports helix via v
circle(3).right(10).rotate_extrude(angle=360).show()
path_extrude(obj, path, ...)
Extrude a 2D shape along an arbitrary 3D path
path_extrude(square(1), [[0,0,0],[0,0,10],[10,0,10]]).show()
skin(obj1, obj2, ..., segments, interpolate)
Skin across multiple 2D profiles placed in 3D space
skin(square(4).roty(40), circle(2).up(10)).show()

Operators on Solids

Translate object by displacement vector
(cube(5) + [10, 0, 0]).show()
Difference (or translate by negated vector)
(cube(10) - sphere(7)).show()
Scale object by a factor or vector
(cube(5) * 2).show()
Union of two objects
(cube(5) | sphere(3)).show()
Intersection of two objects
(cube(10) & sphere(7)).show()
Apply a 4x4 transformation matrix (multmatrix)
(cube(5) @ mat).show()
Hull of two objects (or explode with vector)
(cube(3) ^ sphere(2).right(10)).show()
Minkowski (with solid) or rotate (with vector)
(cube(10) % sphere(1)).show()
Debug modifiers: highlight / background / show-only
show(+cube(5) | -sphere(3))

Object Properties and Attributes

Bounding box dimensions [w, h, d] or [w, h]
print(cube(10).size)
Bounding box minimum corner coordinates
print(cube(10).position)
Bounding box as a solid (cube for 3D, square for 2D)
cube(10).bbox.color("red").show()
obj.mesh(triangulate, color)
Extract vertices and triangles as Python lists
pts, tris = cube(10).mesh()
obj.faces(triangulate)
Get a list of face solids with orientation matrices
for f in sphere(5).faces(): f.show()
Get a list of edge solids from a face
edges = square(10).edges()
obj.inside(point)
Check if a point is inside the solid
cube(10).inside([5, 5, 5])
Get child nodes as a tuple
parts = union(cube(5), sphere(3)).children()
Read root color RGBA tuple (r,g,b,a) in [0,1], or None
cube(5).color("Red").c # (1.0, 0.0, 0.0, 1.0)
Access node-specific data (polygon points, face matrix, etc.)
pts = my_polygon.points

Object Model

All transforms and operations available as methods
cube(5).translate([10,0,0]).color("red").show()
Create a deep copy of the solid
copy = cube(5).clone()
Return the object's metadata dictionary
print(obj.dict())
Store and retrieve arbitrary metadata on solids
c["name"] = "my cube"; print(c["name"])
Iterate over child nodes (yields ChildRef)
for ch in union(a, b): ch.show()
memberfunction(name, func)
Register a user-defined method on all solids
memberfunction("double", lambda s: s.scale([2,2,2]))

Mesh Operations

explode(obj, v)
Explode a solid outward by a vector
cube(5).explode([1,1,1]).show()
oversample(obj, size, texture, projection, texturewidth, textureheight, texturedepth)
Subdivide mesh edges for finer detail
cube(5).oversample(2).show()
debug(obj, faces)
Visualize mesh faces for debugging
cube(5).debug().show()
repair(obj, color)
Make a solid watertight / manifold
osimport("broken.stl").repair().show()
fillet(obj, r, sel, fn, minang)
Add rounded fillets or chamfers to edges
cube(10).fillet(2, fn=5).show()
Split a solid into disconnected parts
parts = separate(my_solid)

Display and Export

show(obj)
Render and display the object in the viewport
cube(10).show()
export(obj, file)
Export object to a file (STL, 3MF, etc.)
cube(10).export("output.stl")
export({"name": solid, ...}, "out.3mf")
Multi-object 3MF: dict keys = part names (.3mf only)
export({"a": cube(5), "b": sphere(3)}, "parts.3mf")
render(obj, convexity)
Force full geometry evaluation
cube(10).render().show()
projection(obj, cut)
Project a 3D object to 2D
sphere(10).projection(cut=True).show()
group(obj)
Group objects without performing boolean operations
group(cube(5) | sphere(3)).show()
Debug modifiers for visualization
highlight(cube(5)).show()
MultiToolExporter(prefix, suffix, mkdir, items)
Split a model into per-color/per-tool files via cumulative difference
MultiToolExporter("out/m-", ".stl", mkdir=True, items=[("r",red),("b",blue)]).export()

I/O and Integration

osimport(file, ...)
Import geometry from file (STL, OFF, AMF, 3MF, SVG, DXF)
osimport("model.stl").show()
osuse(file)
Use an OpenSCAD library file (like use <...>); call modules/functions on the returned handle
lib = osuse("library.scad"); lib.my_module().show()
osinclude(file)
Include an OpenSCAD file (like include <...>) — deprecated, use osuse instead
lib = osinclude("config.scad")
scad(code)
Execute inline OpenSCAD code from Python
scad("cube(10);")
nimport(url)
Import a model from a network URL (GUI only)
nimport("https://example.com/model.stl")

Math Functions

Sin / Cos / Tan / Asin / Acos / Atan
Trigonometric functions (degrees, like OpenSCAD)
y = Sin(45) # 0.7071...
norm(vec)
Calculate the length of a vector
length = norm([3, 4]) # 5.0
dot(vec1, vec2)
Dot product of two vectors
d = dot([1,0,0], [0,1,0]) # 0.0
cross(vec1, vec2)
Cross product of two 3D vectors
c = cross([1,0,0], [0,1,0]) # [0,0,1]

Special Variables

fn / fa / fs
Control roundness: number of segments / min angle / min size
fn = 50; circle(10).show()
t / phi
Animation step and phi = 2 * PI * t
cube(5).rotate([0, 0, t * 360]).show()

Customizer

add_parameter(name, default)
Add a customizer parameter
width = add_parameter("width", 10)
add_parameter(..., range=)
Slider with min/max (use range() or tuple)
add_parameter("x", 50, range=range(0, 101))
add_parameter(..., options=)
Dropdown menu
add_parameter("c", "red", options=["red", "green"])
add_parameter(..., group=)
Organize into tabs
add_parameter("x", 10, group="Size")
add_parameter(..., description=)
Add help text
add_parameter("x", 10, description="Width in mm")
add_parameter(..., step=)
Spinbox with custom step
add_parameter("angle", 45.0, step=0.5)
add_parameter(..., max_length=)
String max length
add_parameter("name", "hi", max_length=20)

GUI and Utility

rendervars(vpd, vpf, vpr, vpt)
Set camera/viewport from code
rendervars(vpd=150, vpr=[55, 0, 25])
add_menuitem(menu, item, callback)
Add a custom menu item to the GUI (GUI only)
add_menuitem("Tools", "My Tool", my_func)
Get the current model or script file path
path = modelpath()
Get PythonSCAD version info
print(version())
marked(value)
Create a marked F-Rep value (for libfive)
m = marked(3.14)
Set machine configuration dictionary
machineconfig({"bed_x": 200, "bed_y": 200})

Handles

Default handle (identity matrix) on every solid
c.top = translate(c.origin, [5, 5, 10])
obj.align(refmat, objmat, flip)
Align object to a handle on another object
cyl.align(c.right_center, cyl.origin).show()

F-Rep / libfive

frep(exp, min, max, res)
Mesh a signed distance function (libfive expression)
frep(sdf, [-4,-4,-4], [4,4,4], 20).show()
ifrep(obj)
Convert a mesh to a libfive implicit function
tree = ifrep(cube(5))
Low-level SDF building blocks: lv.x(), lv.y(), lv.z(), lv.sqrt(), lv.sin(), ...
c = lv.x()**2 + lv.y()**2 + lv.z()**2 - 4

Additional Resources