Maya's Python Print statement


#1

I wrote a logger that outputs stdout/stderr (info, warning, error and debug) to a Qt Window that also output’s to a logging file.

The problem that I’m having now is that I’ve broken Maya’s print statement. Print output will only show up in the Qt Window that I have re-directed output too.

How do I reset Maya’s Script Editor to starting receiving output from print statements again?

NOTE: It’s only Python’s print statement that doesn’t work, everything else outputs (i.e., mel.eval(‘print “Hello World”’)


#2

SOLUTION:
Since I was overwriting XStream and redefining stdout I needed to reset stdout to point to Maya’s output, but where is that? Well, the hint was staring me in the face, I just needed to find it; the answer was in plogging.py (pymel’s logging system). There’s a function called _fixMayaOutput and they use it to reset their own logging system.

So…

import sys
from maya import utils

# redefine sys.stdout
sys.stdout = utils.Output

# in order to use stdout 'write' requires a 'maya.Output' so we flush it
class MayaOutput(sys.stdout):
    def flush(*args, **kwargs):
        utils.Output = MayaOutput()
        sys.stdout = utils.Output

# initialize
log = MayaOutput()

# and flush, we're back to feeding output to the script editor
log.flush()

print("Hello World")

#3

the original console is always available in sys.stdout

https://docs.python.org/2/library/sys.html


#4

I’don know if anyone can read this reeply.
i run the code , in maya,but i got an error:

class MayaOutput(sys.stdout):
def flush(*args,**kwargs):
utils.Output = MayaOutput()
sys.stdout = utils.Output

Error: TypeError: file line 2: Error when calling the metaclass bases

file() argument 2 must be string, not tuple # 

why~? help~!!! please.