def __startIfReady(self): """ Called internally to start the application. """ if self.started: return if self.gotWindow and self.gotP3DFilename: self.started = True # Now we can ignore future calls to startIfReady(). self.ignore('AppRunner_startIfReady') # Hang a hook so we know when the window is actually opened. self.acceptOnce('window-event', self.__windowEvent) # Look for the startup Python file. This might be a magic # filename (like "__main__", or any filename that contains # invalid module characters), so we can't just import it # directly; instead, we go through the low-level importer. # If there's no p3d_info.xml file, we look for "main". moduleName = 'main' if self.p3dPackage: mainName = self.p3dPackage.Attribute('main_module') if mainName: moduleName = mainName # Temporarily set this flag while we import the app, so # that if the app calls run() within its own main.py, it # will properly get ignored by ShowBase. self.initialAppImport = True # Python won't let us import a module named __main__. So, # we have to do that manually, via the VFSImporter. if moduleName == '__main__': dirName = Filename(self.multifileRoot).toOsSpecific() importer = VFSImporter.VFSImporter(dirName) loader = importer.find_module('__main__') if loader is None: raise ImportError('No module named __main__') mainModule = loader.load_module('__main__') else: __import__(moduleName) mainModule = sys.modules[moduleName] # Check if it has a main() function. If so, call it. if hasattr(mainModule, 'main') and hasattr(mainModule.main, '__call__'): mainModule.main(self) # Now clear this flag. self.initialAppImport = False if self.interactiveConsole: # At this point, we have successfully loaded the app. # If the interactive_console flag is enabled, stop the # main loop now and give the user a Python prompt. taskMgr.stop()
def __startIfReady(self): """ Called internally to start the application. """ if self.started: return if self.gotWindow and self.gotP3DFilename: self.started = True # Now we can ignore future calls to startIfReady(). self.ignore('AppRunner_startIfReady') # Hang a hook so we know when the window is actually opened. self.acceptOnce('window-event', self.__windowEvent) # Look for the startup Python file. This might be a magic # filename (like "__main__", or any filename that contains # invalid module characters), so we can't just import it # directly; instead, we go through the low-level importer. # If there's no p3d_info.xml file, we look for "main". moduleName = 'main' if self.p3dPackage: mainName = self.p3dPackage.Attribute('main_module') if mainName: moduleName = mainName # Temporarily set this flag while we import the app, so # that if the app calls run() within its own main.py, it # will properly get ignored by ShowBase. self.initialAppImport = True __import__(moduleName) main = sys.modules[moduleName] if hasattr(main, 'main') and hasattr(main.main, '__call__'): main.main(self) # Now clear this flag. self.initialAppImport = False if self.interactiveConsole: # At this point, we have successfully loaded the app. # If the interactive_console flag is enabled, stop the # main loop now and give the user a Python prompt. taskMgr.stop()