Maya API - Console Output Callback

Recently I needed to retrieve Maya's console output (redirected from our engine) and get its string contents and redirect it to python's stdout.

After a little digging through the Maya API documentation I came up with a simple solution.
http://download.autodesk.com/us/maya/2011help/API/class_m_command_message.html


Setting up an event callback for every time Maya outputs something to it's console is fairly easy, as show below.


from maya import OpenMaya as om

tempmessage = ''
_createCallback()
# -- do something with tempmessage after the next output
_removeCallback(eventid)

def _messageCallback(message, messageType, data):
    tempmessage = message
    
def _createCallback():
    """configure the callback and tie it to our messageCallback func"""
    eventid = om.MCommandMessage.addCommandOutputCallback(_messageCallback, None)
    return eventid

def _removeCallback(eventid):
    """remove the callback associated with eventid"""
    om.MMessage.removeCallback(eventid)

I ended up saving the string (tempmessage) and parsed it as needed for my purposes.


Current Playing: Ty Segall