[vtkusers] Basic Animation Code

Bryan P. Conrad conrabp at ortho.ufl.edu
Thu Jan 7 11:19:01 EST 2010


Fred,

Yes the code runs on my machine, I am using WinXP, Python 2.5 - vtk
5.4.0 (from pythonxy).  I do not know much about using timers since my
first experience was yesterday, but I was able to create a similar
effect in python-vtk (5.0.4-1.1ubuntu) using the older CreateTimer,
DestroyTimer mechanism.  The timer is created the first time with the
argument 0 (VTKXI_TIMER_FIRST) and reset in the callback function using
1 (VTKI_TIMER_UPDATE).  The duration of the timer is set by passing an
argument when the vtkTimerCallback class is initialized.  As I
mentioned, I am new at this so I am not sure this is the best
implementation, but this version seems to work with both vtk 5.4 and
5.0.    

 

 

 

import vtk

 

class vtkTimerCallback():

   def __init__(self,duration,actor):

       self.timer_count = 0

       self.duration = duration

       self.actor = actor

 

   def execute(self,obj,event):

    iren = obj

    if self.timer_count < self.duration:

           print self.timer_count

           self.actor.SetPosition(self.timer_count/100.0,
self.timer_count/100.0,0);

           iren.CreateTimer(1)

           iren.GetRenderWindow().Render()

           self.timer_count += 1

    else:

        iren.DestroyTimer()

 

 

def main():

   #Create a sphere

   sphereSource = vtk.vtkSphereSource()

   sphereSource.SetCenter(0.0, 0.0, 0.0)

   sphereSource.SetRadius(5)

 

   #Create a mapper and actor

   mapper = vtk.vtkPolyDataMapper()

   mapper.SetInputConnection(sphereSource.GetOutputPort())

   actor = vtk.vtkActor()

   actor.SetMapper(mapper)

   prop = actor.GetProperty()

 

   # Setup a renderer, render window, and interactor

   renderer = vtk.vtkRenderer()

   renderWindow = vtk.vtkRenderWindow()

   #renderWindow.SetWindowName("Test")

 

   renderWindow.AddRenderer(renderer);

   renderWindowInteractor = vtk.vtkRenderWindowInteractor()

   renderWindowInteractor.SetRenderWindow(renderWindow)

 

   #Add the actor to the scene

   renderer.AddActor(actor)

   renderer.SetBackground(1,1,1) # Background color white

 

   #Render and interact

   renderWindow.Render()

 

   # Initialize must be called prior to creating timer events.

   renderWindowInteractor.Initialize()

 

   # Sign up callback to receive TimerEvent

   # The argument sets the timer duration and the actor to be animated

   cb = vtkTimerCallback(duration=250, actor=actor) 

   renderWindowInteractor.AddObserver('TimerEvent', cb.execute)

   renderWindowInteractor.CreateTimer(0)

 

   #start the interaction and timer

   renderWindowInteractor.Start()

 

 

if __name__ == '__main__':

   main()

   

 

-bryan

________________________________

From: vtkusers-bounces at vtk.org [mailto:vtkusers-bounces at vtk.org] On
Behalf Of Fred Pauling
Sent: Thursday, January 07, 2010 2:10 AM
To: vtkusers at vtk.org
Subject: Re: [vtkusers] Basic Animation Code

 

Hi Bryan,

 

Have you been able to run your Python example?

I ask this because the latest stable release of python-vtk
(5.0.4-1.1ubuntu) does not include a binding for the
CreateRepeatingTimer method of the vtkRenderWindowInteractor class.

If you can run it, how did you manage to find/create the bindings for
this method?

If you can't, can you suggest how one could work around this lack of
functionality to set a repeating timer in Python?

 

Cheers, 

Fred.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100107/40b5def1/attachment.htm>


More information about the vtkusers mailing list