How do I capture the script editor output of a mel command, using in python?

We have some plugins that run as mel commands, and when things go wrong they spew useful info to the script editor, but return 0. How do I capture the script editor spew (not the return value) of a mel command, using python?

import pymel.core as pm
mel_cmd= 'print("hello world\\n");'
result=pm.mel.eval(mel_cmd) # no this is just the return value
print "the output of mel '{}' is: '{}'".format(mel_cmd, result)

#script editor output:
#hello world
#the output of mel 'print("hello world\n");' is: 'None'

so I came up with this clunky decorator function, but it feels…clunky.

def capture(function):
    """
    a decorator fn that captures output sent to the maya script editor
    :param function: a function whose script editor output we want to capture
    :return:
    """

    TEMP_FILE = os.getenv("TEMP") + ("/maya_script_editor_capture.txt")


    # empty the temp file so we only capture current command
    with open(TEMP_FILE, "w") as f:
        f.write("")

    #wrap the function that outputs to the script editor
    def captured_output(*args, **kw):
        pm.scriptEditorInfo(writeHistory=True, historyFilename=TEMP_FILE)
        result= function(*args, **kw)
        pm.scriptEditorInfo(writeHistory=False)
        with open(TEMP_FILE, "r") as f:
            history=f.read()
            return history
    return captured_output

@capture
def test():
    mel_cmd= 'print("hello world\\n");'
    pm.mel.eval(mel_cmd)

captured_result=test()
print("captured result: {}".format(captured_result))

Seems to give me what I want but I wonder if there’s some other way to redirect the script editor, so that I’m not writing and reading files just to get the text.

You might be able to use StringIO.StringIO() instance instead of the temp file.

I’ll give StringIO a shot.

the plugin programmer explained that the plugin returns MStatus::kSuccess or MStatus::kFailure
but I can’t seem to capture a value from this using MEL (or python wrapped mel). the return values appear to be null /None

StringIO class instances can’t be passed as the historyFilename parameter.
odd that scriptEditorInfo() has no direct ‘give me your text’ function.

Ah, didn’t realize it took a file path and not a file object.
Yeah StringIO won’t work in that case.

Another option would be OpenMaya.MGlobal.executeCommand which lets you run a mel command and potentially capture the success/failure.
Pymel uses this under the hood to capture errors / convert to Exceptions.

What happens if you stick an imposter into sys.stdout in python… ? It will allow you to, for example, record the log there before passing it along. Does mel show up in that? never tried it , but perhaps someone has.