[PYTHON] [maya] How to operate / execute maya's userSetup.py

* Please be careful about the article
Here, it is the content of the investigation to find out "how it actually works" for the functions and operations that are used as usual.
By knowing the mechanism, we will grasp the advantages and disadvantages, prevent troubles that occur in the future, and develop it into an expanded usage method.
It is just a memo of the personal survey. Please note that there may be mistakes and misunderstandings.

Trigger for investigation

Recently (from June 2020) there have been some virus reports for maya. I think there are many people who have been affected. All of these ultimately follow the app behavior via data files. The script that runs at startup is userSetup.py. And userSetup.py is used as a method of normal personal / production environment setting even if it is not a virus story.

Until now, I have often focused on the side that uses it as a general operation, so I wanted to find out how userSetup.py works.

Result of investigation

I used to think of it as a black box "somehow", but it's a very simple mechanism.

sys.userSetup under the folder of path.I'm running py with an execfile.

is. This is all.

How it is written

The relevant .py files are located below.

C:\Program Files\Autodesk\Maya2008\Python\Lib\site-packages\maya\app\startup\basic.py
~
C:\Program Files\Autodesk\Maya2020\Python\Lib\site-packages\maya\app\startup\basic.py

If you extract the necessary parts, it will be as follows.

basic.py


def setupScriptPaths():
    # ....abridgement....
    
    # 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( os.path.dirname(scriptDir) )
    
    # User application dir (eg .../maya/scripts)
    appDir = cmds.internalVar( userAppDir=True )
    sys.path.append( os.path.join( appDir, 'scripts' ) )

def executeUserSetup():
    # ....abridgement....

    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 ) )	

These functions are called and executed from within basic.py (in recent versions batch.py gui.py).

basic.py


# ....abridgement....

# Set up sys.path to include Maya-specific user script directories.
setupScriptPaths()

if not os.environ.has_key('MAYA_SKIP_USERSETUP_PY'):
    # Run the user's userSetup.py if it exists
    executeUserSetup()

What about the existing maya sys.path?

The details depend on your environment, but the folders are roughly as follows.

1. 1. Script folder in module path (plural)
2. Maya's python-based base folder (plural)
3. 3. User environment script folder (plural)

As a result of checking in the Maya 2018 environment at hand, it is as follows. (Excluding special ones)

sys.path confirmation code


import sys
for path in sys.path:
    if not path:
        continue
    print (i)

Execution example


C:\Program Files\Autodesk\Maya2018\plug-ins\ATF\scripts
C:\Program Files\Autodesk\Maya2018\plug-ins\MASH\scripts
C:\Program Files\Autodesk\Maya2018\plug-ins\fbx\scripts
C:\Program Files\Autodesk\Maya2018\plug-ins\camd\scripts
C:\solidangle\mtoadeploy\2018\scripts
C:\Program Files\Autodesk\Maya2018\plug-ins\substance\scripts
C:\Program Files\Autodesk\Maya2018\plug-ins\xgen\scripts

C:\Program Files\Autodesk\Maya2018\bin\python27.zip
C:\Program Files\Autodesk\Maya2018\Python\DLLs
C:\Program Files\Autodesk\Maya2018\Python\lib
C:\Program Files\Autodesk\Maya2018\Python\lib\plat-win
C:\Program Files\Autodesk\Maya2018\Python\lib\lib-tk
C:\Program Files\Autodesk\Maya2018\bin
C:\Program Files\Autodesk\Maya2018\Python
C:\Program Files\Autodesk\Maya2018\Python\lib\site-packages

C:\Program Files\Autodesk\Maya2018\bin\python27.zip\lib-tk

C:/Users/rdj/Documents/maya/2018/prefs/scripts
C:/Users/rdj/Documents/maya/2018/scripts
C:/Users/rdj/Documents/maya/scripts

Finally. Part to be aware of

It seems that you need to pay attention to the following.

1.userSetup.The execution order of py is sys.in path order
2.Documents/maya/userSetup in the user folder such as scripts.py is not a special treatment
To the last sys.userSetup at path.Running alongside the others as one of the py
3. 3. A python script is running in execfile

The mechanism is open to the public for both good and problematic parts, which is amazing. .. ..

Recommended Posts

[maya] How to operate / execute maya's userSetup.py
How to operate NumPy
[Ubuntu] How to execute a shell script
How to execute commands in jupyter notebook
How to operate Linux from the console
How to run a Maya Python script
How to install pip, numpy in Autodesk MAYA
How to operate Linux from the outside Procedure
How to write a GUI using the maya command
Execute Python function from Powershell (how to pass arguments)
How to execute a command using subprocess in Python
How to operate Discord API with Python (bot registration)
How to operate Firefox with selenium on Windows Memo
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
Scraping 2 How to scrape
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to read PyPI
How to install pip
How to use Virtualenv
How to use numpy.vectorize
How to update easy_install
How to install archlinux
How to use pytest_report_header
How to restart gunicorn
How to install python
How to virtual host
How to debug selenium
How to use partial
How to use Bio.Phylo
How to read JSON
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to update Spyder
How to use IPython
How to install BayesOpt
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to grow dotfiles
How to use list []
How to use python-kabusapi
"How to count Fukashigi"
How to install Nbextensions
How to use OptParse
How to use return
How to install Prover9
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod