Python Modules and Maya


#21

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

The scripts installation states;

  1. Unzip the files from the zip file to your Maya user script directory. This directory
    is NOT the same as your installation directory. Path to this directory:
    Windows: C:\Users\ %USER \Documents\maya\ %MAYA VERSION
    Mac: /users/ %USER /Library/Preferences/Autodesk/maya/ %VERSION /scripts

I put the two folders within the zip file, since it’s a Python script, in my custom python path, at present I get the error, when using the proper command to run the script; No Module named NSUV.

Since I’ve been doing clean up of my scripts, that is why that error has surfaced ?

I know the installation mentions the default installation scripts folder for Maya but, why can’t it work in a custom python folder ?

As well I can verify I had someone test the script in the default script folder as mentioned in the installation and it worked, but it only worked once, and that once was after restarting Maya, and running the script, thereafter if you closed the tool / script, and tried to run it again, it wouldn’t run, unless you restarted Maya ?

Two problem that I can’t make sense of.


#22

What does it mean if you run a command to a python script, and you get a “is not defined” error ? I looked up the error and came across a few solutions, but none work ?


#23

All I can say, again, is you have to show me your code. I know my code works because I ran it myself before posting. You have to post the contents of your userSetup.py if you want me to help.

Show me you actual code. Not something where you have modified things with … and so on. Just the actual code. This is the only way we can see your errors.

And you also have to post the error messages. If there were no error mesages then tell us that. But if there were, we need to see the actual messages.

With the information I have requested, the problem will be easy to solve.

David


#24

#Python Path
import sys
sys.path.append(‘C:…\pythonscript’)

#MEL Scripts Path
ENV = ‘MAYA_SCRIPT_PATH’
paths = [os.environ.get(ENV, ‘’)]
paths.insert(0, ‘C:…\scripts’)
os.environ[ENV] = ‘;’.join(paths)


#25

#Python Path
import sys
sys.path.append(‘C:…\pythonscript’)

#MEL Scripts Path
ENV = ‘MAYA_SCRIPT_PATH’
paths = [os.environ.get(ENV, ‘’)]
paths.insert(0, ‘C:…\scripts’)
os.environ[ENV] = ‘;’.join(paths)


#26

Why are using backslashes, , again?
In python you must either use forward slashes / or use two backslashes every time you use one, \.
But I recommend always just use forward slashes. /
Yesterday you even said you were using forward slashes… /
Use forward slashes.

David


#27

The output window gives me an error at this line; paths = [os.environ.get(ENV, ‘’)]. Must there be a path inserted after, ENV ?


#28

Maybe we should start from the very beginning.

What exactly does this path mean:

C:/.../..../pythonscript

Seems that my knowledge of windows is still incomplete. For me this makes no sense at all. Normally with a expression like this “…/” you go up one directory. So the path above tries to go at least two directories above “C:/”, is this possible? But I may be wrong.

