Generate 3D meshes based on SDFs (signed distance functions) with a dirt simple Python API.
Special thanks to Inigo Quilez for his excellent documentation on signed distance functions:
Here is a complete example that generates the model shown. This is the canonical Constructive Solid Geometry example. Note the use of operators for union, intersection, and difference.
from sdf import *
f = sphere(1) & box(1.5)
c = cylinder(0.5)
f -= c.orient(X) | c.orient(Y) | c.orient(Z)
f.save('out.stl')Yes, that's really the entire code! You can 3D print that model or use it in a 3D application.
Have a cool example? Submit a PR!
| gearlike.py | knurling.py | blobby.py | weave.py |
|---|---|---|---|
Note that the dependencies will be automatically installed when following the directions below.
- Python 3
- matplotlib
- meshio
- numpy
- Pillow
- scikit-image
- scipy
Use the commands below to clone the repository and install the sdf library
with uv.
git clone https://github.com/fogleman/sdf.git
cd sdf
uv syncConfirm that it works:
uv run python examples/example.py # should generate a file named out.stlYou can skip the installation if you always run scripts that import sdf
from the root folder.