Sourcing a Python script through Mel

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 ‘Documentsmaya2008scripts’ 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!

7 thoughts on “Sourcing a Python script through Mel”

  1. 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. thanks a lot for this script, that is exactly what i needed. Simple and easy to use.
    Only one thing, it may be interesting to auto-rename through the whole hierarchy of objects but oh well ๐Ÿ™‚

  3. Hey Rod, great article for a beginner like me ๐Ÿ˜‰
    What if I need to call ONE def inside a class from a python file ?? ie.
    # mySecondScript.py
    class test():
              def __init__(self):
                 self.var = ""
     
              def testing(self):
                self.var = 'hello'
                print self.var,
    ?
    What should I have to do to work with statements, like
    from <class> import <def>
    from pythonfile import <def>, <class>
     
    thanks in advance

Leave a Reply to SneakyDevil Cancel reply

Your email address will not be published. Required fields are marked *