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.

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 = ''
# -- do something with tempmessage after the next output

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"""

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

Current Playing: Ty Segall