Update mayavi plot in loop
<p>What I want to do is to update a mayavi plot in a loop. I want the updating of the plot to be done at a time specified by me (unlike, e.g., the animation decorator).</p>

<p>So an example piece of code I would like to get running is:</p>

<pre><code>import time
import numpy as np
from mayavi import mlab

V = np.random.randn(20, 20, 20)
s = mlab.contour3d(V, contours=[0])

for i in range(5):

    time.sleep(1) # Here I'll be computing a new V

    V = np.random.randn(20, 20, 20)

    # Update the plot with the new information
    s.mlab_source.set(scalars=V)
</code></pre>

<p>However, this doesn't display a figure. If I include <code>mlab.show()</code> in the loop, then this steals the focus and doesn't allow the code to continue.</p>

<p>I feel what I should be using is a <em>traits</em> figure (e.g. <a href="http://docs.enthought.com/mayavi/mayavi/building_applications.html" rel="nofollow">this</a>). I can follow the example traits application to run a figure which live-updates as I update the sliders. However, I can't get it to update when my code asks it to update; the focus now is 'stolen' by <code>visualization.configure_traits()</code>.</p>

<p>Any pointers, or a link to appropriate documentation, would be appreciated.</p>

<hr>

<p><strong>EDIT</strong></p>

<p>David Winchester's answer gets a step closer to the solution. </p>

<p>However, as I point out in the comments, I am not able to manipulate the figure with the mouse during the <code>time.sleep()</code> step. It is during this step that, in the full program, the computer will be busy computing the new value of V. During this time I would like to be able to manipulate the figure, rotating it with the mouse etc.</p>

<p>I thin Mayavi uses <code>generators</code> to animate data. This is working for me:</p>

<pre><code>import time
import numpy as np
from mayavi import mlab

f = mlab.figure()
V = np.random.randn(20, 20, 20)
s = mlab.contour3d(V, contours=[0])

@mlab.animate(delay=10)
def anim():
    i = 0
    while i &lt; 5:
        time.sleep(1)
        s.mlab_source.set(scalars=np.random.randn(20, 20, 20))
        i += 1
        yield

anim()
</code></pre>

<p>I used this post as reference ( <a href="http://stackoverflow.com/questions/14287185/animating-a-mayavi-points3d-plot">Animating a mayavi points3d plot</a> )</p>

python interactive mayavi