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

import os 

import sys 

import traceback 

import argparse 

 

from director import applogic 

from director import objectmodel as om 

from director import viewbehaviors 

from director import visualization as vis 

from director.timercallback import TimerCallback 

 

import PythonQt 

from PythonQt import QtCore, QtGui 

 

 

def _consoleAppExceptionHook(exc_type, exc_value, exc_traceback): 

    msg =  ''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)) 

    sys.stderr.write(msg) 

    ConsoleApp.exit(1) 

 

 

class ConsoleApp(object): 

 

    _startupCallbacks = [] 

 

    def __init__(self): 

        om.init() 

        self.objectModelWidget = None 

 

    @staticmethod 

    def start(enableAutomaticQuit=True): 

        ''' 

        In testing mode, the application will quit automatically after starting 

        unless enableAutomaticQuit is set to False.  Tests that need to perform 

        work after the QApplication has started can set this flag to False and 

        call quit or exit themselves. 

 

        In testing mode, this function will register an exception hook so that 

        tests will return on error code if an unhandled exception is raised. 

        ''' 

        if enableAutomaticQuit: 

            ConsoleApp.startTestingModeQuitTimer() 

 

        if ConsoleApp.getTestingEnabled() and not ConsoleApp.getTestingInteractiveEnabled(): 

            sys.excepthook = _consoleAppExceptionHook 

 

        def onStartup(): 

            for func in ConsoleApp._startupCallbacks: 

                try: 

                    func() 

                except: 

                    print traceback.format_exc() 

 

        startTimer = TimerCallback(callback=onStartup) 

        startTimer.singleShot(0) 

 

        result = ConsoleApp.applicationInstance().exec_() 

 

        if ConsoleApp.getTestingEnabled() and not ConsoleApp.getTestingInteractiveEnabled(): 

            print 'TESTING PROGRAM RETURNING EXIT CODE:', result 

            sys.exit(result) 

 

        return result 

 

 

    @staticmethod 

    def startTestingModeQuitTimer(timeoutInSeconds=0.1): 

        if ConsoleApp.getTestingEnabled() and not ConsoleApp.getTestingInteractiveEnabled(): 

            ConsoleApp.startQuitTimer(timeoutInSeconds) 

 

    @staticmethod 

    def startQuitTimer(timeoutInSeconds): 

        quitTimer = TimerCallback() 

        quitTimer.callback = ConsoleApp.quit 

        quitTimer.singleShot(timeoutInSeconds) 

 

    @staticmethod 

    def quit(): 

        ConsoleApp.applicationInstance().quit() 

 

    @staticmethod 

    def exit(exitCode=0): 

        ConsoleApp.applicationInstance().exit(exitCode) 

 

    @staticmethod 

    def applicationInstance(): 

        return QtCore.QCoreApplication.instance() 

 

    def showObjectModel(self): 

 

        if not self.objectModelWidget: 

            w = QtGui.QWidget() 

            l = QtGui.QVBoxLayout(w) 

            model = om.getDefaultObjectModel() 

            l.addWidget(model.getTreeWidget()) 

            l.addWidget(model.getPropertiesPanel()) 

            applogic.addShortcut(w, 'Ctrl+Q', self.quit) 

            self.objectModelWidget = w 

            self.objectModelWidget.resize(350, 700) 

 

        self.objectModelWidget.show() 

        self.objectModelWidget.raise_() 

        self.objectModelWidget.activateWindow() 

        return self.objectModelWidget 

 

    def createView(self, useGrid=True): 

        view = PythonQt.dd.ddQVTKWidgetView() 

        view.resize(600, 400) 

 

        applogic.setCameraTerrainModeEnabled(view, True) 

        if useGrid: 

            self.gridObj = vis.showGrid(view, parent='scene') 

 

        self.viewOptions = vis.ViewOptionsItem(view) 

        om.addToObjectModel(self.viewOptions, parentObj=om.findObjectByName('scene')) 

 

        applogic.resetCamera(viewDirection=[-1,-1,-0.3], view=view) 

        self.viewBehaviors = viewbehaviors.ViewBehaviors(view) 

        applogic._defaultRenderView = view 

 

        applogic.addShortcut(view, 'Ctrl+Q', self.quit) 

        applogic.addShortcut(view, 'F8', self.showPythonConsole) 

        applogic.addShortcut(view, 'F1', self.showObjectModel) 

 

        view.setWindowIcon(om.Icons.getIcon(om.Icons.Robot)) 

        view.setWindowTitle('View') 

 

        return view 

 

    @staticmethod 

    def showPythonConsole(): 

        applogic.showPythonConsole() 

 

    def setupGlobals(self, globalsDict): 

 

        quit = ConsoleApp.quit 

        exit = ConsoleApp.exit 

 

        globalsDict.update(locals()) 

        for arg in ['globalsDict', 'self']: 

            del globalsDict[arg] 

 

    @staticmethod 

    def getTestingArgs(dataDirRequired=False, outputDirRequired=False): 

 

      parser = argparse.ArgumentParser() 

      parser.add_argument('--testing', action='store_true', help='enable testing mode') 

      parser.add_argument('-d', '--data-dir', type=str, help='testing data directory', required=dataDirRequired) 

      parser.add_argument('-o', '--output-dir', type=str, help='output directory for writing test output', required=outputDirRequired) 

      parser.add_argument('-i', '--interactive', action='store_true', help='enable interactive testing mode') 

 

      args, unknown = parser.parse_known_args() 

      return args 

 

    @staticmethod 

    def getTestingDataDirectory(): 

        path = ConsoleApp.getTestingArgs(dataDirRequired=True).data_dir 

        if not os.path.isdir(path): 

            raise Exception('Testing data directory does not exist: %s' % path) 

        return path 

 

    @staticmethod 

    def getTestingOutputDirectory(outputDirRequired=True): 

        path = ConsoleApp.getTestingArgs().output_dir 

        if not os.path.isdir(path): 

            raise Exception('Testing output directory does not exist: %s' % path) 

        return path 

 

    @staticmethod 

    def getTestingInteractiveEnabled(): 

        return ConsoleApp.getTestingArgs().interactive 

 

    @staticmethod 

    def getTestingEnabled(): 

        return ConsoleApp.getTestingArgs().testing 

 

 

 

def main(globalsDict=None): 

 

    app = ConsoleApp() 

    app.showPythonConsole() 

    view = app.createView() 

    view.show() 

    view.raise_() 

    view.activateWindow() 

 

    if globalsDict is not None: 

        app.setupGlobals(globalsDict) 

        globalsDict.update(dict(view=view, app=app)) 

 

    app.start() 

 

 

if __name__ == '__main__': 

    main(globals())