To import *.py from sub directories?

To import *.py from sub directories?

Anonymous
Not applicable
1,658 Views
4 Replies
Message 1 of 5

To import *.py from sub directories?

Anonymous
Not applicable
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 dislike a full path dir with the "import" function...

Then I did some research with google and found that :

- 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 py and pyw files in sub-directories from my "\scripts\_SAMA_LIB\PY"

Python scripts work well in the main scripts directory, but I would like to work with sub dir without to do a sys.path.append&#40;dir&#41; witll all the sub directories!


Really thank you for your help!
0 Likes
1,659 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
I use an empty __init__.py for each &#40;sub&#41; directory and I could &#40;but rarely do so&#41; put the import statements in my userSetup.py


so I might have
scripts
userSetup.py
dir1
__init__.py
dirA
__init__.py
script1.py
script2.py
dirB
__init__.py
script1.py
script2.py
dir2
__init__.py
dirA
__init__.py
script1.py
script2.py
dirB
__init__.py
script1.py
script2.py
0 Likes
Message 3 of 5

Anonymous
Not applicable
hmmm...

We are speaking about it on polycount and I am having a very lot of problem to work with a "python project" in my script directory :

http://boards.polycount.net/showthread.php?p=1117647


What I mean is I am currently working with Melscript and I have very large library of script I did along the years and always good when creating new tools.

Imagine the following tree :

-|scripts
--|_SAMA_Lib
---|MEL
----|Animation
----|Array
----|Bone
----|Export
----|FBX
----|Rigging
----|String
----|Vector
----|etc.....

In each directory I have a set of files with only one function inside.
Then file name = function name is an easy way to call back stuff.

In the "MEL" directory there is a file call "LoadLib.mel" it help me to source a complete lib if I work with it. For example LoadLib&#40;"Rigging"&#41;.


Then starting to work seriously with Python I would like with time to stop Melscript and switch definitely with python.
For that reason I would like to create a system similar to my melscript workflow.
To simply rename the MEL directory with PY and with time to re-write Mel-script to Python



On the Polycount url I gave on the top, you can read on the last message, someone give a solution.

It look like that :

In the PY folder &#40;the old MEL one...&#41; he add the following line in the __init__ file :
import OpenGL.SAMA_ExportToOpenGL as OpenGL


The OpenGL dir is in the PY directory.
And the ExportToOpenGL file in the OpenGL directory.

It means I need to add a line for all my *py file &#40;I already stop to count the mel file I made in my lib...&#41;. Then a litte crazy isn't it?


I could create a script for it and it will create it automaticaly but.....


.... traveling in the C:\Program Files\Autodesk\Maya2009\Python\ directory of the Maya installation, I found several directories in the the python dir, and all the __init__ files are empty.

Then maybe... there are another wait than editing the __init__ files, isn't it?


Do you know more about it?

And another problem is about the reload&#40;&#41; function.

Because if I already launch the import function...
... do a modification in the *.py file....
... import again, it deson't work....
... then try the reload&#40;&#41; function...
... doesn't work too O_o...

And the sub directories could be the problem?
0 Likes
Message 4 of 5

Anonymous
Not applicable
It seems like your layout is not ideal for python. Instead of hacking your python scripts into a mel-style project, why not use a more pythonic layout. Maybe:
scripts/
- _SAMA_Lib/
-- __init__.py
-- Animation.py
-- Array.py
...
Then to use a submodule you can simply do:

from _SAMA_Lib import Animation
0 Likes
Message 5 of 5

Anonymous
Not applicable
hmmm yes I understand what you mean...

I was using exactly the same worflow before with melscript..

But with time each files started to be very big, and when I was looking for a function, it was very difficult to find it...


With my current workflow I did several function I can use when I want in Maya.

For example if write the following command in the script editor :

--&gt; LoadLib "Rigging"

It will return all functions available in the directory.

And if I find my function, I can copy the name and do a :

--&gt; PrintScript "NameOfTheFunction"

And it will print the complete content of the function in my script editor.
Then I can understand how I built it etc...

Very useful when you made a cool function one year ago and you need it again.

You do not need to go in the scripts, to browse directories, to find interesting files and to open them one by one...


This workflow is similar when you call melscript from Maya but from my lib...
I will do a movie this week to show how it works.


Hope it help you to understand the goal here...
0 Likes