4
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?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Frank
  • 2,109
  • 7
  • 25
  • 48
  • You should try to debug your code – by doing so you will be able to see whats going on in your program – Klatten Jun 25 '20 at 09:57

3 Answers3

2

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" ;
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    Is it not maybe related to the way Maya/Python stacks the execution of the code? Like the lifecycles of the code?...thanks for the examples by the way, but I marked @GreenCell's answer since he actually solved the problem :) – Frank Jun 26 '20 at 11:25
2

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.

Frank
  • 2,109
  • 7
  • 25
  • 48
Green Cell
  • 4,677
  • 2
  • 18
  • 49
0

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.

Vadim
  • 469
  • 3
  • 13