Sourcing a Python script through Mel 27 Aug 08

So you want to do something python that’s not supported in Mel but are unsure how exactly to link the two.

 

OK so lets start with the python side of things.

 


Python:

 

You have a file called "myFirstScript.py" inside your ‘Documents\maya\2008\scripts\’ directory and you want to execute it.

 

#	myFirstScript.py	#

######################
#	imports
######################

import maya.mel as mel

######################
#	functions
######################
def helloWorld():
	print('Hello World! (python native)')
	mel.eval('print "Hello World! (mel via python)\\n"')

 

Now that seems simple enough?  From what most of the docs say all you need to type in a python window is:

import myFirstScript
myFirstScript.helloWorld()

However you’ll find this doesn’t work! You’ll get an error something like:

# Error: line 1: No module named myFirstScript
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# ImportError: No module named myFirstScript #

For some reason Autodesk messed up the default paths for the python search path so you have to add one extra:

import sys
sys.path.append(path) # where path equals the full path to the place you put your script i.e.
import sys
sys.path.append('C:/Users/Rod/Documents/maya/2008/scripts' ) # Note: No trailing slash!!!!

What Autodesk seem to have done is added an extra forward slash at the end of the path where python doesn’t want it.  If you type ’sys.path’ in a python script window and press enter you’ll see the full list of default paths it’ll search through.

# Result: ['C:\\Program Files\\Autodesk\\Maya2008\\bin',
'C:\\Program Files\\Autodesk\\Maya2008\\bin\\python25.zip',
'C:\\Program Files\\Autodesk\\Maya2008\\Python\\DLLs',
'C:\\Program Files\\Autodesk\\Maya2008\\Python\\lib',
'C:\\Program Files\\Autodesk\\Maya2008\\Python\\lib\\plat-win',
'C:\\Program Files\\Autodesk\\Maya2008\\Python\\lib\\lib-tk',
'C:\\Program Files\\Autodesk\\Maya2008\\bin',
'C:\\Program Files\\Autodesk\\Maya2008\\Python',
'C:\\Program Files\\Autodesk\\Maya2008\\Python\\lib\\site-packages',
u'C:/Users/Rod/Documents/maya/2008/prefs/scripts',
u'C:/Users/Rod/Documents/maya/2008/scripts/',
u'C:/Users/Rod/Documents/maya/scripts'] #

Hopefully they fix it with the next update but until then if you want your python scripts in that dir or any other you’ll need to do the sys.path.append command.

 


Mel:

Now we’ve got the python script importing correctly how do we execute this via Mel?

python("import myFirstScript"); // Source or 'import' the myFirstScript.py as a new module
python("myFirstScript.helloWorld()"); // Execute the helloWorld function inside the myFirstScript module

 

That’s it!

2 Responses to “Sourcing a Python script through Mel”

  1. SneakyDevil Says:

    Thanks!

    I was wondering – do you know how you would pass vars to an invoked python script? So instead of

    python(“myfirstScript.hellowWorld()”)

    you would have:

    python(“myfirstScript.hellowWorld(arg1, arg2)”)

    etc… I’ve tried but had no success in figuring this out yet.

  2. cOa Says:

    thanks a ton. extremely extremely helpful. once more, thanks.

post a comment

external links

Project Offset

 

Wikipedia Affiliate Button