2 - Distinguishing Python PDF
2 - Distinguishing Python PDF
© 2012 Autodesk 1
Session Agenda
• Working with Modules
• Utilities functions
• Maya Standalone
• Python and MEL Scripting
© 2012 Autodesk 2
Working with Modules
Navigating the Maya Python namespace
© 2012 Autodesk 3
Maya Python Modules
maya Top-level module
import maya.cmds
maya.cmds.sphere()
© 2012 Autodesk 5
Importing Modules
There are three forms of import:
© 2012 Autodesk 6
Using Modules
• To use a module in your code, use “import”
• “import” adds a module to the namespace of your code
• “import” searches sys.path
• Similar to “source” in MEL
© 2012 Autodesk 7
Script Modules
• Module is used to define closely related functionality
• Use Python "import" command to bring in modules
import myModule
myModule.myFunction()
# Result: 1 #
© 2012 Autodesk 8
Python Path
• PYTHONPATH environment variable
• Read when Python initializes
• Stored in sys.path
• sys.path
• Can be modified after Python is initialized
© 2012 Autodesk 9
Scripts Path
• PYTHONPATH defines search path for scripts
© 2012 Autodesk 10
Maya Environment Variable
• Text file: Maya.env
• Syntax:
PYTHONPATH = C:\Users\wengn\Documents\test
• Save it to
C:\Users\wengn\Documents\maya\2012\Maya.env
•
© 2012 Autodesk 11
Config File
• Create a userConfig.py file and add it to somewhere on
your path (PYTHONPATH)
• You can add imports and whatever
• Changes will show up in top level context
© 2012 Autodesk 12
Other Useful Modules (externally)
os process management,
environment variables,
etc
os.path file path routines: join, split,
etc.
sys shell arguments (argv),
python globals
re regular expressions
© 2012 Autodesk 14
Utility Functions
• maya.utils
Utilities that are not API or Command specific are located in
maya.utils
Only two functions there so far
1. maya.utils.executeInMainThreadWithResult()
2. maya.utils.processIdleEvents()
© 2012 Autodesk 15
Utility Functions
• maya.utils.executeInMainThreadWithResult
• executeInMainThreadWithResult allows other threads
to call Maya routines which are not thread safe
• It only works from the GUI app as it uses idle events
• Thread blocks until main thread completes execution
• Any result or exception is passed to the calling thread
© 2012 Autodesk 16
Utility Functions
• maya.utils.executeInMainThreadWithResult
• executeInMainThreadWithResult can accept either a
string to evaluate or a Python function
def myProc():
return maya.cmds.ls()
res = maya.utils.executeInMainThread( myProc )
© 2012 Autodesk 17
Utility Functions
• maya.utils.processIdleEvents
• maya.utils.processIdleEvents() is mainly used for
testing
• It tells the idle queue to process existing idle events
• Returns True if all items on the idle queue were
processed
© 2012 Autodesk 18
Maya Standalone
© 2012 Autodesk 19
Importing Maya into external
interpreter
• Maya modules can be used within an external Python
interpreter as long as:
• version numbers match well enough
• environment is correct
• Maya ships with a “python” command in the bin directory
of the distribution
• Included “python” command sets up correct environment
and runs Maya’s version of Python
© 2012 Autodesk 20
Initialization
• Using Maya in a standalone Python interpreter is similar
to writing a library mode app using the API
• Maya must be initialized before Maya’s Python APIs are
used
• Initialization is done as follows
import maya.standalone
maya.standalone.initialize( name=’python’ )
© 2012 Autodesk 22
Maya Python Scripts
• All "Maya Commands" (previously called "MEL
Commands") now accessible from Python
MEL:
select pCube1;
ls –sl;
Python:
import maya.cmds
maya.cmds.select( “pCube1“ )
maya.cmds.ls( sl=True )
© 2012 Autodesk 23
Python vs. MEL
• Use a good text editor and don’t mix tabs and spaces
Tabs are 8 spaces by default
• You must explicitly import all external code
No auto finding of global functions
Global scope has a different definition in both languages
• All object names must be quoted
• Booleans in Python are “True” and “False” capitalized
• Importing a module again does not reload it
Use reload command instead (i.e. reload(maya.test.UtilsTest))
• Use PYTHONPATH instead of MAYA_SCRIPT_PATH
© 2012 Autodesk 24
Interoperability between scripting
languages
• Python can call MEL and MEL can call Python
© 2012 Autodesk 25
MEL to Python
• MEL has a new “python” command that can be used to
call into Python
• Takes a string containing Python code
• Returns the result of the computation
Basic Python types are converted into matching MEL type
(e.g.strings, ints, lists of ints, etc.)
All other types are converted into their string representation
import myModule
$result = python( “myModule.myMelProc()” )
© 2012 Autodesk 26
Python to MEL
• Python has maya.mel() function, which is possibly
• moving to maya.cmds.mel()
• Takes a string containing MEL code
• Returns the result of the computation
• All MEL return values are converted into a logical Python
value
© 2012 Autodesk 27
Python/MEL communication
• Invoke MEL from Python:
import maya.mel
maya.mel.eval( "ls -sl" )
# Result: ['pSphere1'] #
© 2012 Autodesk 28
Python/MEL communication
• Accessing Python information from MEL
Python:
class MyFile:
_f = "mytestfile.txt"
mf = MyFile()
MEL:
string $f = python("mf._f");
print $f;
© 2012 Autodesk 29
Autodesk
© 2012 Autodesk 30