[vtkusers] vtkAnimationCue and Python

Milan Frank milan.frank at gmail.com
Tue Jun 27 13:42:12 EDT 2006


Hi,

I'm trying to employ vtkAnimationCue and vtkAnimationScene objects in
Python. Things seems to work fine except one detail. I do not know how
to get current time into my "Tick" method.

As far as I understand, In C++ we can derive a vtkAnimationCue
subclass. Thanks to late binding mechanism (virtual methods) our
overridden "Tick" method is called with proper current time as
parameter.

I gather, with Python wrappers virtual methods does not work (between
C++/Python). So, we have to use observer and register our "Tick"
method this way. Such method obviously do not receive current time as
parameter.

I'd like to have my code clear and use VTK mechanisms wherever
possible. Any ideas, please?

Thank you in advance for any suggestions,
Milan.

PS: My testing source code is in the attachment ...
-------------- next part --------------
import wx
import vtk

from vtk.wx.wxVTKRenderWindow import *


class MyCue(vtk.vtkAnimationCue):
  def __init__(self, sphere):
    self._sphere = sphere
    self.AddObserver("AnimationCueTickEvent", self.Tick)

  def Tick(self, ct, dt):
    print "Tick (%s)" % dt
    print "  ct: %s" % ct
    print "  self: %s" % self



class MyFrame(wx.Frame):
  def __init__(self, parent, title):
    wx.Frame.__init__(self, parent, -1, title)
    self.InitGui()
    self.CreatePipeline()
    self.DefineAnimation()

  def InitGui(self):
    self._renWin = wxVTKRenderWindow(self, -1)

  def CreatePipeline(self):
    self._sphere = vtk.vtkSphereSource()
    self._sphere.SetThetaResolution(100)
    self._sphere.SetPhiResolution(100)
    self._sphere.SetStartTheta(10)

    self._mapper = vtk.vtkPolyDataMapper()
    self._actor = vtk.vtkActor()
    self._renderer = vtk.vtkRenderer()

    self._mapper.SetInput(self._sphere.GetOutput())
    self._actor.SetMapper(self._mapper)

    self._renderer.AddActor(self._actor)

    self._renWin.GetRenderWindow().AddRenderer(self._renderer)

  def DefineAnimation(self):
    cue = MyCue(self._sphere)
    cue.SetStartTime(0.0)
    cue.SetEndTime(2.0)

    scn = vtk.vtkAnimationScene()
    scn.AddCue(cue)
    scn.SetStartTime(0.0)
    scn.SetEndTime(2.0)
    scn.SetModeToRealTime()


    print "animation start ..."

    scn.Play()
    scn.Stop()

    print "animation end ..."
    scn.SetAnimationTime(0.3)



#----------------------------------------
#  Main App class
class MyApp(wx.App):
  """
  Main application class.
  """
  def OnInit(self):
    frame = MyFrame(None, "Test app ...")
    frame.Show(True)
    return True

app = MyApp(redirect=False)
app.MainLoop()


More information about the vtkusers mailing list