Extracting data from Output Window

1,715 views
Skip to first unread message

Mark Serena

unread,
Mar 2, 2012, 5:25:51 AM3/2/12
to python_in...@googlegroups.com
[Sorry if this is a duplicate didn't see it on the main page a day later]

Hey guys,

Is there a way to extract information from the output window? I want to do some graphing/plotting.

Thanks
Mark

Justin Israel

unread,
Mar 5, 2012, 1:14:25 PM3/5/12
to python_in...@googlegroups.com
Do you mean the script editor? First you should check if you can actually just catch the output of the commands you are running. If the output is completely out of your control and you really do want to capture what the script editor is spitting out, then you can register a callback using the API...

### code start ###

import maya.OpenMaya as om

def catchOutput(msg, *args):
    with open('maya.log', 'a') as f:
        f.write("Caught: %s\n" % msg)

# set the callback and save the id
catchOutput_id = om.MCommandMessage.addCommandOutputCallback(catchOutput)

# use the id to remove the callback later
om.MCommandMessage.removeCallback(catchOutput_id)

### code end ###

Obviously you would do something else, rather that just logging it to a file. Don't do any printing in that callback, as I have noticed it generates a recursive loop and crashes maya.



Message has been deleted

Chad Dombrova

unread,
Mar 5, 2012, 7:04:04 PM3/5/12
to python_in...@googlegroups.com
if you're talking about the Output Window that comes with Maya on Windows, then that is stdout and stderr, which are two special files/streams that any application can write to.  On linux or osx, when you start an application you can pipe its output to a file or another application.  I don't know about windows, but i'm sure there is something similar.

Typically renderers will allow you to specify a file for render logs.  If it's MR, then there is probably some magic, undocumented environment variable that does it.

why is this forum so slow to update? I posted a topic on Friday and I don't see it till Tuesday morning

first time posters are moderated.  your moderator did not check his email over the weekend ;)  if this was not your first time posting, perhaps you used a different email address.
 
and then i replied to this a few hours ago and it doesn't appear....is it just me?

don't know about the delay in the reply, but in case it helps clear anything up, by default you won't receive your own responses by email.


-chad



Justin Israel

unread,
Mar 5, 2012, 7:50:09 PM3/5/12
to python_in...@googlegroups.com
The code I suggested earlier will work for software renders, and, for anything that maya batch reports back to maya's script editor. Maya batch does write to a log file which you could tail, and get your output from there. I'm not sure where it lives on windows, but on OSX it lives in the user home directory here:  ~/Library/Logs/Maya/mayaRender.log

Specifically on windows, if you have the output window showing this info, its being populated by the subprocess of Maya Batch. So, really the logfile is your best approach I think.



Mark Serena

unread,
Mar 5, 2012, 7:56:06 PM3/5/12
to python_in...@googlegroups.com
I asked a programmer at work this morning, he doesn't use Python but he did come up with this.


import sys, os

class Tee:

def __init__(self, filename):

log = open(filename, "w")

self.prevfd = os.dup(sys.__stdout__.fileno())

os.dup2(log.fileno(), sys.__stdout__.fileno())

self.prev = sys.__stdout__

sys.__stdout__ = os.fdopen(self.prevfd, "w")

def __exit__(self):

restore()

def restore():

os.dup2(self.prevfd, self.prev.fileno())

sys.__stdout__ = self.prev

 
# begin redirect logging to file

tee = Tee("c:\\log.txt")


So at the moment it does what I'm after but it completely redirects the output window and now there isn't any output in the console, I don't know python well enough at the moment to take it further.

Is there a way to keep both? 


Thanks guys,

Mark Serena

unread,
Mar 5, 2012, 10:16:25 PM3/5/12
to python_in...@googlegroups.com
That doesn't work for me on windows. 
Yeah i read that the scriptEditor uses stdout and console window uses __stdout__ don't know if thats a Maya thing or a platform thing.


On Tuesday, March 6, 2012 1:25:51 PM UTC+11, Justin Israel wrote:
I'm not sure how that python snippet was working for you. It seems kind of broken. Also, its not really doing a tee, because I believe the os.dup2 is actually closing the original __stdout__. So its really only writing to your new file, as you noticed. Also, it looks like he was trying to do a context manager with the __exit__ method, yet no __enter__ method and not using it in a "with" statement, so it basically does nothing right now.

It looks to me like sys.__stdout__ is assigned and used differently on a windows machine than on osx, because I don't get the same output flowing to that descriptor that you seem to, but if I had to suggest a version of that code, it might be this:


