File: scene_editor.py

package info (click to toggle)
mayavi2 4.8.3-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,892 kB
  • sloc: python: 49,447; javascript: 32,885; makefile: 129; fortran: 60
file content (105 lines) | stat: -rw-r--r-- 3,440 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
""" A TVTK scene editor. """

# Author: Prabhu Ramachandran <prabhu_r@users.sf.net>
# Copyright (c) 2005, Enthought, Inc.
# License: BSD Style.

import ast

# Enthought library imports.
from apptools.preferences.api import get_default_preferences
from tvtk.pyface.tvtk_scene import TVTKScene
from tvtk.pyface.api import DecoratedScene
from pyface.workbench.api import Editor
from traits.api import Instance


#### Handy functions ##########################################################

def _id_generator():
    """ Return an ever-increasing number useful for creating unique Ids. """

    n = 1
    while True:
        yield(n)
        n += 1

_id_generator = _id_generator()


class SceneEditor(Editor):
    """ A TVTK scene editor. """

    #### 'SceneEditor' interface ##############################################

    # The TVTK scene object.
    scene = Instance(TVTKScene)

    ###########################################################################
    # 'IWorkbenchPart' interface.
    ###########################################################################

    #### Trait initializers ###################################################

    def _id_default(self):
        """ Trait initializer. """

        return self.name

    def _name_default(self):
        """ Trait initializer. """

        return 'TVTK Scene %d' % (next(_id_generator))

    #### Methods ##############################################################

    def create_control(self, parent):
        """ Create the toolkit-specific control that represents the editor. """

        # We hold a reference to the scene itself to make sure it does not get
        # garbage collected (because we only return the scene's 'control' not
        # the scene itself). The scene is also referenced by the scene manager.
        self.scene = self._create_decorated_scene(parent)
        self.scene.render()

        return self.scene.control

    def destroy_control(self):
        """ Destroy the toolkit-specific control that represents the
        editor.
        """
        if self.scene is not None:
            # Close the scene to cleanly shut it down.
            self.scene.close()

        # Call the parent method.
        return super(SceneEditor, self).destroy_control()

    ###########################################################################
    # Private interface.
    ###########################################################################

    def _create_decorated_scene(self, parent):
        """ Create a new decorated scene. """

        pref = get_default_preferences()
        stereo = ast.literal_eval(pref.get('tvtk.scene.stereo'))

        scene = DecoratedScene(parent, stereo=stereo)

        # Set the scene's traits to preference values.
        scene.magnification = \
                ast.literal_eval(pref.get('tvtk.scene.magnification'))

        fg = ast.literal_eval(pref.get('tvtk.scene.foreground_color'))
        bg = ast.literal_eval(pref.get('tvtk.scene.background_color'))
        scene.foreground = fg
        scene.background = bg
        # FIXME: This seems necessary for some strange reason, if not
        # the actual background of the renderer never gets set even
        # though the renderer and the scene's background are synced.
        scene.renderer.background = scene.background

        return scene

#### EOF ######################################################################