How to modify maya environment variables with python?

Hello!

I’m working on drag’n’drop installer for my plug-in which downloads and unpacks latest version to a custom folder and runs the plug-in. In order for Maya to see the plugin, I must add my path to the MAYA_PLUG_IN_PATH environment variable.

I’ve tried these methods

mel.eval(‘putenv “MAYA_PLUG_IN_PATH” “{0}”’.format(env))
os.environ[‘MAYA_PLUG_IN_PATH’] = env
os.system(“set MAYA_PLUG_IN_PATH=’{0}’”.format(env))

and all of them worked fine right after installation. However, after I restarted Maya, MAYA_PLUG_IN_PATH variable didn’t contain my custom path anymore and I got this error:

// Error: file: C:/Program Files/Autodesk/Maya2019/scripts/startup/autoLoadPlugin.mel line 35: Plug-in, “TEST_TOOLS.py”, was not found on MAYA_PLUG_IN_PATH. //

Is there a way to store my custom plugin path to MAYA_PLUG_IN_PATH? I really don’t want to use methods like creating custom Maya.env file, because it doesn’t make sence in terms of distribution.

Thank you!

1 Like

To answer the question you asked, the windows cmd prompt command would be setx, and that should give you a start for googling … but PLEASE don’t mess with my environment variables. It will make me unhappy. Use one of the methods below instead.


The simplest way to add a plugin to maya is to put it in this folder:
os.path.join(os.environ['MAYA_APP_DIR'], cmds.about(version=True), 'plug-ins')
Which evaluates to something like Documents/Maya/2022/plug-ins, but it should work for any OS. That folder might not already exist, so you may have to create it.

That folder is already on the plugin path, it’s easy to deal with, and people can easily recognize that a new plugin goes in the folder named plug-ins. And if somebody wants to uninstall your plugin, it’s in a logical place, and they can just delete it.

Sometimes it’s good to just keep it simple.


But if you need extra control, or if you’re including a bunch of scripts that go with your plugin, or if you just want to keep your stuff separate and clean, you may want to use a maya module file. A .mod file lets you tell maya some extra places to look for plug-ins, scripts, icons, etc … But you don’t have to deal with environment variables directly, and the filepaths can be relative.

So let’s say you want to make a module called myModuleName

You make a file here:
os.path.join(os.environ['MAYA_APP_DIR'], 'modules', 'myModuleName.mod')

And that file could contain something like this:

+ myModuleName any path/to/myModuleName
plug-ins: plug-ins
scripts: MYscripts
icons: extras/MYicons

This modfile says that you’re adding a module, calling it myModuleName, dealing with any version number, and looking for a folder path/to/myModuleName relative to the current .mod file. (Usually, I just put the folder right next to the .mod file on disk. So in that case, the first line would be + myModuleName any myModuleName)

Then inside that myModuleName folder, there will be a plug-ins folder that you’ve named plug-ins, a scripts folder you’ve named MYscripts, and an icons folder you’ve named extras/MYicons. (You can, of course, change those paths to whatever you want. I’m just giving them strange names for illustration purposes)

This is a pretty basic .mod file, and there’s a lot more you can do with it (like point to different folders for different versions of maya), but this should get you started.


3 Likes

Thanks for such a detailed answer! .mod file method is exactly what I was looking for!