Python Modules and Maya


#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 ?


#41

Sorry, I am unable to understand what you want to know. Could you explain your question more in detail?


#42

Okay. I had a look at your problem. Next time, it would be very much easier if you simply tell use that you want to run “MMtoKey” downloadable form CreativeCrash. This way we can try it ourself and check what’s wrong.

To your problem. You tried to run something like this:

import MMtoKey.MMtoKey as mmtokey
mmtokey.ui()

What does not work, simply because there is no ui() in the MMtoKey.py file. The ui resiedes in the init.py file. That means you simply can:

import MMtoKey as mmtokey
mmtokey.ui()

what should work.


#43

The second suggestion worked. I didn’t look in the init.py; learned something new, as well as how to find little issues, hopefully when running python scripts :slight_smile:


#44

How do I add my series of custom maya environment variables to the userSetup.py so I can cease to use Maya.env.

I assume, when updating Maya, you can simply toss the userSetup.py file into the default path for Maya; or a custom MAYA_APP_DIR path and all environment variables and shelves are setup, instantly ?

That is hoping you can also cease to use the system environment variable MAYA_APP_DIR and place that as well into the userSetup.py file, as you mentioned you use a batch script.


#45

You are kidding to me, aren’t you? We already told you how to set environment variables with python.


#46

Calm down, I haven’t dug into it :slight_smile:


#47

Concerning your problem with the NSUV (night shade uv editor):

Instead of placing the scritps directly into your scripts directory, you placed them in your own path.
This results in a few problems:

1.) the preferences files. Maya expect them in the default locations, e.g. in your documents/maya/Maya2016 directory. They will not be fount if they are placed elsewhere (unless you change some environment variables).

2.) The mel scripts. They are expected in a path like scripts/NSUV. So if you have aen extra scripts path called “c:/myscripts”, all scripts (mel an py) have to be placed in “c:/myscripts/NSUV”.

I’d suggest to place the prefs in your default prefs folder and set the python path as shown above. Then it works.


#48

1.) the preferences files. Maya expect them in the default locations, e.g. in your documents/maya/Maya2016 directory. They will not be fount if they are placed elsewhere (unless you change some environment variables).

The preferences folder within the NSUV compressed file; contains a sub-folder of only icons. As I have a custom icons folder in my, Maya.env file, why can’t I place those icons in the custom icon folder, as I’ve done and have them work or within a custom path such as C:\myscripts\NSUV\icons ?

2.) The mel scripts. They are expected in a path like scripts/NSUV. So if you have aen extra scripts path called “c:/myscripts”, all scripts (mel an py) have to be placed in “c:/myscripts/NSUV”.

I’ve tried placed NSUV within a custom scripts & python path, not both at the same time. Within the NSUV folder structure this is how it’s exists whether it was placed in my custom scripts or custom python path; c:\myscript\NSUV
\icons
\panel <MEL files
\scripts\NSUV <strictly python files.

Here is the current error I get when running NSUV from a custom scripts path

# Error: ImportError: file <maya console> line 1: No module named NSUV # 

This is the error I get when run from a custom python path;

// Error: source "NSUV/panel.mel"; // 
// Error: Line 1.24: Cannot find file "NSUV/panel.mel" for source statement. // 
# Error: MelError: file C:\Program Files\Autodesk\Maya2015\Python\lib\site-packages\pymel\core\language.py line 835: Error during execution of MEL script: source "NSUV/panel.mel";
Line 1.24: Cannot find file "NSUV/panel.mel" for source statement.
Script:
  source "NSUV/panel.mel"; # 

#49

Indeed, if you only need icons, Maya can find them in any path with the correct environment variable.

There is no reason to seperate the scripts into mel and python files. Especially if you do not know how the python files accesses the mel files. Your approach cannot work unless you modify content of the python files.

Sorry, but the whole thing is really simple:

Extract the NSUV zip file into a directory.
Set the python search path (for python files) and the maya scripts path (for mel files) correctly, import the NSUV, form the popping up window create a shelf button - done.

And meanwhile after the whole discussion, you should know what that means:

# Error: ImportError: file <maya console> line 1: No module named NSUV # 

You simply set the path wong or the import.


#50

I didn’t understand what you meant. This is how I want it, and what I want to know is the way I want it, is it possible strictly speaking of this script or impossible; and if it’s possible what am I doing wrong ? It may be simple, but until I understand, it’s not simple.

Going back a few steps; I have a custom python path; c:\ or c:/myscripts/NSUV within the NSUV sub-folder the follow sub-folders exist:

  • icons
  • panel
  • scripts

Remember we are strictly talking about this one script. When I run the command import NSUV I get the following error;

// Error: source "NSUV/panel.mel"; // 
// Error: Line 1.24: Cannot find file "NSUV/panel.mel" for source statement. // 
# Error: MelError: file C:\Program Files\Autodesk\Maya2015\Python\lib\site-packages\pymel\core\language.py line 835: Error during execution of MEL script: source "NSUV/panel.mel";
Line 1.24: Cannot find file "NSUV/panel.mel" for source statement.

I want to run the script in the path mentioned above, not in Maya default script path etc. I read your second last and last post a few times and I couldn’t extract what you’re saying, one example is;

2.) The mel scripts. They are expected in a path like scripts/NSUV. So if you have aen extra scripts path called “c:/myscripts”, all scripts (mel an py) have to be placed in “c:/myscripts/NSUV”.

And that is exactly what my path is setup as for the script files, but it is not working.


#51

Set the python serach path to “c:/myscripts/NSUV” and set the mel search path to “c:/myscripts/NSUV”. That’s all, really.


#52

Son of a :banghead:
I appended the paths for NSUV in userSetup.py for both Python & MEL, I’m getting the error; # Error: ImportError: file <maya console> line 1: No module named NSUV # but the paths are correct. Considering the scripts are located in c:\myscripts\NSUV\scripts I added the sub-folder “scripts” but I continue to get the No module named NSUV, :argh:


#53

And in your “scripts” folder exists another folder called NSUV?

NSUV/scripts/NSUV

If so, I can’t help any more. It works fine here.


#54

NOT this:

Do this:

And stop using the backslash all the time. This \ is BAD… This / is good.

If it works for haggi and you have the same scripts then the error is your path setting, no matter how many times you tell us it is not. :banghead:

David