vtkRenderWindowInteractor on OSX

The vtkRenderWindowInteractor class has some erratic behavior, on ubuntu I get a seg fault sometimes when interacting with the scene and pressing q at the same time. On OSX the rendering window doesn’t close unless another instance is created, eg:

import vtk
import time

print("---- Start")
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

iren.Initialize()
renWin.Render()
iren.Start()

print("---- Finalize")
renWin.Finalize()
time.sleep(2)

print("---- TerminateApp")
iren.TerminateApp()
# del renWin, iren  # no effect

# at this point the widown should be closed, but it's not.

ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.SetPosition(500,500)
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.Initialize()
renWin.Render()
iren.Start()  # THIS MAKES THE WINDOW CLOSE

time.sleep(2)

print("---- Exit")
# vtk version       : 9.2.5
# python version    : 3.10.8 (main, Nov 24 2022, 08:09:04) [Clang 14.0.6 ]
# python interpreter: /Users/mmusy/Software/miniconda3/bin/python
# system            : Darwin 21.6.0 posix x86_64

… i just found a way out:

import vtk
import time

print("---- Start (press q)")
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

iren.Initialize()
renWin.Render()
iren.Start()

print("---- TerminateApp")
iren.TerminateApp()
time.sleep(2)

print("---- Finalize")
renWin.Finalize()

iren.ProcessEvents()
time.sleep(2)

print("---- Exit")

this now works! it seems that event processing works differently on different os.

1 Like