python-3.x - VTK 渲染在 PyQt 中无法按预期工作

标签 python-3.x qt rendering pyqt5 vtk

我需要在 PyQt 应用程序中集成 VTK 可视化。但是,当我将模型放入 QVTKRenderWindowInteractor 时,显示会显示一些不想要的透明效果(见下图)。无论我尝试加载什么,这都会发生在表面或点云上。

有没有办法在 QVTKRenderWindowInteractor 中实现正确的表示?

第一张图片是来自 vtk.vtkConeSource() 的锥体.

Cone; Left: Without QVTKRenderWindowInteractor. Right: With QVTKRenderWindowInteractor

第二张图片是cturtle.pcd PCL 测试中的点云。

Cturtle; Left: Without QVTKRenderWindowInteractor. Right: With QVTKRenderWindowInteractor

左:没有 QVTKRenderWindowInteractor。 右:使用 QVTKRenderWindowInteractor

我附上问题的示例代码以供复制。
这是没有Qt的代码:

#!/usr/bin/env python

import vtk
from vtk.util.colors import tomato

"""This simple example shows how to do basic rendering and pipeline creation."""

cone = vtk.vtkConeSource()
cone.SetResolution(8)

coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInputConnection(cone.GetOutputPort())

coneActor = vtk.vtkActor()
coneActor.SetMapper(coneMapper)
coneActor.GetProperty().SetColor(tomato)
coneActor.RotateX(30.0)
coneActor.RotateY(-45.0)

ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)


ren.AddActor(coneActor)
ren.SetBackground(0.1, 0.2, 0.4)

iren.Initialize()

ren.ResetCamera()
ren.GetActiveCamera().Zoom(1.5)
renWin.Render()

iren.Start()

这是 QVTKRenderWindowInteractor 中的等效显示:
#!/usr/bin/env python

import vtk
from PyQt5.QtWidgets import QApplication
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from vtk.util.colors import tomato

"""A simple example that uses the QVTKRenderWindowInteractor class."""


app = QApplication(['QVTKRenderWindowInteractor'])

cone = vtk.vtkConeSource()
cone.SetResolution(8)

coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInputConnection(cone.GetOutputPort())

coneActor = vtk.vtkActor()
coneActor.SetMapper(coneMapper)
coneActor.GetProperty().SetColor(tomato)
coneActor.RotateX(30.0)
coneActor.RotateY(-45.0)

ren = vtk.vtkRenderer()
widget = QVTKRenderWindowInteractor()
widget.GetRenderWindow().AddRenderer(ren)

ren.AddActor(coneActor)
ren.SetBackground(0.1,0.2,0.4)

widget.Initialize()

ren.ResetCamera()
ren.GetActiveCamera().Zoom(1.5)
widget.GetRenderWindow().Render()

widget.Start()

widget.show()
app.exec_()

最佳答案

尝试了几件事后,我得到了以下解决方案Merge Request .

我使用 QGLWidget 作为 QVTKRenderWindowInteractor 的基类,而不是 QWidget。这个改动是因为据说QWidget有时会导致渲染问题。

为此,我在导入 QVTKRenderWindowInteractor 之前放置了以下代码:

import vtk.qt
vtk.qt.QVTKRWIBase = "QGLWidget"

from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor

为了使用 QGLWidget,我必须安装以下软件包:

sudo apt-get install python3-pyqt5.qtopengl

关于python-3.x - VTK 渲染在 PyQt 中无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51357630/

相关文章:

c++ - 使用 Qt 组件编译 C++ 代码

c++ - 在 Qt 中声明元类型的问题

游戏绘制循环的 Javascript setInterval 运行不一致

c++ - OpenGL 批处理 : Why does my draw call exceed array buffer bounds?

c++ - 如何解决 mac 中的 qt5(未找到软件包)cmake 错误?

JavaFX Canvas appendSVG 路径渲染行为

python - 为什么 while 循环中的缩进会产生问题?

python - 如何从 Sklearn 管道中提取特征重要性

python-3.x - 您如何将 "Error in the pull function"消息抑制到标准输出?

python - Namedtuple '_source' 方法在使用 'exec' 引用后停用