[vtkusers] Animations...

Eric E. Monson emonson at cs.duke.edu
Mon May 23 10:42:43 EDT 2011


Hey Jack,

You can, indeed, just create one actor and then change the underlying data and force the scene to update. The key is to call Modified() on the data structure before calling Render() so the pipeline knows the data changed and will update the intermediate filters.

If you're working in Python you have the option of either changing coordinates (or some other info) in the VTK data structures themselves, or keeping around something like a Numpy array which is referenced by the VTK structures and just updating that. The latter method (changing the info in memory that the VTK structures reference) is usually faster.

Here's a little example I modified to show the two options. The BULK=True test just modifies the underlying data directly and is about 4x faster on my machine than BULK=False. (Sorry it's not very pretty, but hopefully it gets the point across. The differences are more dramatic between the two methods with a high vert count.)

-Eric

# =======================
import numpy
import vtk
import vtk.util.numpy_support as VN

# Number of diffusing vertices
num_verts = 100000

# If BULK == True, then changing point coordinates by changing
# 	underlying numpy array
# If BULK == False, then setting polydata point coordinates directly
BULK = True
# BULK = False

# Initial point coordinates
centers = numpy.random.rand(num_verts, 3)

# Comment this out if setting coordinates individually
if BULK:
	centers_vtk = VN.numpy_to_vtk(centers)

vert_ids = vtk.vtkIntArray()
vert_ids.SetNumberOfComponents(1)
vert_ids.SetNumberOfValues(num_verts)
vert_ids.SetName("vert_ids")

points = vtk.vtkPoints()
points.SetNumberOfPoints(num_verts)

verts = vtk.vtkCellArray()
verts.Allocate(verts.EstimateSize(1, num_verts), 1000)
verts.InsertNextCell(num_verts)

# Assign values to the various arrays

if BULK:
	points.SetData(centers_vtk)

for ii in range(num_verts):
	p = centers[ii,:]
	if not BULK:
		points.SetPoint(ii, p[0], p[1], p[2])
	vert_ids.SetValue(ii, ii)
	verts.InsertCellPoint(ii)


# Build the vtkPolyData
poly = vtk.vtkPolyData()
poly.SetPoints(points)
poly.SetVerts(verts)
poly.GetPointData().AddArray(vert_ids)
poly.GetPointData().SetActiveScalars('vert_ids')

# Generate a lookup table
lut = vtk.vtkLookupTable()
lut.SetValueRange(0.5, 1.0)
lut.SetSaturationRange(0.1, 1.0)
lut.SetHueRange(0.4,0.6)
lut.SetRampToLinear()
lut.Build()

mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(poly)
mapper.SetLookupTable(lut)
mapper.ScalarVisibilityOn()
mapper.SelectColorArray('vert_ids')
mapper.SetScalarRange(poly.GetPointData().GetScalars().GetRange())

actor = vtk.vtkActor()
actor.SetMapper(mapper)

ren = vtk.vtkRenderer()
ren.AddActor(actor)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(400,400)
iren = vtk.vtkRenderWindowInteractor()
istyle = vtk.vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(istyle)
iren.SetRenderWindow(renWin)
ren.ResetCamera()
renWin.Render()

# Diffuse points
for tt in range(100):
	
	# Update coordinate centers array
	centers += 0.002*numpy.random.randn(num_verts, 3)
	
	if not BULK:
		for ii in range(num_verts):
			points.SetPoint(ii, centers[ii,0], centers[ii,1], centers[ii,2])
	
	# This Modified() call is what tells the pipeline the
	# data has changed, so it needs to fire again for the new Render()
	poly.Modified()
	
	renWin.Render()

iren.Start()





On May 20, 2011, at 2:11 PM, Guest, Jackie wrote:

> First Post so bare with me;
> 
> I am animating a large number of actors over time using the python vtk wrapper...
> 
> like this...
> 
> while animate:
>     for actors
>          actor.setposition(x,y,z)
>     ren.render()
> 
> This seems to be the only way to reset the pipeline in this environment.  Is there a way
> to create one actor (say and unstructured grid)...change the point ids or modify the mapper
> to reset point locations then re-render the scene.  More speed is our goal.
> 
> Thanks...Jack 
> 
> _______________________________________________
> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110523/79bcefe3/attachment.htm>


More information about the vtkusers mailing list