Please enclose your code with the code tags (the ‘#’ sign in the message text editor). It makes code easier to read.

djx asked you to paste the error messages. You simply ignored this what is a bit rude. Without error messages we have to guess what’s wrong. In your last posting I suppose the error looks like this:

# Error: name 'os' is not defined
# Traceback (most recent call last):
#   File "<maya console>", line 7, in <module>
# NameError: name 'os' is not defined # 

Is this correct?

I’d recommend to make it a bit easier:

import os
os.environ["MAYA_SCRIPT_PATH"] += ";C:/somePath"

I’d recommend as well to use absolute paths first until you really know everthing works fine. Then you can swith to relative paths.

Before putting someting into userSetup.py try it from the script editor until you are sure that it works. This way you can exclude syntax problems easily.


#29

djx asked you to paste the error messages. You simply ignored this what is a bit rude. Without error messages we have to guess what’s wrong. In your last posting I suppose the error looks like this:

# Error: name 'os' is not defined
# Traceback (most recent call last):
#   File "<maya console>", line 7, in <module>
# NameError: name 'os' is not defined # 

Is this correct?

Yes, that is correct.

I’d recommend to make it a bit easier:

import os
os.environ["MAYA_SCRIPT_PATH"] += ";C:/somePath"

I’m using the exact code djx posted, the only thing I did was insert my paths. I never change anything else.

I’d recommend as well to use absolute paths first until you really know everthing works fine. Then you can swith to relative paths.

Does this relate to the forth point you made or is this different suggestion ?

Before putting someting into userSetup.py try it from the script editor until you are sure that it works. This way you can exclude syntax problems easily.

And so how can this be accomplished ?


#30

Yes, that is correct.

So the os module is missing. Add “import os”.

I’m using the exact code djx posted, the only thing I did was insert my paths. I never change anything else.

I did not say that anyone made something wrong. It was only a suggestion to make the code easier to read and maybe easier to understand.

Does this relate to the forth point you made or is this different suggestion ?

This is a general suggestion. It seems that you try to use relative paths (btw. you did not explain the c:/…/…/ thing), what often results in a wrong path because you may not exactly know what you starting directory is.

And so how can this be accomplished ?

You tried to extend the system variables for mel and for python to use your own scripts. That is not a problem. Let’s imagine you have a python script called pscript.py and it is located in a directory called “C:/userData/scripts/pythonscripts”
Then you can try the following in the maya script editor in a python panel:


import pscript

What will fail with a error message. Then try to extend the path with:

import sys
sys.path.append("C:/userData/scripts/pythonscripts")

Then try it again


import pscript

Now the script should be found and can be used.
Mel is similiar. If you have a mel script called melscript.mel in a directory called “C:/userData/scripts/melscripts” then you can exend the path in the script editor from a python tab with:

import os
os.environ["MAYA_SCRIPT_PATH"] += ";C:/userData/scripts/melscripts"

Now in a mel tab in the script editor try:

source "melscript";

And you should not get any error message.


#31

Dear @Falam,

You have two of the most helpful and expert people on this forum helping you on the second page already over a trivial problem. I don’t mean to be rude (but I’m gonna be, and it would help you :slight_smile: ), but you are kind of exhausting these two precious resources, so please try to concentrate, be more attentive, and follow instructions to the letter - no shortcuts, and no I know better. Same goes for replying to questions; double check if you answered everything that was asked. Most likely, you have a typo, and they are trying to help you find it. Don’t fight them.
Let’s go over Haggai’s last requests:

  1. He asked you what do you mean by “…”. Is this how you actually write a path, or do you mean it as a substitute to what’s really there? Either way see point 5, please.

  2. When you reply/post a message on the forum, you have icons above the textbox where you enter the message that help you format the message. One of them is ‘#’. Please use it for any piece of code/output that you write or copy/paste.

  3. Please don’t say ‘yes, it’s correct’, please paste your output even if it looks exactly the same (using ‘#’ from point 2).

  4. Try Haggai’s suggested alternative. Moreover, please paste your exact code (using ‘#’ from point 2), even if it looks exactly like David’s. It seems that you have dots and slashes problem. Also, elaborate which setup user scripts mel/python are currently in use (and paste the exact content of each).

  5. Please google (search) the difference between an absolute and relative path. In general: don’t use ‘…’ to point to previous directories in the tree. For that matter, use a path that doesn’t contain any dots.
    On the subject, don’t use backslashes as well (use foreslashes instead).

  6. “And so how can this be accomplished?” - What are you asking exactly? Do you know what is Maya’s script editor and how to use it (i.e. execute code from it)? If not, please google. Otherwise, what’s the question? Just run the contents of the user script and experiment from the script editor.
    BTW, you do know that the user scripts are loaded only once when Maya starts, and any change of them requires restarting Maya, right? Hence the suggestion to use the script editor for experimenting.


Moreover, to emphasize again a comment from before. Please provide as much information as you can, even if it seems redundant or stupid. Say exactly what isn’t working, give (again and again, after each update) the exact code script you are using and its output. No shortcuts. Also, please ask clear and detailed questions (see point 6).

EDIT: This posted before Haggai’s last reply.


#32

So the os module is missing. Add “import os”.

Add import os as such ?

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

#MEL Scripts Path
ENV = ‘MAYA_SCRIPT_PATH’
import OS
paths = [os.environ.get(ENV, ‘’)]
paths.insert(0, ‘C:/…/…/scripts’)
os.environ[ENV] = ‘;’.join(paths)

or

import OS
#MEL Scripts Path
ENV = ‘MAYA_SCRIPT_PATH’
paths = [os.environ.get(ENV, ‘’)]
paths.insert(0, ‘C:/…/…/scripts’)
os.environ[ENV] = ‘;’.join(paths)

Either method doesn’t work; the MEL scripts in the MEL scripts path are not being found ?

You tried to extend the system variables for mel and for python to use your own scripts. That is not a problem. Let’s imagine you have a python script called pscript.py and it is located in a directory called “C:/userData/scripts/pythonscripts”
Then you can try the following in the maya script editor in a python panel:


import pscript

What will fail with a error message. Then try to extend the path with:

import sys
sys.path.append("C:/userData/scripts/pythonscripts")

Then try it again

import pscript

Now the script should be found and can be used.

If the script is within it’s own folder, whatever the script name is, and that script folder is within the sub-folder pythonscripts, as in a case with a python script then the script is not found rather I get an error No module named .py found ?

# Error: line 1: No module named py
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# ImportError: No module named py # 

Mel is similiar. If you have a mel script called melscript.mel in a directory called “C:/userData/scripts/melscripts” then you can exend the path in the script editor from a python tab with:

import os
os.environ["MAYA_SCRIPT_PATH"] += ";C:/userData/scripts/melscripts"

Now in a mel tab in the script editor try:
source "melscript";
And you should not get any error message.

[/QUOTE]

Doing the steps you mentioned, with the exception I changed the path; I did get an error;

# Error: line 1: invalid syntax
#   File "<maya console>", line 2
#     source "scripts";
#                    ^
# SyntaxError: invalid syntax # 

#33

Lower case os

If the script is within it’s own folder, whatever the script name is, and that script folder is within the sub-folder pythonscripts, as in a case with a python script then the script is not found rather I get an error No module named .py found ?

It doesn’t matter in which directory which scripts are located. Simply add the directory to the path which contains the python scripts, that’s all.

# Error: line 1: No module named py
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# ImportError: No module named py # 

This time we have the error message but no code. From the error message I deduce that you tried to do something like this:

import py

Doing the steps you mentioned, with the exception I changed the path; I did get an error;

# Error: line 1: invalid syntax
#   File "<maya console>", line 2
#     source "scripts";
#                    ^
# SyntaxError: invalid syntax # 

Yes because you did not read my answer carefully:

Now in a mel tab in the script editor try:

And from this: source “scripts” I think that “scripts” is a directory. Correct? If so it is wrong, source works with files only, e.g. a file called mymelscript.mel can be sourced with:

source "mymelscript";


#34

The MEL scripts work, and so do the Python scripts. :thumbsup:

I have fallen in love with the userSetup file :bowdown:

It doesn’t matter in which directory which scripts are located. Simply add the directory to the path which contains the python scripts, that’s all.

I’m not following ?


#35

If you have subfolders within a python script directory like this:

pythonscripts/
pythonscripts/mytools
pythonscripts/mytools/toolscript.py

Then you normally add only the main directory to the path, in this case “pythonscripts”.
If you want to access files from the subdirectory you first have to tell python that it is a module.
You do it by simply adding an empty text file called init.py to the directory you want to use.

pythonscripts/
pythonscripts/mytools/__init__.py

Now from within Maya you can do:

import mytools.toolscript as mytoolscript
mytoolscript.executewhateveryouhavedefined()


#36

If by default the script requires one to do a command such as this (fake command); acmeToMaya.ui() you still must include, because you placed the init.py file in the subfolder where the script lives, the following command;

import mytools.toolscript as mytoolscript
acmetoMaya.ui()

I tried that as such;

import AcmeToMaya.AcmeToMaya as AcmeToMaya
AcmeToMaya.ui()

And I got an error that the module has no attribute ‘ui’ but the original command for the script requires the ui() to be placed to run the script. I’m trying to make sense of this without being specific to any one script, if this is the correct method as you say and I’m not doubting you, then all should work ?

Another question, can I setup all the Maya environment variables I have in my Maya.env in the userSetup.py file and can I remove the MAYA_APP_DIR system environment variable and have it setup strictly in my userSetup.py ?


#37

The directory structure for this import:

import AcmeToMaya.AcmeToMaya as AcmeToMaya
AcmeToMaya.ui()

has to be:

AcmeToMaya/__init__.py
AcmeToMaya/AcmeToMaya.py

And the AcmeToMaya.py contains a function called ui().
Is this the case in your setup?

Concerning your other question: Yes, it should work to set all environment variables in the userSetup.py. To be honest I did not try it yet because we always start Maya with a batch script where the evn is set correctly.


#38

[QUOTE=haggi]The directory structure for this import:

import AcmeToMaya.AcmeToMaya as AcmeToMaya
AcmeToMaya.ui()
has to be:
AcmeToMaya/__init__.py
AcmeToMaya/AcmeToMaya.py

And the AcmeToMaya.py contains a function called ui().
Is this the case in your setup?

I did the command correctly, but I get the error, ‘module’ has no attribute ‘ui’ ?


#39

Hmmmm… I asked if this is your directory setup and if in your py file is a function called “def ui():” and you answered “I did the command correctly”. I suppose my english is terrible and hard to understand, because I expected something like: “Yes, the directory structure is exactly as in your post and the py file contains the function.”

From the error message you are still not posting (you may have a look at djx posts), I suppose that this works for you:

import AcmeToMaya.AcmeToMaya as AcmeToMaya

Is that correct? (a simple yes/no would be okay)
If so, then the error is in your file AcmeToMaya.py. Show the code in your file and we can see if there is something wrong. Well something like:

def ui():
    print "Doing UI"

as content of the AcmeToMaya.py should be enough and should work.


#40

My mistake I did know to look within the .py file instead I was looking outside for a file name which included “ui()”. There is no def ui() or traces of def ui() as in anything equivalent; or any of the other files ?