On Sat, Jul 18, 2009 at 17:40, Chad Dombrova<[email protected]> wrote: > > 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!
The problem here is that we are using PyObject_Unicode() to convert non-string objects to unicode strings. That's equivalent to calling the old unicode() built-in method, which is now known as str(). I tried replacing that with a call to PyObject_Repr() followed by PyUnicode_FromEncodedObject() (not sure if the second call is necessary). One problem which immediately sprang up is that the display of float values changes. To see it in action, try the following in the script editor: a=0.1 str(a) # Result: 0.1 # repr(a) # Result: 0.10000000000000001 The str() representation is preferable in this case, though there may be cases where repr() will give the better result. > 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: Maya replaces the display hook each time before executing commands from the Script Editor, or doing an eval(), and replaces the original hook afterward. So you can replace it for the duration of the execution of your commands, but you can't have it left in place after execution, which is what you want. I'm not sure why we're being so forceful about it. Possibly to ensure that calls from MEL to Python always get back values in their correct formatting. -- -deane --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/python_inside_maya -~----------~----~----~----~------~----~------~--~---
