| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
# Local imports. |
| 12 |
from attractors.model.i_model_3d import IModel3d |
| 13 |
from mayavi.core.ui.mayavi_scene import MayaviScene |
| 14 |
|
| 15 |
# Enthought library imports. |
| 16 |
from mayavi.tools.mlab_scene_model import MlabSceneModel |
| 17 |
from tvtk.pyface.scene_editor import SceneEditor |
| 18 |
|
| 19 |
from pyface.tasks.api import TraitsTaskPane |
| 20 |
from traits.api import Dict, Instance, List, on_trait_change, Str |
| 21 |
from traitsui.api import EnumEditor, HGroup, Item, Label, View |
| 22 |
|
| 23 |
|
| 24 |
class Plot3dPane(TraitsTaskPane): |
| 25 |
#### 'ITaskPane' interface ################################################ |
| 26 |
|
| 27 |
id = "example.attractors.plot_3d_pane" |
| 28 |
name = "Plot 3D Pane" |
| 29 |
|
| 30 |
#### 'Plot3dPane' interface ############################################### |
| 31 |
|
| 32 |
active_model = Instance(IModel3d) |
| 33 |
models = List(IModel3d) |
| 34 |
|
| 35 |
scene = Instance(MlabSceneModel, ()) |
| 36 |
|
| 37 |
view = View( |
| 38 |
HGroup( |
| 39 |
Label("Model: "), |
| 40 |
Item("active_model", editor=EnumEditor(name="_enum_map")), |
| 41 |
show_labels=False, |
| 42 |
), |
| 43 |
Item( |
| 44 |
"scene", |
| 45 |
editor=SceneEditor(scene_class=MayaviScene), |
| 46 |
show_label=False, |
| 47 |
), |
| 48 |
resizable=True, |
| 49 |
) |
| 50 |
|
| 51 |
#### Private traits ####################################################### |
| 52 |
|
| 53 |
_enum_map = Dict(IModel3d, Str) |
| 54 |
|
| 55 |
########################################################################### |
| 56 |
# Protected interface. |
| 57 |
########################################################################### |
| 58 |
|
| 59 |
#### Trait change handlers ################################################ |
| 60 |
|
| 61 |
@on_trait_change("active_model.points") |
| 62 |
def _update_scene(self): |
| 63 |
self.scene.mlab.clf() |
| 64 |
if self.active_model: |
| 65 |
x, y, z = self.active_model.points.swapaxes(0, 1) |
| 66 |
self.scene.mlab.plot3d(x, y, z, line_width=1.0, tube_radius=None) |
| 67 |
|
| 68 |
@on_trait_change("models[]") |
| 69 |
def _update_models(self): |
| 70 |
# Make sure that the active model is valid with the new model list. |
| 71 |
if self.active_model not in self.models: |
| 72 |
self.active_model = self.models[0] if self.models else None |
| 73 |
|
| 74 |
# Refresh the EnumEditor map. |
| 75 |
self._enum_map = dict((model, model.name) for model in self.models) |