Search code examples
pythonanimationmayavimayavi.mlab

animating with mayavi mlab.points3d


Hello everyone i am trying to animate some data using Mayavi mlab.points3d and having some issues. Here is my code:

import numpy as np
from mayavi import mlab

##Some lists to animate
px=np.arange(0,10000,1)
py=np.arange(0,50000,5)

##Animation function
def run(px,py):
        cc = mlab.gcf().scene.camera
        cc.position[-1] = 10
        T_max = len(px)
        delayer=40
        @mlab.animate(delay=delayer)
        def anim_loc():
            f = mlab.gcf()
            while True:
                for i in np.arange(0,T_max,1):
                    s=0.5
                    mlab.points3d(px[i],py[i],s,color=(0,0,0),opacity=1)
                    mlab.view(distance=50,azimuth=80,elevation=80)
                    print(px[i],py[i])
                    yield

        b=anim_loc()
run(px,py)
mlab.show()

When i execute this code the animation only lasts for like 40 frames or so and after that it freezes without any error or exception. When i run the animation multiple times the animation freezes at different frames, sometimes after 20 frames and sometimes even after 80 frames. I am hereby not sure whether it is because of the code i wrote, or the computer i use (which should be fast enough for such task) or it is a bug in Mayavi. I am using spyder 3.2.8 with anaconda navigator. I would be very pleased for any help :).


Solution

  • You have to change the data in the sources in your @mlab.animated function. You're calling the plotter function instead.

    Here's a reduced version of your example:

    import numpy
    from mayavi import mlab
    
    
    # data
    px=numpy.arange(0,10000,1)
    py=numpy.arange(0,50000,5)
    pz=numpy.zeros_like(px)
    s=0.5
    # render
    pts=mlab.points3d(px,py,pz)
    T_max = len(px)
    delayer=40
    @mlab.animate(delay=delayer)
    def anim_loc():
        for i in numpy.arange(2, T_max,500):
            _x = px[0:i]
            _y = px[0:i]
            _z = pz[0:i]
            pts.mlab_source.reset( x = _x, y = _y, z = _z, )
            yield
    
    anim_loc()
    mlab.show()