Michael Fogleman

Projects AboutResumeMore

SDF Modeling January 2021

1,497 Python 3D Meshing

Generate 3D meshes based on signed distance functions with a dirt simple Python API.

The goal of this library is to provide a simple, fun, and easy-to-use API for generating 3D models in our favorite language Python. The code simply uses the Marching Cubes algorithm to generate a mesh from a signed distance function (SDF). This would normally be abysmally slow in Python. However, numpy is used to evaluate the SDF on entire batches of points simultaneously. Furthermore, multiple threads are used to process batches in parallel. The result is surprisingly fast (for marching cubes). Supports text, 2D extrusion & revolution, image masks, various file formats, and more!

The code below is a complete example which generates the model shown in the image. This is the canonical Constructive Solid Geometry example. Note the use of operators for intersection, union, 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')