Hi,

I'm ccing the mailing lists since this response is likely to be of
general interest.

>>>>> "Massimo" == DiPierro, Massimo <[EMAIL PROTECTED]> writes:

    Massimo> First of all let me congratulate for developing mayavi. I
    Massimo> tried it and I am impressed.  I am the author of

Thanks for your compliments!

    Massimo> www.fermiqcd.net <http://www.fermiqcd.net/> and I am
    Massimo> starting a new project for visualization in lattice QCD.

Looks impressive!
 
    Massimo> I would very much like to use envisage and mayavi2. I am
    Massimo> new to vtk and mayavi so I am stuck with a problem and I
    Massimo> am hoping you could help me.

OK.
 
    Massimo> I am using the attached test program but my data source
    Massimo> (in the example heart.vtk) is periodically replaced by a
    Massimo> different file. How do I tell mayavi to periodically
    Massimo> reload the data from source without changing the other
    Massimo> parameters of the visualization? I am trying to implement
    Massimo> a real-time visualization of isosurfaces for the 3D Ising
    Massimo> model.
 
Attached is a script that is a bit too long since some of the
functionality needs to be elsewhere but it works.  Copy heart.vtk to
/tmp and then run the script like so:

 mayavi2 -x poll_file.py

Then when the mayavi window is still open, edit /tmp/heart.vtk --
change some scalars, for example change one of them to 600 and you
should see things update automatically.

The example is quite well documented that you should be able to follow
it easily.  I'll try and stick the Watcher class somewhere in
enthought.util.wx when I get the time so this is a little easier to
do out of the box.

regards,
prabhu

"""A simple script that polls a data file for changes and then updates
the mayavi pipeline.
"""
# Author: Prabhu Ramachandran <[EMAIL PROTECTED]>
# Copyright (c) 2006, Prabhu Ramachandran
# License: BSD Style.

#----------------------------------------------------------------------
# A simple timer subclass to poll the files without blocking the UI.
# This ought to be placed somewhere in enthought.util.wx.
import wx

# I don't like the name "Timer", nor do I like "Watcher", suggestions
# please.
class Watcher(wx.Timer):
    """Simple subclass of wx.Timer that allows the user to have a
    function called periodically.
    """
    def __init__(self, interval, callable, *args, **kw_args):
        """Initialize instance to invoke the action at each `interval`
        and call the given `callable` with given arguments and keyword
        args.
        """
        wx.Timer.__init__(self)
        self.callable = callable
        self.args = args
        self.kw_args = kw_args
        self.Start(interval)

    def Notify(self):
        """Overridden to call the given callable.
        """
        self.callable(*self.args, **self.kw_args)
#----------------------------------------------------------------------


import os
from enthought.mayavi.sources.vtk_file_reader import VTKFileReader
from enthought.mayavi.modules.outline import Outline
from enthought.mayavi.modules.contour_grid_plane import ContourGridPlane

fname = '/tmp/heart.vtk'

# The pipeline
d = VTKFileReader()
d.initialize(fname)
mayavi.add_source(d)

o = Outline()
mayavi.add_module(o)

c = ContourGridPlane()
mayavi.add_module(c)
c.grid_plane.position = 16
c.module_manager.scalar_lut_manager.show_scalar_bar = True

class Poller(object):
    def __init__(self, fname, data):
        self.fname = fname
        self.data = data
        self.last_stat = os.stat(fname)

    def poll_file(self):
        # Check the file
        s = os.stat(self.fname)
        if s[-2] == self.last_stat[-2]:
            return
    
        print "file changed"
        self.last_stat = s
        # Force the reader to re-read the file.
        d = self.data
        d.reader.modified()
        d.update()
        # Propagate the changes in the pipeline.
        d.data_changed = True

p = Poller(fname, d)
w = Watcher(1000, p.poll_file)

# The watcher needs to remain in scope if not it is GC'd and the
# polling stops.  So we inject it into "mayavi".  This is a hack for
# the situation where we run this using the mayavi2 -x <script.py>
# approach.  If this were run from within mayavi2 via the text editor
# then this is not necessary.
mayavi.watcher = w

# To stop polling the file do:
#mayavi.watcher.Stop()
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
MayaVi-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mayavi-users

Reply via email to