Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

#$Id: mayaviActions.py 16 2009-11-16 21:44:21Z sarkiss $ 

 

#Customized from tvtk/plugins/scene/ui/actions.py by Sargis Dallakyan (sargis@scripps.edu) 

 

# Enthought library imports. 

from enthought.pyface.api import FileDialog, OK 

from enthought.pyface.action.api import Action 

from enthought.traits.api import Instance, Property, Str 

 

class MyAction(Action): 

    def __init__(self, frame): 

        self.frame = frame 

 

class SceneAction(MyAction): 

    """ Base class for actions that require a scene manager. """ 

 

    #### 'SceneAction' interface ############################################## 

 

    # The scene manager. 

    scene_manager = Property(Instance( 

        'enthought.tvtk.plugins.scene.i_scene_manager import ISceneManager' 

    )) 

 

    def _get_scene_manager(self): 

        """ Trait property getter. """ 

 

        from enthought.tvtk.plugins.scene.i_scene_manager import ( 

            ISceneManager 

        ) 

 

        return self.window.get_service(ISceneManager) 

 

 

 

class SaveScene(SceneAction): 

    """ An action that saves a scene to an image. """ 

 

    #### 'Action' interface ################################################### 

 

    name = 'Save Scene' 

 

    def perform(self, event): 

        """ Performs the action. """ 

 

        extensions = [ 

            '*.png', '*.jpg', '*.jpeg', '*.tiff', '*.bmp', '*.ps', '*.eps', 

            '*.tex', '*.rib', '*.wrl', '*.oogl', '*.pdf', '*.vrml', '*.obj', 

            '*.iv' 

        ] 

 

        wildcard = '|'.join(extensions) 

 

        dialog = FileDialog( 

            parent   = self.frame, 

            title    = 'Save scene to image', 

            action   = 'save as', 

            wildcard = wildcard 

        ) 

        if dialog.open() == OK: 

            scene = self.frame.mayaviEngine.scene 

            if scene is not None: 

                scene.save(dialog.path) 

 

        return 

 

 

class SaveSceneToImage(SceneAction): 

    """ An action that saves a scene to an image. """ 

 

    #### 'Action' interface ################################################### 

 

    # Name of the action. 

    name = 'Image' 

 

    #### 'SaveSceneToImage' interface ######################################### 

 

    # The save method name. 

    save_method = Str('save') 

 

    # The wildcard for the file dialog. 

    wildcard = Str("All files (*.*)|*.*") 

 

    ########################################################################### 

    # 'Action' interface. 

    ########################################################################### 

 

    def perform(self, event): 

        """ Perform the action. """ 

 

        dialog = FileDialog( 

            parent   = self.frame, 

            title    = 'Save scene to %s' % self.name, 

            action   = 'save as', 

            wildcard = self.wildcard 

        ) 

        if dialog.open() == OK: 

            scene = self.frame.mayaviEngine.scene 

            if scene is not None: 

                method = getattr(scene, self.save_method) 

                method(dialog.path) 

 

        return 

 

 

# These are all specific subclasses that save particular images. 

class SaveSceneToPNG(SaveSceneToImage): 

    name        = 'PNG Image' 

    save_method = 'save_png' 

    wildcard    = 'PNG images (*.png)|*.png|' \ 

                  'All files (*.*)|*.*' 

 

class SaveSceneToJPEG(SaveSceneToImage): 

    name        = 'JPEG Image' 

    save_method = 'save_jpg' 

    wildcard    = 'JPEG images (*.jpg)|*.jpg|' \ 

                  'JPEG images (*.jpeg)|*.jpeg|' \ 

                  'All files (*.*)|*.*' 

 

class SaveSceneToBMP(SaveSceneToImage): 

    name        = 'BMP Image' 

    save_method = 'save_bmp' 

    wildcard    = 'BMP images (*.bmp)|*.bmp|' \ 

                  'All files (*.*)|*.*' 

 

class SaveSceneToTIFF(SaveSceneToImage): 

    name        = 'TIFF Image' 

    save_method = 'save_tiff' 

    wildcard    = 'TIFF images (*.tif)|*.tif|' \ 

                  'TIFF images (*.tiff)|*.tiff|' \ 

                  'All files (*.*)|*.*' 

 

class SaveSceneToPS(SaveSceneToImage): 

    name        = 'PostScript bitmap Image' 

    save_method = 'save_ps' 

    wildcard    = 'PostScript bitmap images (*.ps)|*.ps|' \ 

                  'All files (*.*)|*.*' 

 

class SaveSceneToGL2PS(SaveSceneToImage): 

    name        = 'Vector PS/EPS/PDF/TeX' 

    save_method = 'save_gl2ps' 

    wildcard    = 'All files (*.*)|*.*|' \ 

                  'EPS files (*.eps)|*.eps|' \ 

                  'PS files (*.ps)|*.ps|' \ 

                  'PDF files (*.pdf)|*.pdf|' \ 

                  'TeX files (*.tex)|*.tex' 

 

class SaveSceneToRIB(SaveSceneToImage): 

    name        = 'RenderMan RIB file' 

    save_method = 'save_rib' 

    wildcard    = 'RIB files (*.rib)|*.rib|' \ 

                  'All files (*.*)|*.*' 

 

