Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

python - Animating a mayavi points3d plot

I'm trying to make a video of the trajectories of particles. However, somehow my scene never updates. Here's a very simple example:

from __future__ import absolute_import, division, print_function
from mayavi import mlab
import numpy as np
import math

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

xs = np.cos(alpha)
ys = np.sin(alpha)
zs = np.zeros_like(xs)

mlab.points3d(0,0,0)
plt = mlab.points3d(xs[:1], ys[:1], zs[:1])

@mlab.animate(delay=100)
def anim():
    f = mlab.gcf()
    while True:
        for (x, y, z) in zip(xs, ys, zs):
            print('Updating scene...')
            plt.mlab_source.x[0] = x
            plt.mlab_source.y[0] = y
            plt.mlab_source.z[0] = z
            f.scene.render()
            yield


anim()
mlab.show()

If I run this script, it shows a window with the two points and the animation GUI. It also prints a continous stream of "Updating Scene..." messages on the terminal. However, the scene doesn't show any movement at all.

What am I doing wrong?

Python 2.7, Mayavi 4.1, VTK 5.8

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just change to:

...

    for (x, y, z) in zip(xs, ys, zs):
        print('Updating scene...')
        plt.mlab_source.set(x=x, y=y, z=z)
        yield

...

you don't even need the f.scene.render(), according to documentation mlab_source.set guarantees the refresh.

Also since shape of your data doesn't change you don't need to use mlab_source.reset.

I also tested and works fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...