0

Autodesk Maya 2012 provides "mayapy" - a modded build of python filled with the necessary packages to load Maya files and act as a headless 3D editor for batch work. I'm calling it from a bash script. If that script opens a scene file in it with cmds.file(filepath, open=True), it spews pages of warnings, errors, and other info I don't want. I want to turn all of that off only while the cmds.file command is running.

I've tried redirecting from inside of the Python commands I'm sending into mayapy inside the shell script, but that doesn't work. I can silence everything by redirecting stdout/err to /dev/null in the call to the bash script. Is there any way to silence it in the call to the shell, but still allow my passed-in command inside the script to print out information?

test.sh:

#!/bin/bash

/usr/autodesk/maya/bin/mayapy -c "
cmds.file('filepath', open=True);
print 'hello'
"

calling it:

$ ./test.sh                  # spews info, then prints 'hello'
$ ./test.sh > /dev/null 2>&1 # completely silent
Gary Fixler
  • 5,632
  • 2
  • 23
  • 39
  • I see you've tried pointing sys.stdout/err to a dead write method, but have you also tried pointing sys.__stdout__ and sys.__stderr__ to the dead write method as well? They typically point to the original values of stdout and stderr and maybe mayapy is using those for the print methods instead of the typical ones? – mglowe Feb 02 '13 at 07:57
  • I just gave it a shot, but it still prints out all the extra info. – Gary Fixler Feb 02 '13 at 08:01
  • Just as a sanity check: I'm doing `sys.stdout = open('/dev/null','w')`, and the same with stderr, and your two values with the underscores. – Gary Fixler Feb 02 '13 at 08:02
  • Am I right in presuming that mayapy is generating these messages and propagating them up to the calling bash script, and that's what's actually printing them out? It would seem so, as I can stop it at the bash level. – Gary Fixler Feb 02 '13 at 08:04
  • It's certainly possible that they could be propagating messages up, but it's probably more likely that they're printing the messages somewhere else all together instead of propagating them to the python layer. How much stuff are you planning to log that belongs to you, because I'm thinking of a workaround that might work. – mglowe Feb 02 '13 at 08:10
  • Not much. Just a 1 to a few lines of info per file, typically. – Gary Fixler Feb 02 '13 at 08:14

2 Answers2

2

Basically, I think the best way to solve this is to implement a wrapper that will execute test.sh and sanitize the output to the shell. To sanitize the output, I would simply prepend some string to notify your wrapper that this text is good for output. My inspiration for the wrapper file came from this: https://stackoverflow.com/a/4760274/2030274

The contents are as follows:

import subprocess

def runProcess(exe):
    p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    while(True):
      retcode = p.poll() #returns None while subprocess is running
      line = p.stdout.readline()
      yield line
      if(retcode is not None):
        break

for line in runProcess(['./test.sh']):
  if line.startswith('GARYFIXLER:'):
      print line,

Now you could imagine test.sh being something along the lines of

#!/bin/bash

/usr/autodesk/maya/bin/mayapy -c "
cmds.file('filepath', open=True);
print 'GARYFIXLER:hello'
"

and this will only print the hello line. Since we are wrapping the python call in a subprocess, all output typically displayed to the shell should get captured and you should intercept the lines that you don't want.

Of course, to call test.sh from a python script, you need to make sure you have the correct permissions.

Community
  • 1
  • 1
mglowe
  • 134
  • 5
0

I knew I was just getting twisted around with pipes. Maya is indeed sending all batch output to stderror. This frees stdout entirely once you properly pipe stderr away. Here's an all-bash one-liner that works.

# load file in batch; divert Maya's output to /dev/null
# then print listing of things in file with cmds.ls()
/usr/autodesk/maya/bin/mayapy -c "import maya.standalone;maya.standalone.initialize(name='python');cmds.file('mayafile.ma', open=True);print cmds.ls()" 2>/dev/null
Gary Fixler
  • 5,632
  • 2
  • 23
  • 39