在Python中从STL文件渲染二维图像

在Python中从STL文件渲染二维图像,python,matplotlib,3d,numpy-stl,Python,Matplotlib,3d,Numpy Stl,我想加载一个STL文件并生成一组不同旋转的2D图像 我根据示例获得了使用numpy stl的基础知识,最后得到了以下代码- from stl import mesh from mpl_toolkits import mplot3d from matplotlib import pyplot filename = '3001.stl' # Create a new plot figure = pyplot.figure() axes = figure.gca(projection='3d')

我想加载一个STL文件并生成一组不同旋转的2D图像

我根据示例获得了使用numpy stl的基础知识,最后得到了以下代码-

from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot

filename = '3001.stl'

# Create a new plot
figure = pyplot.figure()
axes = figure.gca(projection='3d')

# Load the STL files and add the vectors to the plot
mesh = mesh.Mesh.from_file(filename)

axes.add_collection3d(mplot3d.art3d.Poly3DCollection(mesh.vectors, color='lightgrey'))
#axes.plot_surface(mesh.x,mesh.y,mesh.z)
# Auto scale to the mesh size
scale = mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)

#turn off grid and axis from display      
pyplot.axis('off')

#set viewing angle
axes.view_init(azim=120)

# Show the plot to the screen
pyplot.show()
这工作得很好,只是我最终得到了一个组件的轮廓,缺少很多细节。下面的图片是一块乐高积木

我试着突出边缘。但这对模型是如何创建的很敏感,这对我来说并不好

我希望通过添加灯光,阴影可以帮助添加缺少的细节,但我找不到一种方法来做到这一点


你知道如何在下面的代码中添加光源来创建阴影吗?

如果你觉得Mayavi没有帮助,你可以尝试一下它是用于图形/3D渲染应用程序的。我发现做这样简单的事情非常简单。

在厌倦了Mayavi的安装灾难后,我最终为此编写了自己的库。

你的代码应该是

import vtkplotlib as vpl
from stl.mesh import Mesh

path = "your path here.stl"

# Read the STL using numpy-stl
mesh = Mesh.from_file(path)

# Plot the mesh
vpl.mesh_plot(mesh)

# Show the figure
vpl.show()
如果希望砖块为蓝色,可以使用

vpl.mesh_plot(mesh, color="blue")

请注意,matplotlib不是三维光线跟踪器。事实上,它通常被描述为2.5D,因为它提供平面投影3D输出。对于您的应用程序,我可以想象mayavi更适合。感谢@ImportanceOfBeingErnest-我在其他地方读到过,并尝试安装mayavi,但无法使用pip安装;在尝试的过程中,我发现了光源对象和一堆样本,它们显示了一些相当不错的3d渲染效果,希望如此!同意,安装mayavi有点痛苦。但我可以想象,花更多的时间来运行它会更有效率,而不是在一个软件中找到解决办法,而这个软件只是有限的(关于真正的3D绘图);遗憾的是:-)我最终在c语言中完成了这项工作,基于此示例代码使用OpenGL-