1
2 """Contains routines to startup individual programs"""
3 __docformat__ = "restructuredtext"
4
5 import sys
6 import os
7
8
9
10
12 """Run optional user scripts"""
13
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
24 """Initialize MRV
25 :return: True if it initializes maya, otherwise return False"""
26
27
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
33
34
35 try:
36 import mrv.maya
37 return True
38 except (EnvironmentError, ImportError):
39 print "Didn't initialize Maya"
40 return False
41
42
43
45 """Perform additional ipython initialization"""
46 import IPython
47 import logging
48
49
50 logging.basicConfig(level=logging.INFO)
51
52 if maya_support:
53
54 ip = IPython.ipapi.get()
55 ip.ex("from mrv.maya.all import *")
56
57
58
59 import mrv.maya.nt.typ as typ
60 typ.prefetchMFnMethods()
61
62
63
64
65
66
67
68
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
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
91
92
93 if flag in lrargs:
94 lrargs.remove(flag)
95 config[i] = True
96
97
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
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
116
117 if no_maya or (force_reuse_this_interpreter and not start_maya):
118
119
120
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
131
132 if eval_script or module:
133 remaining_args = rargs[2:]
134
135
136
137
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
148
149 fpath = remaining_args[0]
150 ext = os.path.splitext(fpath)[1]
151
152
153
154 del(sys.argv[1:])
155 remaining_args.pop(0)
156 sys.argv.extend(remaining_args)
157
158
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
168
169 bytes = open(fpath, 'rb').read()
170
171
172 code = marshal.loads(bytes[8:])
173 exec code in globals()
174 else:
175 raise ValueError("Unsupported file format in %r" % fpath)
176
177
178 elif not sys.stdin.closed:
179
180 eval_script = sys.stdin.read()
181 exec(eval_script)
182 else:
183
184 raise EnvironmentError("Please specify '-c CMD' or '-m MODULE', or provide a file to indicate which code to execute")
185
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
193
194
196 """Get the main ipython system up and running"""
197 maya_support = ipython_setup_mrv()
198
199
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
205
206 ips = IPython.Shell.start()
207 ipython_setup(maya_support)
208 ipython_apply_user_configuration()
209 ips.mainloop()
210
211
212