View Full Version : calling python scripts outside the maya script path


YourDaftPunk
02 February 2009, 01:48 AM
In mel you can source a script and call a procedure easy enough:
source "/some/network/path"; command;

Is this possible with a python script? I tried using sys.path to edit the PYTHONPATH environmental variable on my system but import and function calls still fail. What am I doing wrong?

Thanks.
-shawn

RyanT
02 February 2009, 02:36 AM
I have a long post about this on my blog of how import works.

http://www.rtrowbridge.com/blog/2008/11/27/maya-python-import-scripts/

Can you show exactly what you are typing and what you expect. You can not import based on a path. But you can write a python defition to do something like it. For example:

#Before this next example I created a folder here c:/testC With a file called rtPrintSomething3.py in it with the code:

def rtTest():
print 'hello'


#The script to source a python file like MEL source

import sys
import os

"""
Here is a function that I made that works very similarly to the MEL source command
If a full path is passed to psource it will check if the path is part of sys.path and
if not it will add it. If only the module name is passed to filepath it will import that
module and not add anything to sys.path Either way this function will import and reload
the module so that you know it is freshly updated
"""
def psource( module ):

file = os.path.basename( module )
dir = os.path.dirname( module )

toks = file.split( '.' )
modname = toks[0]

# Check if dirrectory is really a dirrectory
if( os.path.exists( dir ) ):

# Check if the file dirrectory already exists in the sys.path array
paths = sys.path
pathfound = 0
for path in paths:
if(dir == path):
pathfound = 1

# If the dirrectory is not part of sys.path add it
if not pathfound:
sys.path.append( dir )

# exec works like MEL's eval but you need to add in globals() at the end to make sure
# the file is imported into the global namespace else it will only be in the scope of this function
exec ('import ' + modname) in globals()

# reload the file to make sure its up to date
exec( 'reload( ' + modname + ' )' ) in globals()

# This returns the namespace of the file imported
return modname

# When you first import a file you must give it the full path
psource( 'c:/testC/rtPrintSomething3.py' )

rtPrintSomething3.rtTest()

# Now you can edit the file and add a new definition
# After using psource the directory will already be added to sys.path so you can just psource
# the module which will reload the module
psource( 'rtPrintSomething3' )

YourDaftPunk
02 February 2009, 08:01 PM
Ryan, your post was invaluable and when I tested your script, it worked beautifully.

My script is working fine now with a few fixes. I made sure I was importing into the global namespace. Also, I think I was one directory short on sys.path - I added //Network/blablabla/scripts when I needed //Network/blablabla/scripts/lighting. I might have assumed python would check the subdirectory :) Finally, I'm using reload() now too.

I think it's too bad that the Maya documentation did not come with an explanation or examples as clear as yours.

Thanks.
-shawn

CGTalk Moderation
02 February 2009, 08:01 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.


1