# Convert a 3D NumPy array of voxels to an STL file

Given a 3D boolean array representing voxels, how can it be converted to a 3D-printer-ready file?

The end-goal I would like to achieve is to print the 3D shape that the numpy array represents (True coding for fill this voxel, False for leave it empty).

For example, the array

[
[
[T, T, T],
[T, F, T],
[T, T, T]
],
[
[T, F, T],
[F, F, F],
[T, F, T]
],
[
[T, T, T],
[T, F, T],
[T, T, T]
]
]


would encode a level-1 Menger sponge.

• Could you expand upon your question please..? – Greenonline Jun 8 '19 at 18:29
• @Greenonline it's hard for me to know what is missing from my question (it's my first question here). The end-goal I would like to achieve is to print the 3D shape that the numpy array represents (True coding for "fill this voxel", False for "leave it empty"). Let me know if I'm still being unclear. – Stephane Bersier Jun 8 '19 at 18:40
• explain in your question what you want, how your boolean array is formated, stuff that helps us understand the problem. – Trish Jun 8 '19 at 19:28
• @Trish I added an example. Does it help? – Stephane Bersier Jun 8 '19 at 20:07
• Seems like OpenSCAD would be suitable for this. – T. M. Jun 9 '19 at 15:03

I agree with the use of OpenSCAD, but since it is difficult to program in OpenSCAD, I would use SolidPython, which is a front end for OpenSCAD with the full programming capability of Python.

In the alternative, you could use any programming language to decode your arrays and generate the OpenSCAD code for the little network of cubes (or voxels).

The final possibility is to generate an STL file directly. I've helped someone do this, but we found the rules to be a little non-intuitive. We used mesh tools to check out results, both by looking for error messages, and by displaying the result to see if it looked as we intended it to look.

• It's also possible to generate STL files in Python using numpy-stl. – Anderson Green Jul 3 '19 at 20:38
• An OpenSCAD approach will be fine if you're going for a low-res minecraft style model with a smallish number of cubes. If the voxels are supposed to approximate a smooth surface at the printer's best resolution, then OpenSCAD will take days to render it out. There has got to be a better way to do this! – Nathaniel Nov 10 '19 at 2:41

Try voxelfuse.

    from voxelfuse.voxel_model import VoxelModel
from voxelfuse.mesh import Mesh
from voxelfuse.primitives import generateMaterials

if __name__=='__main__':
sponge = [
[
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
],
[
[1, 0, 1],
[0, 0, 0],
[1, 0, 1]
],
[
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
]
]

model = VoxelModel(sponge, generateMaterials(4))  #4 is aluminium.
mesh = Mesh.fromVoxelModel(model)
mesh.export('mesh.stl')