vtkRenderWindow 未打开图形窗口

问题描述 投票:0回答:2

编译我的 python 代码后,vtkRenderWindow 不会打开窗口。

我找到了一个基本的 vtk 示例来解决问题,但我似乎找不到任何解决方案。

代码运行没有任何错误,但没有打开窗口来显示图像。

我在使用 matplotlib 时遇到了同样的问题,必须将后端更改为 TkAgg 才能显示任何图形。

感谢您的帮助!

import vtk
from vtk.util.colors import tomato

cylinder = vtk.vtkCylinderSource()
cylinder.SetResolution(8)

cylinderMapper = vtk.vtkPolyDataMapper()
cylinderMapper.SetInputConnection(cylinder.GetOutputPort())

cylinderActor = vtk.vtkActor()
cylinderActor.SetMapper(cylinderMapper)
cylinderActor.GetProperty().SetColor(tomato)
cylinderActor.RotateX(30.0)
cylinderActor.RotateY(-45.0)

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

ren.AddActor(cylinderActor)
ren.SetBackground(0.1, 0.2, 0.4)
renWin.SetSize(200, 200)


iren.Initialize()

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

python vtk
2个回答
0
投票

将 vtkRenderer 对象添加到 vtkRenderWindow 对象后,您正在修改它。另外,在将 vtkRenderWindow 对象设置为交互器对象后,您可以修改它。我建议保持秩序(一个接着一个) - 未经测试,需要 Mesa 10.6.5 或更高版本以及 OpenGL 3.2。或稍后:

# -*- coding: UTF-8 -*-

import vtk
from vtk.util.colors import tomato

if __name__ == '__main__':

  cylinder = vtk.vtkCylinderSource()
  cylinder.SetResolution(8)

  cylinderMapper = vtk.vtkPolyDataMapper()
  cylinderMapper.SetInputConnection(cylinder.GetOutputPort())

  cylinderActor = vtk.vtkActor()
  cylinderActor.SetMapper(cylinderMapper)
  cylinderActor.GetProperty().SetColor(tomato)
  cylinderActor.RotateX(30.0)
  cylinderActor.RotateY(-45.0)

  ren = vtk.vtkRenderer()
  ren.AddActor(cylinderActor)
  ren.SetBackground(0.1, 0.2, 0.4)
  ren.ResetCameraClippingRange()  #instead of ren.ResetCamera()
  #ren.GetActiveCamera().Zoom(1.5) #keep this out for a while (suspicious, because you request a zoom without doing something with it)

  renWin = vtk.vtkRenderWindow()
  renWin.AddRenderer(ren)
  renWin.SetWindowName("some title") #just a bonus
  renWin.SetSize(200, 200)

  #style = vtk.vtkInteractorStyleTrackballCamera() #later uncomment this for test
  iren = vtk.vtkRenderWindowInteractor()
  iren.SetRenderWindow(renWin)
  #iren.SetInteractorStyle(style) #later uncomment this for test
  iren.Initialize()

  renWin.Render()
  iren.Start()

0
投票

添加以下几行为我解决了同样的问题。

# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
© www.soinside.com 2019 - 2024. All rights reserved.