Ive got a problem with vtk (8.1), pyqt5 (5.10.1). If I use the vtkCallBackTimer with the "original" vtk.vtkRenderWindowInteractor() its all working fine. But if I use the vtk.qt.QVTKRenderWindowInteractor.QVTKRenderWindowInteractor() the callback function is executed successively with no pause. Here is the example:
import sys
from PyQt5 import QtWidgets
import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from mainwindow import Ui_MainWindow
class vtkTimerCallback():
def __init__(self):
self.timer_count = 0
def execute(self, obj, event):
print(self.timer_count)
self.actor.SetPosition(self.timer_count, self.timer_count, 0)
iren = obj
iren.GetRenderWindow().Render()
self.timer_count += 1
class ExampleApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super (ExampleApp, self).__init__(parent)
self.setupUi(self)
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(0.0, 0.0, 0.0)
sphereSource.SetRadius(5)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(sphereSource.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor # <-- WOKRING
renderWindowInteractor = QVTKRenderWindowInteractor(self.frame) # <-- NOT WORKING (self.frame is a QFrame in the mainwindow.ui)
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(actor)
renderer.SetBackground(1, 1, 1) # Background color white
renderWindow.Render()
renderWindowInteractor.Initialize()
cb = vtkTimerCallback()
cb.actor = actor
renderWindowInteractor.AddObserver('TimerEvent', cb.execute)
#****************
renderWindowInteractor.CreateRepeatingTimer(1000) #Pause between new function call in millisecs,
# with the QVTKRenderWindowInteractor the pause is ignored ?????
#****************
renderWindowInteractor.Start()
def main():
app = QtWidgets.QApplication(sys.argv)
trudeUI = ExampleApp()
trudeUI.show()
app.exec_()
if __name__ =='__main__':
main()
I hope anyone can help. I need the RepeatingTimer for an animation. If sb. has an alternative would be ok, but not the QtCore.QTimer, please. Should be a vtk-thing... I trying this for 4 hours now, any help is welcome...
Just had the same problem. As deep as I looked it appears to be a bug comming from QVTKRenderWindowInteractor
class.
While creating the repeating timer, the duration parameter (1000 in your example) is not taken into account and the default parameter (10) is used instead.
So to fix it I created a subclass MyQVTKRenderWindowInteractor
and overwrote the methods I needed:
class MyQVTKRenderWindowInteractor(QVTKRenderWindowInteractor):
def __init__(self, *arg):
super(MyQVTKRenderWindowInteractor, self).__init__(*arg)
self._TimerDuration = 10 # default value
def CreateTimer(self, obj, event):
self._Timer.start(self._TimerDuration) # self._Timer.start(10) in orginal
def CreateRepeatingTimer(self, duration):
self._TimerDuration = duration
super(MyQVTKRenderWindowInteractor, self).GetRenderWindow().GetInteractor().CreateRepeatingTimer(duration)
self._TimeDuration = 10
Now renderWindowInteractor = MyQVTKRenderWindowInteractor(self.frame)
should work.
Hopping answer has not come to late ++
来源:https://stackoverflow.com/questions/49786260/using-vtktimercallback-with-qvtkrenderwindowinteractor-not-working