You just assign a file-like interface to __stdout__ with a write() method that writes to both your own file, and the original file descriptor. See if that works for you.

On Tuesday, March 6, 2012 1:25:51 PM UTC+11, Justin Israel wrote:
I'm not sure how that python snippet was working for you. It seems kind of broken. Also, its not really doing a tee, because I believe the os.dup2 is actually closing the original __stdout__. So its really only writing to your new file, as you noticed. Also, it looks like he was trying to do a context manager with the __exit__ method, yet no __enter__ method and not using it in a "with" statement, so it basically does nothing right now.

It looks to me like sys.__stdout__ is assigned and used differently on a windows machine than on osx, because I don't get the same output flowing to that descriptor that you seem to, but if I had to suggest a version of that code, it might be this:


You just assign a file-like interface to __stdout__ with a write() method that writes to both your own file, and the original file descriptor. See if that works for you.

Justin Israel

unread,
Mar 5, 2012, 10:21:06 PM3/5/12
to python_in...@googlegroups.com
Does it not print to either location?

For me, it worked fine in capturing sys.stdout (when I changed it to that file descriptor instead), as I think on OSX the console output is hijacked to the OSX console.app when you launch maya from an Icon. I can't speak for windows, but I do think the __stdout__ descriptor is platform specific. OSX wants to make sure its routed to the systems central logging facility. 

Have you tried looking into whether you can simply tail the mayaRender.log (if it exists for you) ?

Mark Serena

unread,
Mar 5, 2012, 10:42:20 PM3/5/12
to python_in...@googlegroups.com
I get an empty log.txt file but i keep the output window feedback...so original functionality. 
mayaRenderLog.txt is only for batch rendering afaik.I want to be able to get my current render data from within Maya. 

Mark Serena

unread,
Mar 12, 2012, 5:32:00 AM3/12/12
to python_in...@googlegroups.com
found this online...adding this to the window path works aswell. 
"c:\..\..\...\bin\maya.exe" -log c:\debug.txt

much simplier. thanks guys for the help. I really appreciate it, I'll be back later to ask questions about plotting I'm sure. :p

Panupat Chongstitwattana

unread,
Mar 13, 2012, 5:26:48 AM3/13/12
to python_in...@googlegroups.com
where exectly do you add that command at?


--

Mark Serena

unread,
Mar 13, 2012, 6:13:44 AM3/13/12
to python_in...@googlegroups.com
Right click your Maya shortcut and choose properties and in the target box add this to the end -log c:\debug.txt 
Should look like this:
"C:\Program Files\Autodesk\Maya2012\bin\maya.exe" -log c:\debug.txt

ChrisV

unread,
Mar 15, 2012, 12:03:18 PM3/15/12
to python_inside_maya
Is there a way to redirect the Output Window info or stdout to a Qt
interface. We have an exporter plugin which kicks info to the Output
Window, but I'd like to pipe this to an exporter UI which I've
created.

On Mar 13, 5:13 am, Mark Serena <markjser...@gmail.com> wrote:
> Right click your Maya shortcut and choose properties and in the target box
> add this to the end -log c:\debug.txt
> Should look like this:
> *"C:\Program Files\Autodesk\Maya2012\bin\maya.exe" -log c:\debug.txt*

Justin Israel

unread,
Mar 15, 2012, 5:40:24 PM3/15/12
to python_in...@googlegroups.com, python_inside_maya
If the only way on to redirect the output on windows is to a file using the log flag, then you can have your qt window just tail that file.

Macbeth R.

unread,
May 31, 2013, 7:46:02 PM5/31/13
to python_in...@googlegroups.com
The code posted by Mark Serena worked for me on Linux.

Anyone figured out how to redirect that code to sys.stdout so it canr print in script editor?

Justin Israel

unread,
Mar 5, 2012, 9:25:51 PM3/5/12
to python_in...@googlegroups.com
I'm not sure how that python snippet was working for you. It seems kind of broken. Also, its not really doing a tee, because I believe the os.dup2 is actually closing the original __stdout__. So its really only writing to your new file, as you noticed. Also, it looks like he was trying to do a context manager with the __exit__ method, yet no __enter__ method and not using it in a "with" statement, so it basically does nothing right now.

It looks to me like sys.__stdout__ is assigned and used differently on a windows machine than on osx, because I don't get the same output flowing to that descriptor that you seem to, but if I had to suggest a version of that code, it might be this:


You just assign a file-like interface to __stdout__ with a write() method that writes to both your own file, and the original file descriptor. See if that works for you.

Reply all
Reply to author
Forward
0 new messages