Home Technical Talk

[Maya-Python] to import sub directories?

polycounter lvl 17
Offline / Send Message
sama.van polycounter lvl 17
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

  • Eric Chadwick
    I don't know Python enough to help you, but this section of Tech-Artists.Org is a good place to post Python queries.
  • Shawner
    Offline / Send Message
    Shawner polycounter lvl 18
    I can show you an example of how I've structured it.
    From what I know the first __init__ needs to be in your pythonpath.

    So it looks a like this:
    python(This directory is provided in the python path)
        -userSetup.py
        -myCode
          -code
             -myCode.py
             -__init__.py(empty init)
          -__init__.py(* This does have a line)
    

    * 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...
  • Shawner
    Offline / Send Message
    Shawner polycounter lvl 18
    Just tried this in my Maya scripts folder
    myCode
       -__init__.py(*)
       -code
          -testCode.py
          -__init__.py
    

    (*)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.
  • sama.van
    Offline / Send Message
    sama.van polycounter lvl 17
    alright it works now with :
    from _SAMA_LIB.PY.OpenGL import SAMA_ExportToOpenGL as test
    test.SAMA_ExportToOpenGL()
    

    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 :
    from _SAMA_LIB.PY import LoadLib as test
    test.LoadLib("OpenGL")
    

    Then I tried something like that :
    from _SAMA_LIB.PY reload(LoadLib) as test
    test.LoadLib("OpenGL")
    

    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. :;(
  • Shawner
    Offline / Send Message
    Shawner polycounter lvl 18
    you only imort the first time.

    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.
  • sama.van
    Offline / Send Message
    sama.van polycounter lvl 17
    I tried these different combination once I did a clean"import" :
    from _SAMA_LIB.PY reload(LoadLib) as test
    test.LoadLib("OpenGL")
    # Error: ('invalid syntax', ('<maya console>', 2, 24, 'from _SAMA_LIB.PY reload(LoadLib) as testn'))
    #   File "<maya console>", line 1
    #      from _SAMA_LIB.PY reload(LoadLib) as test
    #                             ^
    #  SyntaxError: invalid syntax # 
    
    
    reload(LoadLib)
    test.LoadLib("OpenGL")
    # Error: name 'LoadLib' is not defined
    # Traceback (most recent call last):
    #   File "<maya console>", line 1, in <module>
    # NameError: name 'LoadLib' is not defined #
    
    from _SAMA_LIB.PY reload(LoadLib)
    test.LoadLib("OpenGL")
    # Error: ('invalid syntax', ('<maya console>', 2, 24, 'from _SAMA_LIB.PY reload(LoadLib)n'))
    #   File "<maya console>", line 1
    #      from _SAMA_LIB.PY reload(LoadLib)
    #                             ^
    #  SyntaxError: invalid syntax # 
    
    reload(LoadLib) as test
    test.LoadLib("OpenGL")
    # <maya console>:2: Warning: 'as' will become a reserved keyword in Python 2.6
    # Error: ('invalid syntax', ('<maya console>', 2, 18, 'reload(LoadLib) as testn'))
    #   File "<maya console>", line 1
    #      reload(LoadLib) as test
    #                       ^
    #  SyntaxError: invalid syntax # 
    

    http://docs.python.org/library/functions.html#reload

    wtf... seriously :D:D Already 2 days on it XD.
  • Shawner
    Offline / Send Message
    Shawner polycounter lvl 18
    Can you tell me what directory structure you're using?

    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
  • sama.van
    Offline / Send Message
    sama.van polycounter lvl 17
    Ok let's choose the following one:


    -|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 :
    from _SAMA_LIB.PY.OpenGL import SAMA_ExportToOpenGL as test
    test.SAMA_ExportToOpenGL()
    

    But impossible to make working the reload() >__< hahaha!
  • Shawner
    Offline / Send Message
    Shawner polycounter lvl 18
    At work now, let me see if I can get something working with your directory structure


    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.
Sign In or Register to comment.