Python Modules and Maya


#1

Anyone deeply familiar with Maya.env & Python modules. I have an issue that has been plaguing me for a long time and I’ve tried so many options and nothing it working and I hope someone can help, I really do hope someone can help me, finally come to a resolution.


#2

If you want my help you will need to explain your “issue”. I’m good… but I’m not that good :stuck_out_tongue:

David


#3

Here goes nothing; I hope someone can finally come to a resolution and get all of this working. :banghead:

I have the MAYA_APP_DIR in my system environment variable, which overrides the default installation path of Maya which typically exists in MyDocuments > Maya > etc (scripts, icons etc) in Windows

The system environment MAYA_APP_DIR points to a custom path, within that custom path typically exists, it does in my case and as it should for anyone, the Maya.env within Maya.env I have a custom scripts / python / icons, etc etc paths (plural for paths).

The python path variable as you know is PYTHONPATH=“CustomPath” (custom path is a placeholder for the actual path, the python path is not literally “custom path” without drive etc) The problem is none of the Python Scripts in my custom Python path work. What I realized after an alarm bell went off; was I removed a python modules from my system environment variable, not from the Maya.env file.

Before deleting the Python Modules path modules from my system environment variable, all python scripts in the custom path which Maya reads from the Maya.env file worked, but not any longer.

I’ve was then informed to create a userSetup.mel or userSetup.py, and place this line; python("import sys;sys.path.append(‘customPath’); within a userSetup.mel file, skip adding it to my system environment variable for more control. By placing the line mentioned previously, Maya would find the python modules and all my python scripts in my custom python path would work, that didn’t turn out. :banghead: :surprised

How do I link the python modules on my system to the custom python path in the Maya.env file, that is the problem to which appears to have no solution. Since, when I try to run any Python script in Maya, I get no procedure found, because Maya can’t find the python modules.


#4

You should post your exact command and the errors and stackTrace that you get. Otherwise we are just guessing.

I have MAYA_APP_DIR set to u:\maya
Maya 2016 prefs are in u:\maya\2016
Maya.env is in u:\maya\2016
Maya.env is empty file.
Standard maya scripts are in u:\maya\2016\scripts
userSetup.py is in u:\maya\2016\scripts

Inside userSetup.py I have something like this (note the forward slash here)

import sys
sys.path.append(‘u:/dev/pythonScripts’)

After starting maya I can import scripts that live in u:/dev/pythonScripts

David


#5

My MAYA_APP_DIR points to a preferences sub-folder, within the preferences sub-folder I have the following sub-folders

2015-x64
-prefs
-presets
-scripts
-user.tooltips

The dash means it’s a sub-folder. My custom scripts path (MAYA_SCRIPT_PATH) only contains MEL scripts, and that variable is set in the Maya.env file.

Red = All MEL scripts exist
Blue = All Python scripts exist
Yellow = Maya.env exists

All MEL scripts work when placed in the folder, Python Scripts on the other hand, as mentioned, don’t work and I get the following error when I try to run any Python script which is placed in the Blue path;
Error: ImportError: file <maya console> line 1: No module named <whatever_script> #

There is no other information I can supply that is everything in a nut shell, how things are setup etc. :shrug:


#6

Turn on stack-trace in the script editor history menu.
Then tell me the exact command that caused this error.

Also, try running this

sys.path

It will print a list of paths in the script editor. Look at the paths and try to find your ‘pythonscript’ folder. If it is not there, then you have found your problem. If it is there, then I need to see the exact command you used to run the script.

David


#7

My pythonscripts folder was not found; what could be the problem ?


#8

It depends how you are setting the pythonpath. There are many ways to do this.
In my opinion you should do it in userSetup.py using sys.path.append

Some questions…
Do you have a userSetup.py? Or do you have a userSetup.mel?
You should only have one or the other - not both.
What code do you have in userSetup?
Maybe you have a syntax error in userSetup?
Did you check for errors on startup ?

David


#9

I have one userSetup.mel file. This is what I have in my userSetup.mel file; python("import sys;sys.path.append(‘C:…\pythonscript’);

These are the following errors when starting Maya;

// Error: python("import sys;sys.path.append('C:\....\....\pythonscript');
 // 
// Error: "C:/..../..../scripts/userSetup.mel" line 1.93: Unterminated string. // 

#10

There is no need to use backslashes, even if you are on Windows.
You simply forgot the last quotation mark to terminate your string.
And I doubt that this will work at all:

sys.path.append('C:/..../..../pythonscript')

I suppose you mean something like this:

sys.path.append('./../../pythonscript')

#11

I changed the back slashes to forward slashes in the userSetup.mel file as well replaced one of the double quotes with single quotes as it now looks like this;

python("import sys;sys.path.append('C:/..../...../pythonscript');

Unfortunately I’m still getting the same error as before when Maya starts in the script editor :banghead:


#12

I dont know why you are using a userSetup.mel. These days userSetup.py is easier.

Your code is incorrect. The error said “unterminated string”. That means quotes do not match.

So assuming you want to keep using mel, you need to close the double quote that begins that embedded python command… And you are missing the final bracket. Here is how it should be…in mel…

python(“import sys;sys.path.append(‘C:/…/…/pythonscript’)”);

If you were to switch to userSetup.py it gets simpler… like this…

import sys
sys.path.append(‘C:/…/…/pythonscript’)

David


#13

python(“import sys;sys.path.append(‘C:/…/…/pythonscript’)”);

I see previously syntax error :surprised Those are fixed, but the same error continues when starting Maya ? :banghead:


#14

I decided to use a userSetup.py file instead of a userSetup.mel file and everything works. :keenly:

But I’m still getting the same error, I think it’s related to userSetup.mel, why it’s still loading the mel copy of userSetup, I have to find out.


#15

Rename userSetup.mel to something else like myOldUserSetup.txt and maya will not see it anymore.
Or just delete it :thumbsup:

David


#16

I don’t understand why the userSetup.mel didn’t work. Are they not one in the same ?

I assume I can completely eliminate the user of Maya.env and use strictly use the userSetup file for Python and Scripts path; as it seems more reliable ?


#17

One is MEL, the other is PYTHON. They have the same job to do, but the syntax is completely different. You can use either, but if you try to use both together, you will become confused and so will maya.

Both should work if you get the syntax right. Since is messy to launch python commands in a mel script, and, if you ever need to, it is easy to launch mel commands from a python script, I would recommend using userSetup.py

David


#18

I did get the syntax correct for MEL but it doesn’t work :banghead: I suppose I should stick with userSetup.py.

Is it the same command used to load a custom MEL script custom path as it was to load a PYTHON scripts custom path for userSetup.py ?

Side Note- You probably are familiar with NSUV do you run into the problem whereas you can only run the script once when starting Maya, then if you close the script and try to run it again, it won’t run ?


#19

Just saying “it doesnt work” is boring and unhelpful. The code I posted worked here. If something doesnt work, post the code AND the error (with stacktrace) and someone maybe able to tell you whats wrong. :shrug:

No. Custom mel scripts location is set with the MAYA_SCRIPT_PATH environment variable. Python scripts are usually set with sys.path (but there are other ways). Setting an env variable in userSetup.py uses a different command to setting sys.path.

userSetup.py example

import sys
import os

# mel scripts path
ENV = 'MAYA_SCRIPT_PATH'
paths = [os.environ.get(ENV, '')]
paths.insert(0, 'U:/work/mel/')
os.environ[ENV] = ';'.join(paths)

# python path
sys.path.insert(0, 'U:/work/python/')

I have never used that script. Post the command you are using PLUS the error (with stacktrace). :slight_smile:

David


#20

No. Custom mel scripts location is set with the MAYA_SCRIPT_PATH environment variable. Python scripts are usually set with sys.path (but there are other ways). Setting an env variable in userSetup.py uses a different command to setting sys.path.

userSetup.py example

import sys
import os

# mel scripts path
ENV = 'MAYA_SCRIPT_PATH'
paths = [os.environ.get(ENV, '')]
paths.insert(0, 'U:/work/mel/')
os.environ[ENV] = ';'.join(paths)

# python path
sys.path.insert(0, 'U:/work/python/')

I pasted the above code into my userSetup.py file, changing only the path for the MEL scripts path, and removing the MAYA_SCRIPT_PATH from my Maya.env environment variable; but none of my MEL scripts are working ?