Does someone know how to import multi sub directories with *.py and *.pyw files with python?
For exemple to import my mel script lib I am using the following mel script code :
// ==================
global proc LoadLib (string $libsToLoad[])
{
string $dir = `internalVar -userScriptDir` + "_SAMA_LIB/MEL/";
for ($lib in $libsToLoad)
{
string $sourcedir = $dir + $lib + "/";
string $files [] = `getFileList -fs "*.mel" -fld $sourcedir`;
print $files;
for ($file in $files)
{
string $cmd = "source \"" + $sourcedir + $file + "\";";
eval $cmd;
}
}
}
// ==================
Then I did a similar function with Python because I would like to switch on Python :
import maya.cmds as mc
def LoadLib (libsToLoad):
_dir = mc.internalVar(userScriptDir=1) + '_SAMA_LIB/PY/'
for lib in libsToLoad:
sourceDir = _dir + lib + "/"
files = mc.getFileList(fld=sourceDir)
for _file in files:
_file = sourceDir + _file
print _file
import _file
Problem is Python seems to not like the fact using the "import" function with a full path dir...
Then I did some research with google and found that :
-
http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder
As they said I added an empty __init__.py in each directories I would like to import my python.
But nothing works
...
In maya I am doing some test with :
import maya.cmds as mc
_dir = mc.internalVar(userScriptDir=1) + '_SAMA_LIB/PY/__init__'
print _dir
import _dir
but I have the following error :
# Error: No module named _dir
# Traceback (most recent call last):
# File "<maya console>", line 4, in <module>
# ImportError: No module named _dir #
Then... is that possible???
I would like to source multi sub directories in my "\scripts\_SAMA_LIB\PY"
Python script works well in the main scripts dir, but I would like to work with sub dir wihtout to do a sys.path.append(dir) witll all the sub directories!
Replies
From what I know the first __init__ needs to be in your pythonpath.
So it looks a like this:
* import code.myCode as myCode
now from Maya I can import myCode
I can try sourcing from the maya scripts folder and see if it works. Should...
(*)In the init put this line:
import code.testCode as testCode
So to call a function within the testCode.py you can go
import myCode
myCode.testCode.myFunc()
Or within Maya you can go:
from myCode import testCode as tc
tc.myFunc()
Hope that helps.
Thank you a lot!
I am updating my script now but I have a set of error,conflict, problem etc... and I cannot launch several time the same following command from Maya...
I Need to restart maya for each test :
Then I tried something like that :
But it creates an error.
I tried several other way with reload function but same, it gives an error =_=...
Reload function is very easy for use from the Script directory but with sub dir this is the war.
All tutorial from google will reload from the main directory.
Also is there a way to a import *.py file but to detect if it is already imported and then I can switch to reload function?
Import function only works the first time. Then if a command return a simple string and not an array, the 2nd import will keep in memory the variable was a string and it creates an error.
And the debug in Maya is the hell, really. :;(
In my example above if I made any changes to the module after the import I would use reload()
so:
reload(testCode)
so try:
reload(test)
So just to be clear, you import your module the first time and only the first time, then everytime you make a change you use reload.
Not at work so can't test it right now.
http://docs.python.org/library/functions.html#reload
wtf... seriously :D Already 2 days on it XD.
Are you using something similar to what I have shown? I don't include the .py in my imports. And once you do a clean import you only need to run:
reload(test). Diffult to know what exactly is going on when I don't know your exact module structure. I've never run a from and reload on the same line...that seems that it may be part of the issue.
Cheers,
Shawn
-|scripts (Maya defaul scripts directory)
---|_SAMA_LIB
|PY
|OpenGL
|SAMA_ExportToOpenGL.py
Maybe easier like this
> "C:\Users\sama\Documents\maya\2009-x64\scripts\_SAMA_LIB\PY\OpenGL"
The right command to import the SAMA_ExportToOpenGL.py file with his function is :
But impossible to make working the reload() >__< hahaha!
Okay so I've made a file structure similar to what you have, just put the content into your scripts folder:
http://www.digitalartisan.ca/python__init.zip
now run:
import __SAMA_LIB.PY.OpenGL as OpenGL
as your initial import, then:
reload(OpenGL)
to update changes
There is a line in the __init__.py in the PY folder that does the work of seting the module namespace:
import OpenGL.SAMA_ExportToOpenGL as OpenGL
As you add more models(or python scripts) you want to update this _init__.py
Just noticed the __init__.py in the OpenGL folder has a print in it. You can remove that.