PythonSCAD Helper

๐Ÿงฑ Primitives

cube([x, y, z])
Create a rectangular cuboid with specified dimensions
cube([10, 20, 30]).show()
sphere(r)
Create a sphere with radius r
sphere(15).show()
cylinder(h, r1, r2)
Create a frustrum of a cone with height h and radii r1 and r2
cylinder(h=20, r1=10, r2=5).show()
square([x, y])
Create a square
square([5, 15]).show()
circle(r)
Create a circle
circle(5).show()
polygon(points)
Create a polygon with given points
polygon([[0,0], [10,0], [5,10]]).show()
polyhedron(points, faces)
Create a 3D shape from points and faces
points = [[0,0,0], [1,0,0], [1,1,0], [0,1,0], [0,0,1], [1,0,1], [1,1,1], [0,1,1]]
faces = [[0,1,2,3], [4,5,6,7], [0,1,5,4], [1,2,6,5], [2,3,7,6], [3,0,4,7]]
polyhedron(points, faces).show()
spline(points, fn)
Create a smooth curve through specified points
pts = [[0,6],[10,-5],[20,10],[0,19]]
s = spline(pts, fn=20).linear_extrude(height=1)
for pt in pts:
  s |= cylinder(r=0.3, h=10, fn=20).translate(pt)
s.show()
text(string, size, font)
Render 2D text with size and font
text("Hello", size=10).linear_extrude(2).show()

๐Ÿ”„ Transformations

translate(obj,[x, y, z])
Translate object by x, y, z
cube([5,5,5]).translate([10, 0, 0]).show()
rotate(obj,[x, y, z])
Rotate object by x, y, z degrees
cube([5,10,5]).rotate([0, 0, 45]).show()
scale(obj,[x, y, z])
Scale object by x, y, z factors
cube([5,5,5]).scale([2, 2, 1]).show()
mirror(obj,[x, y, z])
Mirror object across the plane defined by x, y, z
cube([5,10,15]).mirror([1, 0, 0]).show()
multmatrix(m)
Apply transformation matrix
mat = [[1,0,0,10],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
cube(5).multmatrix(mat).show()
divmatrix(m)
Apply inverse matrix
mat=[[1,0,0,6],[0,1,0,0],[0,0,1,0],[0,0,0,1]] # Move to the right by 6
a=cube([1,1,1])
b=a.multmatrix(mat) # move to right
c=b.divmatrix(mat) # move back left
c.show()
b.show()
offset(r, delta=None, chamfer=False)
Offset 2D shape
square(10).offset(2).show()
roof(method, convexity=1)
Create roof from 2D shape
square(10).roof().show()
pull(src, dst)
Pull between points
cube(5).pull([0,0,1],[10,2,2]).show()
wrap(r)
Wrap around cylinder
text("Hello again").linear_extrude(3).rotate([90,0,0]).wrap(2).show()

๐Ÿ”— Boolean Operations

union(obj1, obj2)
Union of obj1 and obj2
union(cube([5,5,5]), sphere(3)).show()
difference(obj1, obj2)
Subtract obj2 from obj1
difference(cube([5,5,5]), sphere(3)).show()
intersection(obj1, obj2)
Intersection of obj1 and obj2
intersection(cube(5), sphere(3)).show()

๐ŸŒ€ Extrusions

linear_extrude(shape,height)
Extrude 2D shape linearly
circle(5).linear_extrude(10).show()
rotate_extrude(angle,shape,v)
Extrude 2D shape by rotating it, and displacing in x,y,z
square(3).right(5).rotate_extrude(v=[0,0,10], angle=600).show()
path_extrude(shape, path)
Extrude shape along a specified path, with curved bends
path_extrude(square(2),[[0,0,0],[0,0,5,3],[8,8,8],[8,8,12]]).show()

๐ŸŽจ Appearance

color(obj,"color")
Apply color to object (CSS extended color space)
cube([5,5,5]).color("rebeccapurple").show()
color(obj,[r, g, b, a])
Apply RGB color and alpha as float versions to object
cube(5).color([0.4, 0.8,0.3, 0.2]).show()

๐Ÿงฐ Advanced 3D operations

hull(obj1, obj2)
Create convex hull of obj1 and obj2
hull(cube([5,5,5]), sphere(3)).show()
minkowski(obj1, obj2)
Minkowski sum of obj1 and obj2
minkowski(cube(10), sphere(1)).show()
offset(shape,delta)
Offset 3D shape by delta
cube([10,15,4]).offset(4).show()
fillet(shape,r)
Creates a fillet or bevel on a solid
cylinder(r1=10,r2=5,h=10,fn=6).fillet(fn=2,r=1).show()
concat(obj1, obj2, ...)
Concatenate multiple objects without union
part1 = cube(2)
part2 = sphere(1).right(3)
concat(part1, part2).show()

๐Ÿงช GUI interaction

show(obj)
Render the object
show(cube([5,5,5]))
highlight(obj)
Highlight object
cube(5).highlight().show()
background(obj)
Show in background
cube(5).background().show()
only(obj)
Show only this object
only(cube(5)).show()
marked(number)
Create draggable parameter, press F6 and hold Ctrl while dragging
size = marked(5)
cube(size).show()
print(val)
Prints values to the console
v=125
print("v + v = ",v+v)
add_parameter(name, default)
Add a parameter for customization
add_parameter("myvar", 5)
cube(myvar).show()

๐Ÿ Python-Specific Features

Variables and Expressions
Define variables and use them in expressions
width = 10
height = 20
cube([width, height, 5]).show()
Loops and Conditionals
Use loops and conditionals to create patterns
for i in range(5):
  cube([5, 5, 5]).translate([i * 10, 0, 0]).show()
Functions
Define reusable functions
def pillar(radius):
  return cylinder(h=50, r=radius)
show(pillar(9))

๐Ÿงฐ Utilities

range(start, end, step)
Generates a range of values
for V in range range(0, 10, 2:
  print(v)
len(x)
Returns length of list or string
print(len([5,6,17]))
print(len("some words"))
round(x, digits)
Rounds x to specified number of digits
print(round(3.14159, 2))

๐Ÿ” Flow Control

for i in range(n):
Iterate over a range of values
for i in range(3): cube(2).right(i * 3).show()
if condition:
Execute code block if condition is true
if True: cube(2).show()
while condition:
Execute code block while condition is true
i = 0
while i < 3:
  cube(2).right(i * 3).show()
  i += 1

๐Ÿ“ฆ Modules

def my_shape():
Define a reusable shape
def my_shape():
  return cube(2).up(1)
my_shape().show()
import openscad
Import the OpenSCAD module
import openscad as o
o.cube(2).show()

๐Ÿงฉ Custom Shapes

circle(r, angle)
Create a circular arc
circle(r=5, angle=70).show()
cylinder(r, h, angle)
Create a cylindrical sector
cylinder(r=5, h=10, angle=120).show()
sphere(rfunc)
Create a sphere with radius defined by a function
def rfunc(v):
  cf = abs(v[0]) + abs(v[1]) + abs(v[2]) + 3
  print("cf = ",cf)
  return 10 / cf
sphere(rfunc(10,22,-12), fs=0.5, fn=10).show()
linear_extrude(xsection, height)
Extrude a shape defined by a function along Z-axis
from math import sin
def xsection(h):
  v = 5 + sin(h)
  return [[-v,-v],[v,-v],[v,v],[-v,v]]
linear_extrude(xsection, height=10, fn=20).show()
rotate_extrude(xsection)
Rotate a shape defined by a function around Z-axis
from math import sin, pi
def xsection(h):
  v = 2 * sin(4 * pi * h)
  return [[10+v,-v],[15-v,-v],[15-v,5+v],[10+v,5+v]]
rotate_extrude(xsection, fn=50).show()

โš™๏ธ Extra Features

align(object,connecthandle)
Align one object to another using handles
cone=cylinder(h=10,d1=5,d2=0)
cone.tip=translate(cone.origin,[0,0,10])
cub= cube(6)-[3,3,0]
cone = cone.roty(30) # Handles are also transformed
cone |= cub.align(cone.tip)
cone.show()
mesh()
Convert object to mesh (vertices and triangles)
u = cube(1) | cylinder(d=1,h=10)
pts, tris = u.mesh()
v = polyhedron(pts, tris)
v.show()
print("pts = ",pts,"\n\ntris = ",tris)
quick transformations
Shorthand for common transformations
cube(2).right(1.5).rotx(30).show() (cylinder(r=3,h=5)*0.4 + [5,0,0]).show()
quick combination
Union(+), Difference(-) and Intersections(&)
(cube(2)-sphere(2)).show()

๐Ÿงต Skinning

skin(shape1, shape2)
Create a smooth surface between two 2.5D shapes
a = square(4, center=True).roty(40)
b = circle(r=2, fn=20).rotx(40).up(10)
skin(a, b).show()

๐Ÿ”ง Utilities & Constants

fn
Fragments (sides) used for circles or curves
circle(5, fn=60).linear_extrude(1).show()
fa
Minimum angle per fragment in degrees
circle(5, fa=5).linear_extrude(1).show()
fs
Minimum size (in mm) per fragment
circle(5, fs=0.5).linear_extrude(1).show()

๐Ÿ“ฅ Import / Export

osimport(filename)
Import a file as an object
d=osimport("rooftiles.stl").show()
osuse(scadfile)
Reuse existing SCAD modules
bosl=osuse("BOSL2\\std.scad") bosl.cuboid(5, roundings=0.5, fn=40).show()
scad(code)
Evaluate SCAD code
scad("sphere(5);").show()
export(obj, filename)
Export an object to an 3D file
export(d,"tiles.stl")

โž— PythonSCAD trigonometry

Sin(x)
Returns the sine of angle x (degrees)
Sin(90)
Cos(x)
Returns the cosine of angle x
Cos(0)
Tan(x)
Returns the tangent of angle x
Tan(45)
Asin(x)
Returns the arcus sine of x (degrees)
Asin(1)
Acos(x)
Returns the arcus cosine of x
Acos(0.5)
Atan(x)
Returns the arcus tangent of x
Atan(1)
norm(v)
Vector magnitude
print(norm([3,4,0])) # 5.0
dot(v1, v2)
Dot product
print(dot([1,2,3],[4,5,6])) # 32
cross(v1, v2)
Cross product
print(cross([1,0,0],[0,1,0])) # [0,0,1]
bbox(obj)
Evaluate Bounding Box
min, max = obj.bbox()

๐Ÿž Debugging Aids

debug(obj, faces)
Color selected faces for debugging
debug(cube(2), faces=[1,2]).show()