wolph / numpy-stl

Simple library to make working with STL files (and 3D objects in general) fast and easy.
http://numpy-stl.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
628 stars 107 forks source link

saving ascii STL #212

Closed rvalluru285 closed 1 year ago

rvalluru285 commented 1 year ago

Hi Wolph -

Thanks for the great package to work with STL files. I am wondering if you could help with the issue I am having. I am trying to convert PLY file and save it as a STL ascii and I am using mode=stl.Mode.ASCII argument while saving the file. However, it throws an error saying <module 'stl' has no attribute 'Mode'>. Below is the code. Thanks in advance for the help.

for file in plyList: sName = file.split('') pName = sName[0] + '' + sName[1]

d = read_ply(file)
xyz = list(d.values())
pcs = pd.DataFrame(xyz[1])
faces = pd.DataFrame(xyz[2])
points = pcs[['x', 'y', 'z']]

## Create new mesh with normalized z-axis
m = o3d.geometry.TriangleMesh()
np_vertices = np.array(points)
np_triangles = np.array(faces).astype(np.int32)

m.vertices = o3d.utility.Vector3dVector(np_vertices)
m.triangles = o3d.utility.Vector3iVector(np_triangles)

bbox = o3d.geometry.AxisAlignedBoundingBox(
        min_bound=(-350, 650, 100),
        max_bound=(350, 1350, points['z'].max()))

i3 = o3d.geometry.TriangleMesh.crop(m, bbox)
abc= np.asarray(i3.vertices)
a_f = pd.DataFrame(abc, columns = ['x', 'y', 'z'])
mesh_inform= pd.DataFrame(np.asarray(i3.triangles), columns = ['v1', 'v2', 'v3'])
test = pd.merge(points, a_f, on = ['x', 'y', 'z'], how='inner')

## Separate vertices and associated faces and save file in STL format
vertices = np.asarray(test)
faces = np.asarray(mesh_inform)
newSTL = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))

for i, f in enumerate(faces):
    for j in range(3):
        newSTL.vectors[i][j] = vertices[f[j],:]
newSTL.save(f'binarySTL/{pName}.stl', mode=stl.Mode.ASCII)

AttributeError: module 'stl' has no attribute 'Mode'

wolph commented 1 year ago

There are a few possible causes for this, but they are all the same issue really. You are importing the wrong module :)

You can easily find out by doing:

import stl
print(stl)

That should show you where stl is loaded from. The two most likely options are:

  1. You have a file named stl.py in your local directory
  2. You have a different Python module installed that also offers a stl package that is conflicting with this one.

If it's issue number two than it's likely because you have the stl package installed. It's quite a common issue: https://github.com/wolph/numpy-stl/issues/47

In that case a pip uninstall stl should do the trick

rvalluru285 commented 1 year ago

Thank you of the suggestion. I will follow your suggestions to rectify it. In case, if I have a further query, I will let you know. Thank you for your response. Ravi