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 :))

  1. As soon as Maya’s Python version gets an upgrade we will be able to just:

    import __main__
    __main__.someCommand = lambda: print(‘someCommand called!’)

You must be logged in to post a comment.