2009
08.11

A cool feature of Maya’s python is that it runs as an interactive session, and that lets you do this neat trick from any imported module

you can add objects to the __main__ namespace (The script editor’s interactive prompt), this is also where python will search when you use the MEL command python();

from some python file:

#import the interactive main module
import __main__

def someCommand():
'''This is a pretty basic function...'''
print 'someCommand called!'

#now assign it to an attribute in __main__
__main__.someCommand = someCommand
python("someCommand()");
// someCommand called!

This can also be used as a way to create global variables across multiple modules, as it is always present so long as Maya is open (although any attributes you define may not be, so check first :) )

2 comments so far

Add Your Comment
  1. Awesome. Looks like this would be a good method of bring Mel’s global variables over to Python’s as well.

    • Yeah, you could loop over all global mel varaibles and import them into __main__ pretty easily. Although keeping them in sync becomes a challenge. I think pymel’s dict subclass works slightly better (although still very hackish)