<s> """<STR_LIT>""" <EOL> try : <EOL> import vtk <EOL> except ImportError as m : <EOL> m . args = ( '<STR_LIT>' % <EOL> ( m . args [ <NUM_LIT:0> ] , '<STR_LIT:_>' * <NUM_LIT> ) , ) <EOL> raise <EOL> from traits . api import ( HasStrictTraits , List , Str , <EOL> Property , Instance , Event , HasTraits , Callable , Dict , <EOL> Bool , on_trait_change , WeakRef ) <EOL> from traitsui . api import View , Item <EOL> from apptools . persistence import state_pickler <EOL> from apptools . scripting . api import Recorder , recordable <EOL> from tvtk . common import is_old_pipeline <EOL> from mayavi . core . base import Base <EOL> from mayavi . core . scene import Scene <EOL> from mayavi . core . common import error , process_ui_events <EOL> from mayavi . core . registry import registry <EOL> from mayavi . core . adder_node import AdderNode , SceneAdderNode <EOL> from mayavi . preferences . api import preference_manager <EOL> from mayavi . core . ui . mayavi_scene import viewer_factory <EOL> def _id_generator ( ) : <EOL> """<STR_LIT>""" <EOL> n = <NUM_LIT:1> <EOL> while True : <EOL> yield ( n ) <EOL> n += <NUM_LIT:1> <EOL> scene_id_generator = _id_generator ( ) <EOL> def get_args ( function ) : <EOL> """<STR_LIT>""" <EOL> return function . __code__ . co_varnames [ : function . __code__ . co_argcount ] <EOL> class Engine ( HasStrictTraits ) : <EOL> """<STR_LIT>""" <EOL> __version__ = <NUM_LIT:0> <EOL> scenes = List ( Scene , record = True ) <EOL> children_ui_list = Property ( record = False ) <EOL> name = Str ( '<STR_LIT>' ) <EOL> current_scene = Property ( Instance ( Scene ) , record = False ) <EOL> current_object = Property ( record = False ) <EOL> current_selection = Property ( record = False ) <EOL> started = Event ( record = False ) <EOL> scene_factory = Callable ( viewer_factory ) <EOL> running = Bool ( False , record = False ) <EOL> closed = Event ( ) <EOL> recorder = Instance ( Recorder , record = False ) <EOL> _current_scene = WeakRef ( Scene , allow_none = True ) <EOL> _current_object = WeakRef ( HasTraits , allow_none = True ) <EOL> _current_selection = WeakRef ( HasTraits , allow_none = True ) <EOL> _viewer_ref = Dict <EOL> current_selection_view = View ( Item ( name = '<STR_LIT>' , <EOL> enabled_when = '<STR_LIT>' , <EOL> style = '<STR_LIT>' , springy = True , <EOL> show_label = False , ) , <EOL> resizable = True , <EOL> scrollable = True <EOL> ) <EOL> def __init__ ( self , ** traits ) : <EOL> super ( Engine , self ) . __init__ ( ** traits ) <EOL> preference_manager . root . on_trait_change ( self . _show_helper_nodes_changed , <EOL> '<STR_LIT>' ) <EOL> def __get_pure_state__ ( self ) : <EOL> d = self . __dict__ . copy ( ) <EOL> for x in [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> d . pop ( x , None ) <EOL> return d <EOL> def __set_pure_state__ ( self , state ) : <EOL> n_scene = len ( self . scenes ) <EOL> n_saved_scene = len ( state . scenes ) <EOL> for i in range ( n_scene - n_saved_scene ) : <EOL> self . close_scene ( self . scenes [ - <NUM_LIT:1> ] ) <EOL> for i in range ( n_saved_scene - n_scene ) : <EOL> self . new_scene ( ) <EOL> state_pickler . set_state ( self , state ) <EOL> def __getstate__ ( self ) : <EOL> return state_pickler . dumps ( self ) <EOL> def __setstate__ ( self , str_state ) : <EOL> self . __init__ ( ) <EOL> state = state_pickler . loads_state ( str_state ) <EOL> state_pickler . update_state ( state ) <EOL> self . __set_pure_state__ ( state ) <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> registry . register_engine ( self ) <EOL> self . started = self <EOL> self . running = True <EOL> def stop ( self ) : <EOL> registry . unregister_engine ( self ) <EOL> self . running = False <EOL> self . closed = True <EOL> @ recordable <EOL> def add_source ( self , src , scene = None ) : <EOL> """<STR_LIT>""" <EOL> passed_scene = scene <EOL> if scene is not None : <EOL> tvtk_scene = scene . scene <EOL> for sc in self . scenes : <EOL> if sc . scene == tvtk_scene : <EOL> scene = sc <EOL> break <EOL> else : <EOL> error ( '<STR_LIT>' ) <EOL> return <EOL> else : <EOL> scene = self . current_scene <EOL> if scene is None : <EOL> self . new_scene ( ) <EOL> scene = self . current_scene <EOL> scene . add_child ( src ) <EOL> self . current_object = src <EOL> @ recordable <EOL> def add_filter ( self , fil , obj = None ) : <EOL> """<STR_LIT>""" <EOL> passed_obj = obj <EOL> if obj is None : <EOL> obj = self . current_object <EOL> if not isinstance ( obj , Base ) : <EOL> msg = '<STR_LIT>' '<STR_LIT>' <EOL> error ( msg ) <EOL> return <EOL> if ( obj is not None ) and ( not isinstance ( obj , Scene ) ) : <EOL> if obj . running : <EOL> obj . add_child ( fil ) <EOL> self . current_object = fil <EOL> else : <EOL> msg = '<STR_LIT>' '<STR_LIT>' <EOL> error ( msg ) <EOL> else : <EOL> if obj is None : <EOL> error ( '<STR_LIT>' ) <EOL> else : <EOL> error ( '<STR_LIT>' ) <EOL> @ recordable <EOL> def add_module ( self , mod , obj = None ) : <EOL> """<STR_LIT>""" <EOL> self . add_filter ( mod , obj = obj ) <EOL> @ recordable <EOL> def save_visualization ( self , file_or_fname ) : <EOL> """<STR_LIT>""" <EOL> o = vtk . vtkObject <EOL> w = o . GetGlobalWarningDisplay ( ) <EOL> o . SetGlobalWarningDisplay ( <NUM_LIT:0> ) <EOL> try : <EOL> if is_old_pipeline ( ) : <EOL> state_pickler . dump ( self , file_or_fname ) <EOL> else : <EOL> state = state_pickler . get_state ( self ) <EOL> st = state . scenes [ <NUM_LIT:0> ] . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:4> ] <EOL> l_pos = st . seed . widget . position <EOL> st . seed . widget . position = [ pos . item ( ) for pos in l_pos ] <EOL> saved_state = state_pickler . dumps ( state ) <EOL> file_or_fname . write ( saved_state ) <EOL> except ( IndexError , AttributeError ) : <EOL> state_pickler . dump ( self , file_or_fname ) <EOL> finally : <EOL> o . SetGlobalWarningDisplay ( w ) <EOL> @ recordable <EOL> def load_visualization ( self , file_or_fname ) : <EOL> """<STR_LIT>""" <EOL> o = vtk . vtkObject <EOL> w = o . GetGlobalWarningDisplay ( ) <EOL> o . SetGlobalWarningDisplay ( <NUM_LIT:0> ) <EOL> try : <EOL> state = state_pickler . load_state ( file_or_fname ) <EOL> state_pickler . update_state ( state ) <EOL> for scene_state in state . scenes : <EOL> self . new_scene ( ) <EOL> scene = self . scenes [ - <NUM_LIT:1> ] <EOL> if scene . scene is not None : <EOL> scene . scene . disable_render = True <EOL> state_pickler . update_state ( scene_state ) <EOL> scene . __set_pure_state__ ( scene_state ) <EOL> scene . render ( ) <EOL> finally : <EOL> o . SetGlobalWarningDisplay ( w ) <EOL> @ recordable <EOL> def open ( self , filename , scene = None ) : <EOL> """<STR_LIT>""" <EOL> passed_scene = scene <EOL> reader = registry . get_file_reader ( filename ) <EOL> if reader is None : <EOL> msg = '<STR_LIT>' % filename <EOL> error ( msg ) <EOL> else : <EOL> src = None <EOL> if scene is None : <EOL> scene = self . current_scene <EOL> if scene is None : <EOL> scene = self . new_scene ( ) <EOL> try : <EOL> sc = scene . scene <EOL> if sc is not None : <EOL> sc . busy = True <EOL> callable = reader . get_callable ( ) <EOL> if reader . factory is None : <EOL> src = callable ( ) <EOL> src . initialize ( filename ) <EOL> else : <EOL> src = callable ( filename , self ) <EOL> if src is not None : <EOL> self . add_source ( src , passed_scene ) <EOL> finally : <EOL> if sc is not None : <EOL> sc . busy = False <EOL> if src is not None : <EOL> return src <EOL> def record ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> r = self . recorder <EOL> if r is not None : <EOL> r . record ( msg ) <EOL> def add_scene ( self , scene , name = None ) : <EOL> """<STR_LIT>""" <EOL> if name is None : <EOL> if hasattr ( scene , '<STR_LIT:name>' ) : <EOL> name = scene . name <EOL> else : <EOL> name = '<STR_LIT>' % next ( scene_id_generator ) <EOL> s = Scene ( scene = scene , name = name , parent = self ) <EOL> s . start ( ) <EOL> recorder = self . recorder <EOL> self . scenes . append ( s ) <EOL> self . current_scene = s <EOL> if recorder is not None : <EOL> recorder . register ( s ) <EOL> @ recordable <EOL> def remove_scene ( self , scene , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> s = None <EOL> for index , x in enumerate ( self . scenes ) : <EOL> if x . scene is scene : <EOL> s = x <EOL> break <EOL> if s is not None : <EOL> s . stop ( ) <EOL> self . scenes . remove ( s ) <EOL> recorder = self . recorder <EOL> if recorder is not None : <EOL> recorder . unregister ( s ) <EOL> if scene in self . _viewer_ref : <EOL> del self . _viewer_ref [ scene ] <EOL> if scene is self . _current_scene : <EOL> self . _current_scene = None <EOL> @ recordable <EOL> def new_scene ( self , viewer = None , name = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if viewer is None : <EOL> factory_kwargs = { } <EOL> factory_kwargs_names = get_args ( self . scene_factory ) <EOL> for arg , value in kwargs . items ( ) : <EOL> if arg in factory_kwargs_names : <EOL> factory_kwargs [ arg ] = value <EOL> viewer = self . scene_factory ( ** factory_kwargs ) <EOL> process_ui_events ( ) <EOL> if name is not None : <EOL> viewer . name = name <EOL> self . _viewer_ref [ viewer . scene ] = viewer <EOL> self . add_scene ( viewer . scene ) <EOL> if hasattr ( viewer , '<STR_LIT>' ) : <EOL> viewer . on_trait_change ( self . _on_scene_closed , '<STR_LIT>' ) <EOL> viewer . on_trait_change ( self . _on_scene_activated , '<STR_LIT>' ) <EOL> if hasattr ( viewer , '<STR_LIT:title>' ) : <EOL> self . current_scene . sync_trait ( '<STR_LIT:name>' , viewer , '<STR_LIT:title>' ) <EOL> return viewer <EOL> @ recordable <EOL> def close_scene ( self , scene ) : <EOL> """<STR_LIT>""" <EOL> viewer = self . get_viewer ( scene ) <EOL> self . remove_scene ( scene . scene ) <EOL> if hasattr ( scene , '<STR_LIT>' ) : <EOL> scene . close ( ) <EOL> elif scene . scene is not None : <EOL> scene . scene . close ( ) <EOL> if viewer is not None and hasattr ( viewer , '<STR_LIT>' ) : <EOL> viewer . close ( ) <EOL> def get_viewer ( self , scene ) : <EOL> """<STR_LIT>""" <EOL> return self . _viewer_ref . get ( scene . scene ) <EOL> def dialog_view ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def _on_select ( self , object ) : <EOL> """<STR_LIT>""" <EOL> self . current_selection = object <EOL> self . _current_object = object <EOL> try : <EOL> scene = object . scene <EOL> for s in self . scenes : <EOL> if s . scene == scene : <EOL> self . _current_scene = s <EOL> break <EOL> except AttributeError : <EOL> pass <EOL> def _get_current_scene ( self ) : <EOL> n_scene = len ( self . scenes ) <EOL> if n_scene == <NUM_LIT:0> : <EOL> return None <EOL> elif n_scene == <NUM_LIT:1> : <EOL> return self . scenes [ <NUM_LIT:0> ] <EOL> elif self . _current_scene is not None : <EOL> return self . _current_scene <EOL> elif n_scene > <NUM_LIT:1> : <EOL> return self . scenes [ - <NUM_LIT:1> ] <EOL> else : <EOL> return None <EOL> def _set_current_scene ( self , scene ) : <EOL> old = self . _current_scene <EOL> self . _current_scene = scene <EOL> self . trait_property_changed ( '<STR_LIT>' , old , scene ) <EOL> def _get_current_object ( self ) : <EOL> if self . _current_object is not None : <EOL> return self . _current_object <EOL> elif self . current_scene is not None : <EOL> return self . current_scene <EOL> else : <EOL> return None <EOL> def _set_current_object ( self , object ) : <EOL> old = self . _current_object <EOL> self . _current_object = object <EOL> self . trait_property_changed ( '<STR_LIT>' , old , object ) <EOL> def _get_current_selection ( self ) : <EOL> return self . _current_selection <EOL> def _set_current_selection ( self , object ) : <EOL> old = self . _current_selection <EOL> if not isinstance ( object , ( Base , AdderNode ) ) : <EOL> object = None <EOL> self . _current_selection = object <EOL> self . trait_property_changed ( '<STR_LIT>' , old , object ) <EOL> def _on_scene_closed ( self , obj , name , old , new ) : <EOL> self . remove_scene ( obj . scene ) <EOL> def _on_scene_activated ( self , obj , name , old , new ) : <EOL> for scene in self . scenes : <EOL> if scene . scene is obj . scene : <EOL> self . current_scene = scene <EOL> break <EOL> def _closed_fired ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _viewer_ref . clear ( ) <EOL> self . scenes = [ ] <EOL> preference_manager . root . on_trait_change ( self . _show_helper_nodes_changed , <EOL> '<STR_LIT>' , <EOL> remove = True ) <EOL> registry . unregister_engine ( self ) <EOL> def _show_helper_nodes_changed ( self ) : <EOL> self . trait_property_changed ( '<STR_LIT>' , [ ] , <EOL> self . children_ui_list ) <EOL> def _get_children_ui_list ( self ) : <EOL> """<STR_LIT>""" <EOL> if preference_manager . root . show_helper_nodes and len ( self . scenes ) == <NUM_LIT:0> : <EOL> return [ SceneAdderNode ( object = self ) ] <EOL> else : <EOL> return self . scenes <EOL> @ on_trait_change ( '<STR_LIT>' ) <EOL> def _trigger_children_ui_list ( self , old , new ) : <EOL> """<STR_LIT>""" <EOL> self . trait_property_changed ( '<STR_LIT>' , old , new ) <EOL> def _recorder_changed ( self , old , new ) : <EOL> if new is not None : <EOL> new . record ( '<STR_LIT>' ) <EOL> new . record ( '<STR_LIT>' ) <EOL> new . record ( '<STR_LIT>' ) <EOL> new . record ( '<STR_LIT>' ) <EOL> new . record ( '<STR_LIT>' ) <EOL> new . record ( '<STR_LIT>' ) <EOL> new . record ( '<STR_LIT>' ) <EOL> new . record ( '<STR_LIT>' ) <EOL> new . record ( '<STR_LIT>' ) <EOL> new . record ( '<STR_LIT>' ) <EOL> new . record ( '<STR_LIT>' ) <EOL> elif old is not None : <EOL> old . record ( '<STR_LIT>' ) <EOL> old . record ( '<STR_LIT>' ) <EOL> old . record ( '<STR_LIT>' ) </s>
