
#	module: maya.app.startup.basic
#
#	This module is always imported during Maya's startup.  It is imported from
#	both the maya.app.startup.batch and maya.app.startup.gui scripts
#

import maya.cmds as cmds
import maya.app, maya.app.commands
import sys, os.path, atexit

def setupScriptPaths():
	"""
	Add Maya-specific directories to sys.path
	"""
	
	# Per-version prefs scripts dir (eg .../maya8.5/prefs/scripts)
	#
	prefsDir = cmds.internalVar( userPrefDir=True )
	sys.path.append( os.path.join( prefsDir, 'scripts' ) )
	
	# Per-version scripts dir (eg .../maya8.5/scripts)
	#
	scriptDir = cmds.internalVar( userScriptDir=True )
	sys.path.append( scriptDir )
	
	# User application dir (eg .../maya/scripts)
	#
	appDir = cmds.internalVar( userAppDir=True )
	sys.path.append( os.path.join( appDir, 'scripts' ) )
	
def executeUserSetup():
	"""
	Look for userSetup.py in the search path and execute it in the "__main__"
	namespace
	"""
	try:
		for path in sys.path:
			scriptPath = os.path.join( path, 'userSetup.py' )
			if os.path.isfile( scriptPath ):
				import __main__
				execfile( scriptPath, __main__.__dict__ )
	except Exception, e:
		sys.stderr.write( "Failed to execute userSetup.py\n" )
		sys.stderr.write( str( e ) )	
	
# Set up sys.path to include Maya-specific user script directories.
setupScriptPaths()

# Set up auto-load stubs for Maya commands implemented in libraries which are not yet loaded
maya.app.commands.processCommandList()

# Run the user's userSetup.py if it exists
executeUserSetup()

# Register code to be run on exit
atexit.register( maya.app.finalize )
