Skip to content

Moving a particle in Mayavi2

March 10, 2011

I have explained in my previous post “how to plot a particle at specified position”.  If we need to move particle from one place to another we may also want to change the data of the plot and update the plot without having to recreate the entire visualization.

Instead of recreating entire visualization, mlab provide a very convenient way to change the data of an existing mlab visualization.

Consider simple example:

import numpy
from enthought.mayavi import mlab
mlab.figure(1, bgcolor=(0, 0, 0), size=(1300, 700))
mlab.clf()

x = numpy.array([1,1.2])
y = numpy.array([1,1.2])
z = numpy.array([1,1.2])
s = numpy.array([0.3,0.5])       

g=mlab.points3d(x,y,z)
ms =g.mlab_source

for i in range(10):
    y=y+0.1
    ms.set(y=y)     #updating y value

for i in range(10):
    z=z+0.1
    ms.set(z=z)     #updating z value
    
for i in range(10):
    x=x+0.1
    ms.set(x=x)     #updating x value

mlab.show()

The key here is that the g object above has a special attribute called mlab_source. This sub-object allows us to manipulate the points and scalars.

The only thing to keep in mind here is that the shape of x,y,z should not be changed. If multiple values have to be changed, you can use the set method of the mlab_source.

Particle moving video:

 

 

One Comment leave one →
  1. June 3, 2011 8:55 am

    I tried it, I like it very much. thank u !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: