one of the things i love about python is that i keep learning new  
tricks.... check this out, i bet you'll learn something too.

i've been playing around with changing the way that python results are  
displayed in the script editor.  IMHO, Autodesk made a pretty big  
mistake when they changed the behavior from the default python  
interpreter.

in the script editor i get this:

from pymel import *
ls()[0]
# Result: time1 #

but in the interpreter i get this:

 >>> from pymel import *
 >>> ls()[0]
Time(u'time1')

there's a big difference between time1 and Time(u'time1').  the latter  
tells me much more about the object  and this is pretty essential for  
anyone learning python (or someone trying to teach python).   
technically, the difference in output is the difference between these:

print x            # bad!  not standard
print repr(x)  #good!

also, i just learned that there's a shorthand for the latter:

print `x`

that's all well and good, but how can we change this in the script  
editor?  first, you may be aware that maya overwrites the default  
stdin, stdout, and stderr with their own classes in order to redirect  
to the script editor. you can find this code in maya.app.startup.gui:


# Replace sys.stdin with a GUI version that will request input from  
the user
sys.stdin = maya.app.baseUI.StandardInput()

# Replace sys.stdout and sys.stderr with versions that can output to  
Maya's
# GUI
sys.stdout = maya.utils.Output()
sys.stderr = maya.utils.Output( error=1 )


if you ever want to get to the untouched versions of these, you can  
find them at sys.__stdout__ and sys.__stderr__

moving on.  python also provides a hook for changing the way that  
results are printed. it's called sys.displayhook.  this is exactly  
what we want to override.  it seems that Maya has overwritten this  
too, but it is not occurring anywhere visible in the maya package (i  
searched all the .py files for displayhook and came up empty handed).   
here's how we know maya has overwritten it:

sys.displayhook.__module__
# Result: maya.app.python

the maya.app.python module has some callbacks which look like they are  
being used elsewhere to overwrite sys.excepthook, but there's nothing  
about displaying normal output.  so, i figured i'd overwrite it with  
my own.  well, think again.  no matter what i do i can't overwrite it,  
but i'm not getting any errors:

def myhook(value):
        print "gotcha"

sys.displayhook = myhook  # overwrite
sys.diplayhook == myhook #check if assignment worked
# Result: False #
sys.displayhook.__module__ # double check
# Result: maya.app.python

the object won't budge.  try something deeper:

sys.__dict__['displayhook'] = myhook
sys.diplayhook == myhook #check
# Result: False #

still there.  try deleting it first:

delattr(sys, 'displayhook')
# RuntimeError: lost sys.displayhook
sys.displayhook = myhook  # overwrite
sys.diplayhook == myhook #check if assignment worked
# Result: False #
sys.displayhook.__module__ # double check
# Result: maya.app.python

nada.

any more ideas on how i can overwrite this?  autdodesk, if you're  
listening, please allow this hook to be overwritten!!!

-chad








--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---

Reply via email to