[vtkusers] Basic Animation Code

Bryan P. Conrad conrabp at ortho.ufl.edu
Wed Jan 6 09:06:44 EST 2010


I have done some simple animations in a single thread in python.  The
tricky part seems to be that while the animation loop is running,
Windows will think that the RenderWindow is non-responsive and the
window will be eventually become blank (all white).  A work-around seems
to be to use the win32gui library and call the PeekMessage function
during each loop of the renderer.  I have only done this in python, so I
cannot tell you exactly how to implement the PeekMessage function in
C++, although the MSDN documentation should help:
http://msdn.microsoft.com/en-us/library/ms644943%28VS.85%29.aspx.

I admit that this method seems like a hack, so please let me know if
anyone knows of a better way!

 I have attached some sample code below (in python).

import vtk
import time
import win32gui

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.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()
    renderWindow.SetWindowName("Basic Animation")
    
    for i in range(10):
        prop.SetColor(i/10.0,0,0)  # Change the sphere color
	  actor.SetPosition(i-5,0,0) # Change the sphere position
        renderWindow.Render()	# Redraw the scene
        time.sleep(1) #Adjust to change the speed of animation
        win32gui.PeekMessage(0,0,0,0) # Keep window from non-responding
        
    renderWindowInteractor.Start()


if __name__ == '__main__':
    main()

____________________________
Bryan P. Conrad, Ph.D.
Senior Engineer
 
Department of Orthopaedics and Rehabilitation
University of Florida
PO Box 112727
Gainesville, FL 32611
Phone: 352.273.7412
Fax: 352.273.7407




-----Original Message-----
From: vtkusers-bounces at vtk.org [mailto:vtkusers-bounces at vtk.org] On
Behalf Of David Doria
Sent: Tuesday, January 05, 2010 11:29 PM
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] Basic Animation Code

On Wed, Dec 16, 2009 at 10:27 PM, Leon Danon <kernelblaha at gmail.com>
wrote:
> Hi All,
>
> I'm attempting (and failing) to run a simple animated model where the
> position and colour of an actor (in my case a cylider) changes, while
at the
> same time being rendered.
> I have done a bit of googling, but have come up with nothing very
useful. My
> understanding is that it should be done with threads (one for the
renderer,
> the other for the model).
> Does anyone have a simple example in C++ of how this would be
implemented.
> It would be greatly appreciated.
> Thanks
> Leon

Surely someone knows how to do this? Is an example of what you want
something like a loop to move an object across the scene?

pseudo code:

for(xposition = 1; xposition < 10; xposition++)
{
  actor.setposition(xposition,0,0);
  render scene
}

Can anyone translate to VTK?

Thanks,

David
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at:
http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers



More information about the vtkusers mailing list