I am very new to this so please excuse me if I don't have all the terminology right yet. I am using tvtk.StructuredGrid to visualize a grid, and I want to be able to modify the dataset and have the figure update. Here is a sample code:<br>
<br> ----------------------------------------------------<br><span style="font-family: courier new,monospace;">#!/usr/bin/python</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># -*- coding: utf-8 -*-</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">from numpy import empty</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">from enthought.tvtk.api import tvtk</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">from enthought.mayavi import mlab</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import numpy</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import time</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># Create the points array</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">def xyzToPts(x,y,z):</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # Reform into 2D array</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> pts = empty(z.shape + (3,), dtype=float)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> pts[...,0] = x</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> pts[...,1] = y</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> pts[...,2] = z</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> pts.shape = pts.size/3, 3</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return(x.shape,pts)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># -- Set initial mesh</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">x, y = numpy.mgrid[0:3:1,0:3:1]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">x.shape= x.shape + (1,)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">y.shape= y.shape + (1,)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">z= x*0.0</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># Create the 2D points array</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">(xsh,pts)= xyzToPts(x,y,z)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># Create the structured grid dataset</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">sg = tvtk.StructuredGrid(dimensions=xsh, points=pts)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">d = mlab.pipeline.add_dataset(sg)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">gx = mlab.pipeline.grid_plane(d)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># Iterate on performing smoothing passes and displaying the updated mesh</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">for i in range(4):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> time.sleep(1)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print "-- Iterating, i= ",i</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> x= x*0.75</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> y= y*0.75</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> z= z*0.75</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (xsh,pts)= xyzToPts(x,y,z)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # How to have the plot onscreen update? This doesn't work!</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #sg.point_data.set(scalars=pts)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> #sg.modified()</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # Creates a new dataset. Not what we want!</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> sg = tvtk.StructuredGrid(dimensions=xsh, points=pts)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> d = mlab.pipeline.add_dataset(sg)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> gx = mlab.pipeline.grid_plane(d)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># Launch the interactive viewer</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">mlab.show()</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">exit()</span><br>
----------------------------------------------------<br><br>The above code displays a new dataset each time the x,y,z points are changed. Instead I would like to modify the values of the current dataset and have the figure update to reflect the new values. I just can't get this to work. Can somebody please help?<br>
<br>Thanks,<br><br>Heath<br><br>