1

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.

Darkgaze
  • 2,280
  • 6
  • 36
  • 59

4 Answers4

1

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
Ari Gold
  • 1,528
  • 11
  • 18
  • Any idea why Maya doesn't get anything new added on userScripts or maya.env using that USER_SCRIPT variable? – Darkgaze Sep 07 '16 at 08:07
  • what is the result/print of import os; print os.environ["USER_SCRIPT_PATH"]? – Ari Gold Sep 07 '16 at 08:26
  • The right one. The path I added. But the contents are not available... and there are __init__ files. The folder works from the scripts folder under maya, but not in any other path. – Darkgaze Sep 08 '16 at 08:25
  • @darkgaze probably because user setup is run way too late in the process to init anything you can laso edit the system variables in the OS itself. – joojaa Sep 21 '16 at 08:51
0

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.

DrWeeny
  • 2,487
  • 1
  • 14
  • 17
  • Good point about the .py file. But it doesn't give me an answer since what I'm asking is how to add a path and make maya recognize the files on that folder. – Darkgaze Sep 07 '16 at 08:06
  • in python, you don't need to add a global path. You do : import folderScript and then everyone in maya can use your package. – DrWeeny Sep 07 '16 at 08:36
  • I said in my question that importing doesn't work. It doesn't recognize the files on that path when I add it using userSetup. Maybe adding it with python to sys.path instead of the MAYA_SCRIPT_PATH would suffice? – Darkgaze Sep 08 '16 at 08:24
  • sys.path.append is the way I was thinking about. – DrWeeny Sep 08 '16 at 08:33
0

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>
Community
  • 1
  • 1
theodox
  • 12,028
  • 3
  • 23
  • 36
  • Thanks for the answer. As I mentioned, doing it with maya.env doesn't work. But I can't modify USER_SCRIPT_PATH, and that doesn't work. If I modify MAYA_SCRIPT_PATH, it removes everything in that variable, obviously – Darkgaze Sep 08 '16 at 08:22
  • userSetup doesn't work either. The problem is that Maya doesn't recognizes the vars. But these are variables inside maya. How would you change the variables BEFORE maya starts, as you mention? – Darkgaze Sep 08 '16 at 08:23
  • Something like this, in a BAT file: ```set MAYA_SCRIPT_PATH="c:/path/to/scripts" "C:/program files/autodesk/maya/2016/bin/maya.exe" ``` That's two lines, can't format it like that in here though – theodox Sep 09 '16 at 03:10
  • So if you execute that before executing maya, but not using userSetup... it does work? .... I shouldn't make people in my office execute a bat for now... I'd prefer to find an answer to this (how to make maya find the files on that folder): Probably executing the sys.path.append would be the best answer... – Darkgaze Sep 09 '16 at 07:12
  • if the bat file runs the `set` and then Maya, your paths will be set. you'll still run your userSetup as normal - but it will see the changes you made to the path in the env vars. – theodox Sep 19 '16 at 00:27
0

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

I know the answer is a bit more than the question... ¿ Is this solving your issue ?

melMass
  • 3,813
  • 1
  • 31
  • 30