Hello everybody,

I would like to embed a mayavi scene in a wxPython application to 
represent the frame of a molecular dynamic trajectory (e.g. a 
(N,3)-numpy array where N is the number of atoms of the molecule). To 
keep simple, the application will send to the scene the frame. The frame 
can change in contents but also in size as the scene should also be able 
to represent a subpart of the molecule defined by indexes (a list of 
int). I wrote the following code:

class MolecularViewer(HasTraits):

    scene = Instance(MlabSceneModel, ())
   
    indexes = Instance(list)

    coords = Array(shape = (None,3))

    view = View(Item("scene", editor = SceneEditor(),resizable = True, 
show_label = False), resizable = True)

    def __init__(self, frame, indexes):
        HasTraits.__init__(self)
        self.frame = frame

    def set_indexes(self; indexes):
        self.indexes = sorted(indexes)

    @on_trait_change('scene.activated')
    def update_view(self):
        self.scene.malb.clf(figure = self.scene.mayavi_scene)

        x = self.coords[:,0]
        y = self.coords[:,1]
        z = self.coords[:,2]

        s = N.zeros(x.shape)

        self.pts = self.scene.mlab.quiver3d(x, y, z, figure = 
self.scene.mayavi_scene)
        src = ArraySource()
        self.scene.mayavi_scene.add_child(src)
        src.add_module(self.pts)       
  
    @on_trait_change('coords')
    def update_coords(self):
               
        x = self.coords[:,0]
        y = self.coords[:,1]
        z = self.coords[:,2]

        self.pts.mlab_source.set(x = x, y = y, z = z)

    @on_trait_change('indexes')
    def update_indexes(self):
        self.coords = self.frame.array[self.indexes,:]

    def _coords_default(self):
        coords = self.frame
        return coords

    def _indexes_default(self):
        indexes = range(0, len(self.frame))
        return indexes

And here is my problem: the code crash when I change the indexes with 
the following error:

Traceback (most recent call last):
  File 
"C:\Python27\lib\site-packages\enthought\traits\trait_notifiers.py", 
line 367, in call_2
    self.handler( object, new )
  File 
"C:\Python27\lib\site-packages\enthought\mayavi\tools\sources.py", line 
258, in _z_changed
    self.points[:,2] = z
ValueError: shape mismatch: objects cannot be broadcast to a single shape

It seems that once initialized, it is not possible to dynamically 
reallocate the size of the x, y z. Am I right ?
Do you see any hint to get around that problem ?

On a general point of view, is my design OK or did I misunderstand 
something with the mayavi/traits mechanism ?
In almost of examples I could find on the web, the constructor of the 
HasTraits subclass is never used. Can it be avoided in case such as mine 
where the data to be scened can change ? If no, can the use of setter 
such as my set_indexes method be avoided ?

thanks a lot guys

Eric





-- 
Eric Pellegrini
Calcul Scientifique
Institut Laue-Langevin
Grenoble, France


------------------------------------------------------------------------------
Simplify data backup and recovery for your virtual environment with vRanger. 
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Data protection magic?
Nope - It's vRanger. Get your free trial download today. 
http://p.sf.net/sfu/quest-sfdev2dev
_______________________________________________
MayaVi-users mailing list
MayaVi-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mayavi-users

Reply via email to