Skip to content

Math Functions

PythonSCAD provides trigonometric and vector math functions that mirror OpenSCAD's built-in math functions. The trigonometric functions use degrees (not radians), matching OpenSCAD convention.

For standard Python math (radians), use Python's built-in math module instead.

Trigonometry

Sin

Calculate the sine of an angle in degrees.

from pythonscad import *

y = Sin(45)   # 0.7071...
y = Sin(90)   # 1.0

Cos

Calculate the cosine of an angle in degrees.

from pythonscad import *

y = Cos(60)   # 0.5
y = Cos(0)    # 1.0

Tan

Calculate the tangent of an angle in degrees.

from pythonscad import *

y = Tan(45)   # 1.0

Asin

Calculate the arc sine, returning degrees.

from pythonscad import *

angle = Asin(0.5)   # 30.0

Acos

Calculate the arc cosine, returning degrees.

from pythonscad import *

angle = Acos(0.5)   # 60.0

Atan

Calculate the arc tangent, returning degrees.

from pythonscad import *

angle = Atan(1.0)   # 45.0

OpenSCAD reference: Mathematical Functions


norm

Calculate the Euclidean length (magnitude) of a vector.

Syntax:

length = norm(vec)

Parameters:

Parameter Type Description
vec list of numbers Input vector

Examples:

from pythonscad import *

length = norm([3, 4])       # 5.0
length = norm([1, 2, 3])    # 3.7416...

OpenSCAD reference: norm


dot

Calculate the dot product of two vectors.

Syntax:

result = dot(vec1, vec2)

Parameters:

Parameter Type Description
vec1 list of numbers First vector
vec2 list of numbers Second vector

Examples:

from pythonscad import *

d = dot([1, 0, 0], [0, 1, 0])   # 0.0
d = dot([1, 2, 3], [4, 5, 6])   # 32.0

cross

Calculate the cross product of two 3D vectors.

Syntax:

result = cross(vec1, vec2)

Parameters:

Parameter Type Description
vec1 [x, y, z] First vector
vec2 [x, y, z] Second vector

Returns: A 3D vector perpendicular to both inputs.

Examples:

from pythonscad import *

c = cross([1, 0, 0], [0, 1, 0])   # vector(0, 0, 1)
c = cross([0, 1, 0], [1, 0, 0])   # vector(0, 0, -1)

Vector functions

There are many places where PythonSCAD accepts vectors supplied as Python lists with 2 or 3 numbers. Tuples and other sequences are accepted as well. Alternatively, PythonSCAD also accepts PythonSCAD Vector objects with calculating capabilities.

Vector1 / Vector2 / Vector3 / Matrix4x4

The pythonscad module exports fixed-size helper types for coordinates and transforms:

Type Shape Typical use
Vector1 [x] 1D sizes / scalars in vector slots
Vector2 [x, y] 2D points and sizes
Vector3 [x, y, z] 3D points, sizes, directions
Matrix4x4 4×4 Transform matrices for multmatrix / origin

When NumPy is installed these classes are NumPy-backed (ndarray subclasses); without NumPy they fall back to plain list subclasses. Constructors accept lists, tuples, other vectors/matrices, or NumPy arrays. Check pythonscad.HAS_NUMPY if a script needs to branch on that.

from pythonscad import *

p = Vector3([10, 0, 5])
m = Matrix4x4([[1, 0, 0, 0],
               [0, 1, 0, 0],
               [0, 0, 1, 0],
               [0, 0, 0, 1]])
show(cube(1).translate(p))

NumPy support

Anywhere PythonSCAD accepts a vector or a list of vectors, a NumPy array or a tuple can be used instead of a Python list.

  • A 1D NumPy array of length 2, 3, or 4 is accepted wherever a single vector (e.g. a size, translation, or point) is expected.
  • A 2D NumPy array (shape (n, 2) or (n, 3)) is accepted wherever a list of vectors (e.g. a point list) is expected.
  • Individual NumPy scalars (e.g. numpy.float64, numpy.int64) are accepted anywhere a plain Python number is expected.

Supported dtypes are float64, float32, int64, and int32. NumPy is not a build-time dependency of PythonSCAD; support only activates if the script itself imports NumPy.

from pythonscad import *
import numpy as np

# A cube sized from a 1D numpy array
c = cube(np.array([1, 2, 3]))

# Tuples work the same way as lists
show(c.translate((10, 0, 0)))

# Translate by a 2D numpy array of vectors (broadcasts a list of copies)
show(c + np.array([[10, 0, 0], [0, 10, 0]]))

Vector addition

from pythonscad import *

vec1 = vector(1,2,3)
vec2 = vector(1,1,1)
print(vec1 + vec2)

Vector subtraction

from pythonscad import *

vec1 = vector(3,3,3)
vec2 = vector(1,2,4)
print(vec1 - vec2)

Vector scaling

from pythonscad import *

dim = sphere(1).size * 2.5
print(dim)

Cross Product

from pythonscad import *

vec1 = vector(1,0,0)
vec2 = vector(0,1,0)

print(vec1 * vec2)

Dot Product

from pythonscad import *

vec1 = vector(1,4,7)
vec2 = vector(1,2,4)

print(vec1.dot(vec2))

Values access

from pythonscad import *

vec1 = vector(1,4,7)
vec1[1] = -3.5
print(vec1[0], vec1[1],vec1[2])

OpenSCAD reference: cross