我计划用Python为raspberrypi编写一个程序,以导入3D STL图像。
为此,我在谷歌上搜索并找到了一个名为“numpy-stl”的Python库,它适合我的要求。我是按照网站的说明安装的
sudo pip install numpy-stl
然后尝试运行示例中给定的代码。
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')
但现在我面临以下错误:
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
任何人都可以指导我如何解决此错误吗? 谢谢你
FWIW,您可以对 meshio (我编写的)执行相同的操作,只不过它适用于整个网格格式。
pip install meshio
import meshio
mesh = meshio.read("input.stl") # or .off, .vtk, .ply, ...
# mesh.points, mesh.cells, ...
我刚刚发布了最快、最轻、最直观的处理 STL 文件的库。这就是所谓的
openstl
。查看针对 meshio、numpy-stl 等的性能基准此处。
pip install openstl
import openstl
import numpy as np
# Read
quad = openstl.read("quad.stl")
# Write
success = openstl.write("quad.stl", quad, openstl.format.binary) # Or openstl.format.ascii (slower but human readable)
# Rotate
rotation_matrix = np.array([
[0,-1, 0],
[1, 0, 0],
[0, 0, 1]
])
rotated_quad = np.matmul(rotation_matrix, quad.reshape(-1,3).T).T.reshape(-1,4,3)
# Translate
translation_vector = np.array([1,1,1])
quad[:,1:4,:] += translation_vector # Avoid translating normals
# Scaling
scale = 1000.0
quad[:,1:4,:] *= scale # Avoid scaling normals
# Convert triangles to vertices and faces
vertices, faces = openstl.convert.verticesandfaces(triangles)