Search code examples
pythondata-visualizationmayavi

Updating data of an Image Plane Widget


I am working on a visualization of different vector fields.

For this purpose I am using the Mayavi Library in Python 2.7 (I think), to create a Image Plane Widget (IPW) and a slider to change the data of the vector field while the visualization is open, but my IPW won't change.

It works if I render the IPW new each time the slider is changed, but that's not what I want.

Is there a way to change the data of an IPW while the program is running without rendering a new Plane each time?

I have written following code:

import numpy as np
from mayavi import mlab
from matplotlib.scale import scale_factory
from traits.api import HasTraits, Range, Instance, Array, \
    on_trait_change
from traitsui.api import View, Item, Group
from mayavi.core.pipeline_base import PipelineBase
from mayavi.core.ui.api import MayaviScene, SceneEditor, \
            MlabSceneModel

class Modell(HasTraits):
  p = Array
  n = Range(0, 9, 5)
  #p is a 4 dimensional Array p[10][20][20][20]
  scene = Instance(MlabSceneModel, ())
  plot = Instance(PipelineBase)

  @on_trait_change('n,scene.activated')
  def update_plot(self):
     self.src = mlab.pipeline.scalar_field(self.p[self.n])
     if self.plot is None:                 
        self.plot = self.scene.mlab.pipeline.image_plane_widget(self.src,
                        plane_orientation='z_axes',
                        slice_index=10,
                        vmin=0, vmax=120)
     else:
        '''here should be the update function, i tried using mlab_source.set(self.src=self.src)
        and all variations of expressions in the brackets but nothing worked.
        I also searched for functions in IPW itself but didn't find something that could help me.'''

  #The layout of the dialog created
  view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                 height=400, width=400, show_label=False),
            Group('_', 'n'),
            resizable=True,
            )

my_model = Modell(p=p)
my_model.configure_traits()

I tried updating the pipeline and updating the data with self.plot.update_pipeline() and self.plot.update_data() but this doesn't work either.


Solution

  • Ok I found the solution for my problem, the trick is to change the data directly through the pipeline. So in my code I just have to set the following command into the else segment:

    self.plot.parent.parent.scalar_data = self.p[self.n]