Maya Python Notes

Page for Maya Python notes

Since there is no real API documentation helping people who come from MEL scripting to Python, I’ll be posting ‘translated’ stuff here. By ‘translated’ I mean getting C++ examples and rewriting in Python code.
Be aware that I have no real programming training so I cannot explain why certain things work the way they do, so be patient for I am also still learning. : )

For the sake of clarity, I’ll be importing all my examples like this: (it’s also the way it’s done in Maya Documentation)

import maya.cmds as cmds
import maya.mel as mel
import maya.OpenMaya as OpenMaya

  • Jitter Deformer Node Walkthrough
  • Click the link above for a detailed walkthrough on how to get your first custom deformer up and running! :- )

  • Getting current selection on the scene
  • This is a quite simple example you’ll find a lot of C++ examples and use a lot in all kinds of scripts and plug-ins.
    Getting a selection list through the API is a bit different than through a Maya command…
    In the first line you instance a MSelectionList object so you can access all of its functions that were pre-written to handle selections in Maya.
    In the second line we call a static method from the MGlobal class (a class that provides common API global functions - as it says on the API docs), the getActiveSelectionList. We use the selection list we just created - it was empty at the moment of its creation and now it’s gonna be populated with the currently selected things in the Maya scene.

    Now we have the objects selected stored to do anything we want with it. In our case, lets invoke a method from the MSelectionList class that returns the selection as strings to use on our code as we see fit - the getSelectionStrings method - and pass a python list(lista) as an argument, so our list will be populated with the needed strings(the selected objects names).
    In the end, I print it to make sure it did its job.

    selected = OpenMaya.MSelectionList()
    OpenMaya.MGlobal.getActiveSelectionList(selected)
    lista = []
    selected.getSelectionStrings(lista)

    print lista

    Note that even if you change the selected objects in the scene, our selection list won’t update until we call the MGlobal.getActiveSelectionList again.

  • Info, Warning and Error Messages
  • Since there is no cmds.error command, you can display an error using the command bellow and tell the function to return, so it will stop the execution of the command.

    OpenMaya.MGlobal.displayError(”the object was not found!”)
    return

    You can also make warning and simple prints using the commands bellow.

    OpenMaya.MGlobal.displayWarning(”it’s almost lunch time!”)
    OpenMaya.MGlobal.displayInfo(”this is sort of a print.”)

  • Finding if a string exists in another string
  • This is actually pure Python, but I wanted to document it because it made my scripting life MUCH more easier.
    In MEL you have to go through a lot to find out if some text exists inside another one. In Python, you just use string comparison.

    ’sometext’ in ‘idontknowsometextwhatever’
    #True

    ‘nothere’ in ‘comparison123_string’
    #False

  • Other learning resources
  • Ryan Trowbridge - Maya API docs demystified for Python users