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.
Cos
Calculate the cosine of an angle in degrees.
Tan
Calculate the tangent of an angle in degrees.
Asin
Calculate the arc sine, returning degrees.
Acos
Calculate the arc cosine, returning degrees.
Atan
Calculate the arc tangent, returning degrees.
OpenSCAD reference: Mathematical Functions
norm
Calculate the Euclidean length (magnitude) of a vector.
Syntax:
Parameters:
| Parameter | Type | Description |
|---|---|---|
vec |
list of numbers | Input vector |
Examples:
OpenSCAD reference: norm
dot
Calculate the dot product of two vectors.
Syntax:
Parameters:
| Parameter | Type | Description |
|---|---|---|
vec1 |
list of numbers | First vector |
vec2 |
list of numbers | Second vector |
Examples:
cross
Calculate the cross product of two 3D vectors.
Syntax:
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:
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.
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.
Vector addition
Vector subtraction
Vector scaling
Cross Product
Dot Product
Values access
OpenSCAD reference: cross