Darkgaze
Darkgaze

Reputation: 2573

Add user scripts folder to Maya

I tried all methods explained on several documentation pages.

I modified the userSetup.mel file and it adds the folder with this code:

string $s = `getenv MAYA_SCRIPT_PATH` + ";C:/MyScripts";
putenv "MAYA_SCRIPT_PATH" $s;

It didn't work. I also tried to do a "rehash" after doing putenv.

I removed that userSetup.mel file and modified the maya.env file using this other variable (since if I do this to MAYA_SCRIPT_PATH it breaks maya because overrides everything it had)

USER_SCRIPT_PATH = C:/MyScripts

Anything works when I do "import folder" on a python tab. Folder is inside MyScripts folder, and there's a init file for all of them. It's not a python error, this folder works under maya/scripts folder.

It's not very clear why USER_SCRIPT_PATH is not mentioned anywhere in the docs as an official variable, no information about why any of those works. The folder ends up on the environment variable using getenv on MEL, but code is not loaded.

Upvotes: 1

Views: 5775

Answers (4)

melMass
melMass

Reputation: 5209

List of available envs: http://www.toxik.sk/mayaenv-configuration-of-variables/

One thing to know is that any OS env vars takes over the Maya env ones. That's why I don't rely on it but use UserSetup.py.

userSetup.py triggers as soon as it can at maya startups (just before UI display).

For instance import pymel.core as pm will allow you to use pm.whatever during the maya session without having to import. To add custom python paths:

from os.path import expanduser
home = expanduser("~/.maya")
sys.path.append(os.path.join(home, "scripts"))
sys.path.append(os.path.join(home, "python", "lib"))

Just like you would in python. That would of course not work for mel scripts inside these folders but makes Maya's python aware of you custom modules and scripts.

Now if you want to execute maya commands from you userSetup you have to rely on the maya.utils.executeDeferred() for instance, part of mine:

def AkelianStartup():

    ###################
    # RENDER SETTINGS #
    ###################

    cmds.setAttr("defaultResolution.height", 720)  # Frame Height
    cmds.setAttr("defaultResolution.width", 1280)  # Frame Width
    cmds.setAttr("defaultResolution.deviceAspectRatio", 1.777)  # Aspect Ratio
    cmds.setAttr("defaultResolution.pixelAspect", 1)  # Pixel Aspect
    cmds.ToggleCurrentFrame()  # turns on the current frame hud

    ##################
    # DEFAULT SHADER #
    ##################

    _defaultLambert()

    ###############
    # DEFAULT CAM #
    ###############

    _defaultPersp(85)

##############################################################
# defer running command until everything is loaded and ready #
##############################################################
utils.executeDeferred('AkelianStartup()')

Upvotes: 0

theodox
theodox

Reputation: 12218

You don't want to set the environment variable after Maya is up and running, which is what you're doing in the mel example. You want it configured before maya starts looking for actual scripts. In general most programs that use env vars read them at startup time and don't recognize changes made while the program is running -- thats why in Windows for example you need to restart your dos windows after changing an env var from the GUI.

Popular methods are:

  1. Launch from a .bat file which sets the var and then runs maya. This lets you run multiple mayas side by side with different settings if needed
  2. Do it with maya.env; this puts all the info into one place
  3. For python, do it in userSetup.py. @AriGold's example will work, although I prefer to use the site module and site.addsitedir('path/to/someplace') because you can use .pth files in your target directory to make it as complex as you need; see this question for more details about pth files.
  4. use a Maya module which allows you to set not only script paths but all the other paths (bitmaps, plugins etc) in one go>

Upvotes: 0

DrWeeny
DrWeeny

Reputation: 2512

At the same place where you find userSetup.mel, you can create a userSetup.py and do whatever import at the beginning of maya or execute any script.

Upvotes: 0

Ari Gold
Ari Gold

Reputation: 1548

you can also add the path like:

import sys
sys.path.append("C:/MyScripts")

import my_module
reload(my_module)
my_module.run()

if you want that maya start with this env, you need a batch file(with all custom env paths like arnold or houdini engine or your own plugins) or a wrapper(more for pipelines with different maya setting for different departments like maya-modeling or maya-fx). this are the default env paths...

-MAYA_PLUG_IN_PATH
-MAYA_MODULE_PATH
-MAYA_SCRIPT_PATH
-USER_SCRIPT_PATH
-PYTHONPATH
-MAYA_SHELF_PATH
-XBMLANGPATH

Upvotes: 1

Related Questions