Julius
Julius

Reputation: 765

Changing number of points in mayavi animation

Here is simple animation script in Python Mayavi:

from mayavi import mlab
import numpy as np

alpha = np.linspace(0, 2*np.pi, 100)
beta = np.linspace(0, np.pi, 100)

x = np.sin(beta) * np.cos(alpha)
y = np.sin(beta) * np.sin(alpha)
z = np.cos(beta)

plt = mlab.points3d(x, y, z)

@mlab.animate(delay=100)
def anim():
    global x, y, z

    f = mlab.gcf()
    for _ in range(100):
        # x = np.concatenate((x, [np.random.random()]))
        # y = np.concatenate((y, [np.random.random()]))
        # z = np.concatenate((z, [np.random.random()]))

        x = 1.1 * x

        plt.mlab_source.set(x=x, y=y, z=z)
        f.scene.render()
        yield


anim()
mlab.show()

This runs well and the points move around. However, I would like to uncomment the np.concatenate lines such that the number of points changes during the animation... Mayavi do not seem to support this?

I think this limitation has to do with the efficiency of updating the plot, but I would like the above to work and do not mind any speed hits.

Any ideas?

I've tried simply replotting mlab.points3d(x, y, z) after a mlab.clf(), but then the animation doesn't show -- only the last frame.

Thank you in advance.

Upvotes: 1

Views: 191

Answers (1)

Felipe Lema
Felipe Lema

Reputation: 2718

You should use reset() instead of set() per the docs:

x, y = np.mgrid[0:3:1,0:3:1]
s = mlab.surf(x, y, np.asarray(x*0.1, 'd'),
            representation='wireframe')
# Animate the data.
fig = mlab.gcf()
ms = s.mlab_source
for i in range(5):
    x, y = np.mgrid[0:3:1.0/(i+2),0:3:1.0/(i+2)]
    sc = np.asarray(x*x*0.05*(i+1), 'd')
    ms.reset(x=x, y=y, scalars=sc)
    fig.scene.reset_zoom()

Upvotes: 2

Related Questions