4

Is there way to convert the stl file content into a numpy array or a set of numpy arrays?

I know that I can always read stl as file and then assign variables, but I was wondering if there is an automated way!

Wolph
  • 78,177
  • 11
  • 137
  • 148
marco
  • 915
  • 4
  • 17
  • 35

2 Answers2

2

Try the numpy-stl package.

After doing pip install numpy-stl:

import numpy
from stl import mesh

# Using an existing stl file:
your_mesh = mesh.Mesh.from_file('some_file.stl')
Wolph
  • 78,177
  • 11
  • 137
  • 148
Uvar
  • 3,372
  • 12
  • 25
  • 2
    As I'm the author of the numpy-stl package, I've edited the answer to improve some example. This should be enough to help @marco further :) – Wolph Aug 13 '15 at 19:38
  • Is there a way of converting .stl files to images using numpy-stl ? – edyvedy13 Jun 24 '17 at 14:29
2

I recommend using trimesh for that. Here is an example script:

from trimesh import Trimesh
from trimesh import voxel
mesh = Trimesh()
mesh.vertices=vertices
mesh.faces=faces
array = voxel.VoxelMesh(mesh=mesh, pitch=1).matrix_solid
Adrian W
  • 4,563
  • 11
  • 38
  • 52
Amit Oved
  • 79
  • 6