class SaveSceneToOOGL(SaveSceneToImage): 

    name        = 'GeomView OOGL file' 

    save_method = 'save_oogl' 

    wildcard    = 'OOGL files (*.oogl)|*.oogl|' \ 

                  'All files (*.*)|*.*' 

 

class SaveSceneToIV(SaveSceneToImage): 

    name        = 'OpenInventor file' 

    save_method = 'save_iv' 

    wildcard    = 'OpenInventor files (*.iv)|*.iv|' \ 

                  'All files (*.*)|*.*' 

 

class SaveSceneToVRML(SaveSceneToImage): 

    name        = 'VRML file' 

    save_method = 'save_vrml' 

    wildcard    = 'VRML files (*.wrl)|*.wrl|' \ 

                  'All files (*.*)|*.*' 

 

class SaveSceneToOBJ(SaveSceneToImage): 

    name        = 'Wavefront OBJ file' 

    save_method = 'save_wavefront' 

    wildcard    = 'OBJ files (*.obj)|*.obj|' \ 

                  'All files (*.*)|*.*' 

 

import sys 

from os.path import isfile 

 

 

 

# Local imports 

from enthought.mayavi.core.common import error, exception 

 

 

###################################################################### 

# `SaveVisualization` class. 

###################################################################### 

class SaveVisualization(MyAction): 

    """ An action that saves the current visualization. """ 

 

    tooltip       = "Save current visualization" 

 

    description   = "Save current visualization to a MayaVi2 file" 

    name = "Save Visualization" 

    ########################################################################### 

    # 'Action' interface. 

    ########################################################################### 

 

    def perform(self, event): 

        """ Performs the action. """ 

        wildcard = 'MayaVi2 files (*.mv2)|*.mv2|' + FileDialog.WILDCARD_ALL 

        dialog = FileDialog(parent=self.frame, 

                            title='Save MayaVi2 file', 

                            action='save as', wildcard=wildcard 

                            ) 

        if dialog.open() == OK: 

            self.frame.mayaviEngine.engine.save_visualization(dialog.path) 

 

###################################################################### 

# `LoadVisualization` class. 

###################################################################### 

class LoadVisualization(MyAction): 

    """ An action that loads a visualization from file. """ 

 

    tooltip       = "Load saved visualization" 

 

    description   = "Load saved visualization from a MayaVi2 file" 

    name = "Load Visualization" 

    ########################################################################### 

    # 'Action' interface. 

    ########################################################################### 

 

    def perform(self, event): 

        """ Performs the action. """ 

        wildcard = 'MayaVi2 files (*.mv2)|*.mv2|' + FileDialog.WILDCARD_ALL 

        parent = self.frame 

        dialog = FileDialog(parent=parent, 

                            title='Open MayaVi2 file', 

                            action='open', wildcard=wildcard 

                            ) 

        if dialog.open() == OK: 

            if not isfile(dialog.path): 

                error("File '%s' does not exist"%dialog.path, parent) 

                return 

            self.frame.mayaviEngine.engine.load_visualization(dialog.path) 

 

###################################################################### 

# `RunScript` class. 

###################################################################### 

class RunScript(MyAction): 

    """ An action that runs a mayavi script.   

     

    WARNING: this can be dangerous since the file runs execfile! """ 

 

    tooltip       = "Execute a Python script (typically a Mayavi script)" 

 

    description   = "Execute a Python script (typically a Mayavi script)" 

 

    ########################################################################### 

    # 'Action' interface. 

    ########################################################################### 

 

    def perform(self, event): 

        """ Performs the action. """ 

        wildcard = 'Python files (*.py)|*.py' 

        parent = self.frame 

        dialog = FileDialog(parent=parent, 

                            title='Open Python file', 

                            action='open', wildcard=wildcard 

                            ) 

        if dialog.open() == OK: 

            if not isfile(dialog.path): 

                parent.log.error("File '%s' does not exist"%dialog.path) 

                return 

 

            # Get the globals. 

            # The following code is taken from scripts/mayavi2.py. 

            g = sys.modules['__main__'].__dict__ 

            execfile(dialog.path, g, g) 

 

from enthought.pyface.action.api import MenuManager 

import wx 

ID_RUN = wx.NewId() 

def AddMayaviMenus(menu, frame): 

 

    runScript = RunScript(frame) 

    menu.Append(ID_RUN, "&Run Python Script") 

    frame.Bind(wx.EVT_MENU, runScript.perform, id=ID_RUN) 

    #mayaviMenu = MenuManager(SaveVisualization(frame), LoadVisualization(frame)) 

    #menu.AppendMenu(-1, "Mayavi", mayaviMenu.create_menu(frame)) 

    saveMenu = MenuManager(SaveSceneToPNG(frame), SaveSceneToJPEG(frame),SaveSceneToBMP(frame), SaveSceneToTIFF(frame), 

                           SaveSceneToPS(frame), SaveSceneToGL2PS(frame), SaveSceneToRIB(frame), SaveSceneToOOGL(frame), 

                           SaveSceneToIV(frame), SaveSceneToVRML(frame),  SaveSceneToOBJ(frame)) 

 

    menu.AppendMenu(-1, "S&ave Scene As", saveMenu.create_menu(frame))