|
| 1 | +""" Application of embedding a Mayavi scene in a TraitsUI: control the data |
| 2 | +that is displayed. |
| 3 | +
|
| 4 | +Derived from Gael Varoquaux's demo: |
| 5 | +http://docs.enthought.com/mayavi/mayavi/auto/example_mlab_interactive_dialog.html#example-mlab-interactive-dialog |
| 6 | +""" |
| 7 | +# Computation imports |
| 8 | +from numpy import linspace, pi, cos, sin |
| 9 | + |
| 10 | +# Traits and TraitsUI imports |
| 11 | +############################# |
| 12 | +from traits.api import HasTraits, Range, Instance, on_trait_change |
| 13 | +from traitsui.api import View, Item, HGroup |
| 14 | + |
| 15 | +# Mayavi imports |
| 16 | +################ |
| 17 | +# Mayavi model |
| 18 | +from mayavi.tools.mlab_scene_model import MlabSceneModel |
| 19 | +# TraitsUI editor for a MlabSceneModel |
| 20 | +from tvtk.pyface.scene_editor import SceneEditor |
| 21 | +# Style of the scene editor |
| 22 | +from mayavi.core.ui.mayavi_scene import MayaviScene |
| 23 | +# Storage of a module (renderer) |
| 24 | +from mayavi.core.api import PipelineBase |
| 25 | + |
| 26 | +def curve(n_mer, n_long): |
| 27 | + """ Data generation |
| 28 | + """ |
| 29 | + phi = linspace(0, 2*pi, 2000) |
| 30 | + return [ cos(phi*n_mer) * (1 + 0.5*cos(n_long*phi)), |
| 31 | + sin(phi*n_mer) * (1 + 0.5*cos(n_long*phi)), |
| 32 | + 0.5*sin(n_long*phi), |
| 33 | + sin(phi*n_mer)] |
| 34 | + |
| 35 | +class Visualization(HasTraits): |
| 36 | + meridional = Range(1, 30, 6) |
| 37 | + transverse = Range(0, 30, 11) |
| 38 | + scene = Instance(MlabSceneModel, ()) |
| 39 | + plot = Instance(PipelineBase) |
| 40 | + |
| 41 | + @on_trait_change('scene.activated') |
| 42 | + def create_plot(self): |
| 43 | + """ Wait before the scene is activated before trying to add data, |
| 44 | + modules, filers into it. |
| 45 | + """ |
| 46 | + x, y, z, t = curve(self.meridional, self.transverse) |
| 47 | + self.plot = self.scene.mlab.plot3d(x, y, z, t, colormap='Spectral') |
| 48 | + |
| 49 | + @on_trait_change('meridional,transverse') |
| 50 | + def update_plot(self): |
| 51 | + """ Recompute the data |
| 52 | + """ |
| 53 | + x, y, z, t = curve(self.meridional, self.transverse) |
| 54 | + self.plot.mlab_source.set(x=x, y=y, z=z, scalars=t) |
| 55 | + |
| 56 | + |
| 57 | + # the layout of the dialog created |
| 58 | + view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene), |
| 59 | + height=250, width=300, show_label=False), |
| 60 | + HGroup( |
| 61 | + '_', 'meridional', 'transverse', |
| 62 | + ), |
| 63 | + ) |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + visualization = Visualization() |
| 67 | + visualization.configure_traits() |
0 commit comments