pythonmayamelpymel

Maya – Why Print method can't be executed after Clear History command?


cmds.scriptEditorInfo(clearHistory=True)
print("hi")

The top line clear's the Maya Script Output window, and then the line below that is supposed to print hi. But when you run this, it flashes the hi output, and then clears everything. So the cmds.scriptEditorInfo(clearHistory=True) is executing last. Could someone explain this to me and help me understand how I can clean the Output Window AND THEN print hi.

I got the clear function from here: How can I clear the Maya Script Editor programatically?


Solution

  • I can also confirm that it clears the history and doesn't print, even if I make a loop and print 100 times.

    There is a way around it using evalDeferred so that it doesn't execute right away:

    import maya.cmds as cmds
    
    cmds.scriptEditorInfo(clearHistory=True)
    cmds.evalDeferred("print 'Hello world!'")
    

    Or if you want to run a lot of code after the clear command:

    import maya.cmds as cmds
    
    def run_code():
        # Run any code here
        print('Hello!')
    
    cmds.scriptEditorInfo(clearHistory=True)
    cmds.evalDeferred("run_code()")
    

    Now the history clears and we see our print command as expected.