在 Python 中从 STL 文件渲染 2D 图像

Yos*_*han 3 python 3d matplotlib 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')

# 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()
Run Code Online (Sandbox Code Playgroud)

这只适用于我最终得到组件的轮廓,缺少很多细节。下图是乐高积木... 乐高积木

我试图突出边缘。但这对模型的创建方式很敏感,这对我来说并不好。

带边的乐高积木

我希望通过添加照明,阴影可以帮助添加缺失的细节,但我找不到办法做到这一点。

知道如何将光源添加到下面的代码中以创建阴影吗?

pul*_*eth 6

在厌倦了 Mayavi 的安装灾难之后,我最终为此编写了自己的库。 https://github.com/bwoodsend/vtkplotlib

您的代码将类似于

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()
Run Code Online (Sandbox Code Playgroud)

如果你想让砖块变成蓝色,你可以用

vpl.mesh_plot(mesh, color="blue")
Run Code Online (Sandbox Code Playgroud)


L0t*_*tad 1

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