i did a little more exploring and it seems that maya is putting the
offending hook back in place during any idle period. for example, i
can actually get my callback to run if i execute the following code
all at once:
import sys
def myhook(value):
print>>sys.__stdout__, "working"
sys.displayhook = myhook #overwrite
sys.displayhook('foo') #manually call the hook. 'foo' is printed in
the output window
but immediately after it finishes running the original maya hook is
back in place:
sys.displayhook('foo') # nothing happens
crappy.
i forgot to mention in my last post that you can easily change
sys.stdout, stderr, stdin without the issues surrounding
sys.displayhook. for example, if you want to redirect all print
statements to the output window as well as the script editor:
import sys
mstdout = sys.stdout
class Redirect(object):
def __init__(self, redirect):
self._redirect = redirect
def write(self, s):
self._redirect.write(s)
sys.__stdout__.write(s)
def flush(self):
self._redirect.flush()
sys.__stdout__.flush()
sys.stdout = Redirect(mstdout)
so, for some reason sys.displayhook was treated in a much more
aggressive way that prevents any meddling, which is sad, because
that's what i do best :)
-chad
On Jul 18, 5:40 pm, Chad Dombrova <[email protected]> wrote:
> 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
-~----------~----~----~----~------~----~------~--~---