Package mrv :: Package cmd :: Module startup
[hide private]
[frames] | no frames]

Source Code for Module mrv.cmd.startup

  1  # -*- coding: utf-8 -*- 
  2  """Contains routines to startup individual programs""" 
  3  __docformat__ = "restructuredtext" 
  4   
  5  import sys 
  6  import os 
  7   
  8   
  9  #{ IPython  
 10   
11 -def ipython_apply_user_configuration():
12 """Run optional user scripts""" 13 # try to load custom settings 14 if "IMRV_CONFIG" in os.environ: 15 filepath = os.environ[ "IMRV_CONFIG" ] 16 try: 17 execfile( filepath ) 18 except Exception: 19 print "Failed to run configuration script" 20 else: 21 print "Set IMRV_CONFIG to point to python script doing additional setup"
22
23 -def ipython_setup_mrv():
24 """Initialize MRV 25 :return: True if it initializes maya, otherwise return False""" 26 # configure MRV 27 # as IPython is some sort of interactive mode, we load the user preferences 28 for var in ( 'MRV_STANDALONE_AUTOLOAD_PLUGINS', 29 'MRV_STANDALONE_INIT_OPTIONVARS', 30 'MRV_STANDALONE_RUN_USER_SETUP' ): 31 os.environ[var] = "1" 32 # END env var loop 33 34 # init maya - if possible 35 try: 36 import mrv.maya 37 return True 38 except (EnvironmentError, ImportError): 39 print "Didn't initialize Maya" 40 return False
41 # END handle maya import 42 43
44 -def ipython_setup(maya_support):
45 """Perform additional ipython initialization""" 46 import IPython 47 import logging 48 49 # init logging 50 logging.basicConfig(level=logging.INFO) 51 52 if maya_support: 53 # make default imports 54 ip = IPython.ipapi.get() 55 ip.ex("from mrv.maya.all import *") 56 57 58 # prefetch methods for convenience 59 import mrv.maya.nt.typ as typ 60 typ.prefetchMFnMethods()
61 # END handle maya 62 63 # } END initialization 64 65 66 #} END ipython 67 68 #{ Startup 69
70 -def mrv(args, info, args_modifier=None):
71 """Prepare the environment to allow the operation of maya 72 :param info: info module instance 73 :param args_modifier: Function returning a possibly modified argument list. The passed 74 in argument list was parsed already to find and extract the maya version. 75 Signature: ``arglist func(arglist, maya_version, start_maya, info) 76 If start_maya is True, the process to be started will be maya.bin, not the 77 python interpreter. If maya_version is 0, the process will continue execution 78 within this python interpreter which is assured to have mrv facilities availble 79 which do not require maya. 80 The last argument is the project's info module""" 81 import mrv.cmd 82 import mrv.cmd.base as cmdbase 83 84 # handle special arguments 85 config = [False, False, False] 86 lrargs = list(args) 87 for i, flag in enumerate((mrv.cmd.mrv_ui_flag, 88 mrv.cmd.mrv_mayapy_flag, 89 mrv.cmd.mrv_nomaya_flag)): 90 # intentionally don't cause an exception to be set - even if we catch 91 # it, its still hanging around and could be picked up by others who 92 # explicitly query it 93 if flag in lrargs: 94 lrargs.remove(flag) 95 config[i] = True 96 #END handle removal 97 # END for each flag to handle 98 start_maya, mayapy_only, no_maya = config 99 rargs = lrargs 100 101 if no_maya and ( start_maya or mayapy_only ): 102 raise EnvironmentError("If %s is specified, %s or %s may not be given as well" % (mrv.cmd.mrv_nomaya_flag, mrv.cmd.mrv_ui_flag, mrv.cmd.mrv_mayapy_flag)) 103 104 force_reuse_this_interpreter = False 105 if not no_maya: 106 force_reuse_this_interpreter, maya_version, rargs = cmdbase.init_environment(rargs) 107 else: 108 maya_version = 0.0 109 # EMD initialize maya if required 110 111 if args_modifier is not None: 112 rargs = list(args_modifier(tuple(rargs), maya_version, start_maya, info)) 113 else: 114 rargs = list(rargs) 115 # END handle arg list 116 117 if no_maya or (force_reuse_this_interpreter and not start_maya): 118 # parse the option ourselves, the optparse would throw on unknown opts 119 # Abort parsing of our options on the first options we didn't understand 120 # and interpret them as options belonging to the starting program 121 remaining_args = rargs[:] 122 eval_script = None 123 module = None 124 if len(rargs) > 1: 125 arg = rargs[0] 126 if arg == '-c': 127 eval_script = rargs[1] 128 elif arg == '-m': 129 module = rargs[1] 130 # END check options 131 132 if eval_script or module: 133 remaining_args = rargs[2:] 134 # END cut off our options 135 # END for each arg 136 137 # overwrite our own sys.args with our parsed arguments 138 arg0 = sys.argv[0] 139 del(sys.argv[:]) 140 sys.argv.extend([arg0] + remaining_args) 141 142 if eval_script: 143 exec(eval_script) 144 elif module: 145 __import__(module) 146 elif remaining_args and os.path.isfile(remaining_args[0]): 147 # if the first remaining arg is a file, execute it - all other args will 148 # be accessible too 149 fpath = remaining_args[0] 150 ext = os.path.splitext(fpath)[1] 151 152 # remove the filename, it wouldn't be availble if this was a real 153 # interpreter either 154 del(sys.argv[1:]) 155 remaining_args.pop(0) 156 sys.argv.extend(remaining_args) 157 158 # adjust important variables to fake a standalone start 159 global __name__, __file__ 160 __file__ = fpath 161 __name__ = '__main__' 162 163 if ext == ".py": 164 execfile(fpath) 165 elif ext in ('.pyc', '.pyo'): 166 import marshal 167 # If we have a pyc/pyo file, we mimic the python interpreter implementation 168 # to get a code object out of it. 169 bytes = open(fpath, 'rb').read() 170 # skip two longs: magic number and .py last modified date stamp 171 # unmashal the code object 172 code = marshal.loads(bytes[8:]) 173 exec code in globals() 174 else: 175 raise ValueError("Unsupported file format in %r" % fpath) 176 # END 177 178 elif not sys.stdin.closed: 179 # read everything until stdin is closed, and execute it 180 eval_script = sys.stdin.read() 181 exec(eval_script) 182 else: 183 # we actually never get here, but leave it just ... in case 184 raise EnvironmentError("Please specify '-c CMD' or '-m MODULE', or provide a file to indicate which code to execute") 185 # END handle flags 186 187 else: 188 if start_maya: 189 cmdbase.exec_maya_binary(rargs, maya_version) 190 else: 191 cmdbase.exec_python_interpreter(rargs, maya_version, mayapy_only)
192 # END handle process to start 193 # END handle flags 194
195 -def imrv():
196 """Get the main ipython system up and running""" 197 maya_support = ipython_setup_mrv() 198 199 # init ipython - needs to be available in your local python installation 200 try: 201 import IPython 202 except Exception, e: 203 raise ImportError("Warning: Failed to load ipython - please install it for more convenient maya python interaction: %s" % str(e)) 204 # END exception handling 205 206 ips = IPython.Shell.start() 207 ipython_setup(maya_support) 208 ipython_apply_user_configuration() 209 ips.mainloop()
210 211 #} END startup 212