2

I am planning to write a program in Python for raspberrypi, to import a 3D STL image.

For that purpose, I googled and got a Python library called "numpy-stl" which is suitable for my requirement. I install it according to the instructions of website

sudo pip install numpy-stl

Then try to Run given Code from example.

from stl import mesh

# Using an existing stl file:
mesh = mesh.Mesh.from_file('some_file.stl')

# Or creating a new mesh:
VERTICE_COUNT = 100
data = numpy.zeros(VERTICE_COUNT, dtype=Mesh.dtype)
mesh = mesh.Mesh(data, remove_empty_areas=False)

# The mesh normals (calculated automatically)
mesh.normals
# The mesh vectors
mesh.v0, mesh.v1, mesh.v2
# Accessing individual points (concatenation of v0, v1 and v2 in triplets)
mesh.points[0] == mesh.v0[0]
mesh.points[1] == mesh.v1[0]
mesh.points[2] == mesh.v2[0]
mesh.points[3] == mesh.v0[1]

mesh.save('new_stl_file.stl')

But now I am facing below error:

Traceback (most recent call last):
  File "/home/pi/checkstl.py", line 1, in <module>
    from stl import stl
  File "/usr/local/lib/python2.7/dist-packages/stl/__init__.py", line 2, in <module>
    import stl.ascii
ImportError: No module named ascii 

Can anybody please guide me on how do I resolve this error? Thank you

Wolph
  • 78,177
  • 11
  • 137
  • 148
Irfan Ghaffar7
  • 1,143
  • 4
  • 11
  • 30
  • It looks like a problem with your installation of numpy-stl. Can you run the first line, `from stl import mesh`? – ali_m Apr 16 '15 at 20:51
  • @ali_m I run it like this as well `from stl import mesh` but still same above error facing – Irfan Ghaffar7 Apr 16 '15 at 21:21
  • Something is broken about your installation then. Try removing and reinstalling numpy-stl, and see if any error messages appear during the installation. – ali_m Apr 16 '15 at 21:34

2 Answers2

3

This should be solved once you update numpy-stl. And more importantly, remove any other stl package - otherwise you have a clash with the module name. (The package numpy-stl is imported as import stl.)

If the package stl 0.0.3 is installed, uninstall it first:

pip uninstall stl

Then the package numpy-stl should work as expected (i.e. it can be used via import stl), as soon as it is installed:

pip install numpy-stl
coproc
  • 6,027
  • 2
  • 20
  • 31
Uvar
  • 3,372
  • 12
  • 25
  • 2
    Welcome to Stack Overflow! Please explain why your solution will solve the problem. An answer that just contains basic steps (even if they work) usually wont help the OP to understand their problem. – SuperBiasedMan Aug 10 '15 at 16:45
  • @SuperBiasedMan: the answer is actually correct, the author simply has two packages installed that clash in terms of namespace. PS: I'm the author of numpy-stl and this isn't the first person with the problem – Wolph Aug 13 '15 at 19:41
  • 2
    @Wolph needing to install a package `numpy-stl` for supporting a `import stl` is really confusing - and error prone, since there is also a package `stl`. The import for your package should be `import numpy-stl`. – coproc Mar 16 '18 at 13:30
  • @coproc that would result in an error, `numpy-stl` is not a valid import name ;) Ideally the abandoned `stl` package should be removed and replaced – Wolph Mar 16 '18 at 17:08
  • 1
    @Wolph right: `numpy-stl` is not a valid module name ... but there would be other options ... e.g. `numpy_stl`? – coproc Mar 17 '18 at 09:29
  • True, but if I would change it now it would break code for a lot of people. The new version of numpy-stl (which I released about a week ago) does warn when you have a conflicting package installed – Wolph Mar 17 '18 at 13:11
0

FWIW, you can do the same with meshio (which I authored), except that it works for a whole range of mesh formats.

pip install meshio
import meshio

mesh = meshio.read("input.stl")  # or .off, .vtk, .ply, ...
# mesh.points, mesh.cells, ...
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249