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

Viewed 606
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?

3 Answers

It seems that it's a bug.

I've tried three approaches and it turned out that all three don't work in case you execute these two lines simultaneously. But it definitely works if each line will be executed separately: firstly scriptEditorInfo() method and secondly print() method.

Your approach:

import maya.cmds as mc

mc.scriptEditorInfo(clearHistory=True)
print("Hello")

Second approach (pymel):

import pymel.core as pc

pc.scriptEditorInfo(clearHistory=True)
print("Hello")

Third approach (MEL):

scriptEditorInfo -clearHistory ;
print "Hello" ;

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.

You can do it using:

import maya.cmds
reporter = mel.eval( 'string $tmp = $gCommandReporter;' )
cmds.cmdScrollFieldReporter(reporter, e=True, clear=True)

cmdScrollFieldReporter command needs defined reporter.

Related