Hi, I have a triangular mesh with data corresponding to face color, which I can plot using the solution from #253 e.g.
mesh = mlab.triangular_mesh(self.vertices[:,0], self.vertices[:,1], self.vertices[:,2], self.triangles,representation='wireframe',opacity=0)
mesh.mlab_source.dataset.cell_data.scalars = color
mesh.mlab_source.dataset.cell_data.scalars.name = 'Cell data'
mesh.mlab_source.update()
mesh2 = mlab.pipeline.set_active_attribute(mesh,cell_scalars='Cell data')
surf = mlab.pipeline.surface(mesh2)
However, I'm not sure how to then animate changes in the color. For instance,
@mlab.animate(delay=100)
def anim():
f = mlab.gcf()
for i in range(0,data.shape[0]):
mesh.mlab_source.dataset.cell_data.scalars = data[i,:]
mesh.mlab_source.dataset.cell_data.scalars.name = 'Cell data'
mesh.mlab_source.update()
mesh2 = mlab.pipeline.set_active_attribute(mesh,cell_scalars='Cell data')
mlab.pipeline.surface(mesh2)
yield
anim()
mlab.show()
does what I would like, but it slows down rapidly as the animation progresses and eventually is unusable. Most changes I try (e.g. removing the pipeline commands) result in the image not updating at all. Is there any other option to update the face colors?
I cleaned up the code a lot by creating a tvtk.PolyData
manually. The other big change however, is that the documentation suggests that the PolyData.modified()
method is what should be used to update the figure. However, this doesn't work for me - instead, I have to use the VTKDataSource.update()
method, where the VTKDataSource
wraps the PolyData
and thus needs to be found by traversing the surface parents
So the final working code was
from mayavi import mlab
from tvtk.api import tvtk
polydata = tvtk.PolyData(points=self.vertices, polys=self.triangles)
polydata.cell_data.scalars = np.ravel(data[:,0].copy())
polydata.cell_data.scalars.name = 'celldata'
mesh = mlab.pipeline.surface(polydata)
@mlab.animate(delay=10)
def anim():
for i in range(0,data.shape[0]):
polydata.cell_data.scalars = np.ravel(data[:,i].copy())
polydata.cell_data.scalars.name = "celldata"
# polydata.modified() # does not work
mesh.parent.parent.update() # works
yield
anim()
mlab.show()
@RomeshA
Spend a day or two trying to fix this and was following the same example. Huge thanks to you!
Most helpful comment
@RomeshA
Spend a day or two trying to fix this and was following the same example. Huge thanks to you!