首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Python中从STL文件渲染2D图像

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

Stack Overflow用户
提问于 2018-07-25 06:39:33
回答 2查看 7K关注 0票数 3

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

我根据this示例了解了使用numpy-stl的基础知识,最后以以下代码结束:

代码语言:javascript
运行
复制
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()

这很好用,只是我最终得到了一个组件的轮廓,缺乏很多细节。下面的图片是一块乐高积木。

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

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

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

EN

回答 2

Stack Overflow用户

发布于 2019-08-15 04:18:48

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

你的代码应该是这样的

代码语言:javascript
运行
复制
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()

如果您希望砖块是蓝色的,可以将mesh_plot替换为

代码语言:javascript
运行
复制
vpl.mesh_plot(mesh, color="blue")
票数 6
EN

Stack Overflow用户

发布于 2019-06-19 04:03:39

如果你不觉得Mayavi有用,你可以试试Panda3D,它是为图形/3D渲染应用而设计的。我发现做这样简单的事情非常简单。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51508506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档