1
"""The base class for all MayaVi modules.
4
# Author: Prabhu Ramachandran <prabhu_r@users.sf.net>
5
# Copyright (c) 2005-2008, Enthought, Inc.
8
# Enthought library imports.
9
from enthought.traits.api import List, Instance, Str
12
from enthought.mayavi.core.pipeline_base import PipelineBase
13
from enthought.mayavi.core.pipeline_info import PipelineInfo
14
from enthought.mayavi.core.common import exception
17
######################################################################
19
######################################################################
20
class Module(PipelineBase):
21
""" Base class for the Mayavi modules.
24
# The version of this class. Used for persistence.
27
# The ModuleManager associated with this module. A Module is
28
# always a child of a ModuleManager. When the module is added to
29
# the mayavi pipeline (as a child of the module manager), the
30
# module manager automatically sets this trait.
31
module_manager = Instance('enthought.mayavi.core.module_manager.ModuleManager',
34
# The (optional) components used by this module. NOTE: This is
35
# not pickled. It is the developers responsibility to setup the
36
# components when the component traits are set in the handler.
37
components = List(record=False)
40
icon = Str('module.ico')
42
# The human-readable type for this object
45
# Information about what this object can consume.
46
input_info = PipelineInfo(datasets=['any'])
48
# Information about what this object can produce.
49
output_info = PipelineInfo(datasets=['none'])
51
######################################################################
53
######################################################################
54
def __init__(self, **traits):
55
super(Module, self).__init__(**traits)
57
# Let the module setup its pipeline.
60
def __get_pure_state__(self):
61
d = super(Module, self).__get_pure_state__()
62
for x in ('module_manager', 'components'):
67
######################################################################
69
######################################################################
70
def setup_pipeline(self):
71
"""Override this method so that it *creates* the tvtk
74
This method is invoked when the object is initialized via
75
`__init__`. Note that at the time this method is called, the
76
tvtk data pipeline will *not* yet be setup. So upstream data
77
will not be available. The idea is that you simply create the
78
basic objects and setup those parts of the pipeline not
79
dependent on upstream sources and filters. You should also
80
set the `actors` attribute up at this point.
84
def update_pipeline(self):
85
"""Override this method so that it *updates* the tvtk pipeline
86
when data upstream is known to have changed.
88
This method is invoked (automatically) when the input fires a
89
`pipeline_changed` event.
91
raise NotImplementedError
93
def update_data(self):
94
"""Override this method so that it flushes the vtk pipeline if
97
This method is invoked (automatically) when any of the inputs
98
sends a `data_changed` event.
100
# By default, just invoke render and set data_changed.
101
self.data_changed = True
105
######################################################################
107
######################################################################
109
"""This is invoked when this object is added to the mayavi
110
pipeline. Note that when start is invoked, all the other
111
information for the pipeline should be already set.
116
# Setup event handlers.
117
self._setup_event_handlers()
119
# Setup the pipeline.
120
self.update_pipeline()
122
# Start the components.
124
for component in self.components:
129
# Call parent method to set the running state.
130
super(Module, self).start()
133
"""Invoked when this object is removed from the mayavi
139
# Teardown event handlers.
140
self._teardown_event_handlers()
142
# Stop the components.
143
for component in self.components:
146
# Call parent method to set the running state.
147
super(Module, self).stop()
149
def add_child(self, child):
150
"""This method intelligently adds a child to this object in
153
# Pass on the buck to our module_manager.
154
self.module_manager.add_child(child)
156
######################################################################
157
# `TreeNodeObject` interface
158
######################################################################
159
def tno_has_children(self, node):
160
""" Returns whether or not the object has children.
164
def tno_allows_children(self, node):
165
""" Returns whether chidren of this object are allowed or not.
169
def tno_get_children(self, node):
170
""" Gets the object's children.
174
######################################################################
175
# Non-public interface
176
######################################################################
177
def _change_components(self, old, new):
178
"""This method sets up the `components` trait and is typically
179
called from a handler for a particular component.
181
For example lets say you are using a `Actor` component and
182
have a `actor` trait on the module. The `_actor_changed`
183
method should setup the pipeline. Typically inside this
184
handler, you also want to change the module's `components`
185
trait. This method merely does that by removing the older
186
component and adding the new one and then updating the
187
pipeline just in case.
189
comp = self.components
194
self.update_pipeline()
196
def _setup_event_handlers(self):
197
mm = self.module_manager
199
mm.on_trait_change(self.update_pipeline, 'source')
200
src.on_trait_event(self.update_pipeline, 'pipeline_changed')
201
src.on_trait_event(self.update_data, 'data_changed')
203
def _teardown_event_handlers(self):
204
mm = self.module_manager
206
mm.on_trait_change(self.update_pipeline, 'source',
208
src.on_trait_event(self.update_pipeline, 'pipeline_changed',
210
src.on_trait_event(self.update_data, 'data_changed',
213
def _scene_changed(self, old_scene, new_scene):
214
for component in self.components:
215
component.scene = new_scene
216
super(Module, self)._scene_changed(old_scene, new_scene)
218
def _components_changed(self, old, new):
219
self._handle_components(old, new)
221
def _components_items_changed(self, list_event):
222
self._handle_components(list_event.removed, list_event.added)
224
def _handle_components(self, removed, added):
225
for component in removed:
229
for component in added:
230
if scene is not None:
231
component.scene = scene
235
def _visible_changed(self,value):
236
for c in self.components:
239
super(Module,self)._visible_changed(value)
241
def _menu_helper_default(self):
242
from enthought.mayavi.core.traits_menu import ModuleMenuHelper
243
return ModuleMenuHelper(object=self.module_manager)