Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animating triangular_mesh face color #482

Closed
RomeshA opened this issue Jan 26, 2017 · 2 comments
Closed

Animating triangular_mesh face color #482

RomeshA opened this issue Jan 26, 2017 · 2 comments

Comments

@RomeshA
Copy link

RomeshA commented Jan 26, 2017

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?

@RomeshA
Copy link
Author

RomeshA commented Jan 27, 2017

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 RomeshA closed this as completed Jan 27, 2017
@Zulex
Copy link

Zulex commented May 7, 2020

@RomeshA

Spend a day or two trying to fix this and was following the same example. Huge thanks to you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants