简体   繁体   English

如何使特定python命令的所有输出静音?

[英]how can I silence all of the output from a particular python command?

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. Autodesk Maya 2012提供了“mayapy” - 一个python的修改版本,其中包含加载Maya文件所需的软件包,并作为批量工作的无头3D编辑器。 I'm calling it from a bash script. 我是用bash脚本调用的。 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. 如果该脚本使用cmds.file(filepath, open=True)打开其中的场景文件,它会发出警告,错误和其他我不想要的信息的页面。 I want to turn all of that off only while the cmds.file command is running. 我希望把所有这些关在cmds.file命令运行。

I've tried redirecting from inside of the Python commands I'm sending into mayapy inside the shell script, but that doesn't work. 我已经尝试从我在shell脚本中发送到mayapy的Python命令内部重定向,但这不起作用。 I can silence everything by redirecting stdout/err to /dev/null in the call to the bash script. 可以通过在调用bash脚本时将stdout / err重定向到/ dev / null 使所有内容静音。 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? 有没有办法在调用shell时使它静音,但是仍然允许我在脚本中传入的命令打印出信息?

test.sh: 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

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. 基本上,我认为解决此问题的最佳方法是实现一个包装器,它将执行test.sh并清理输出到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 我对包装文件的灵感来自于: 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 现在你可以想象test.sh就是这样的东西

#!/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. 因为我们在子进程中包装python调用,所以通常显示给shell的所有输出都应该被捕获,你应该拦截你不想要的行。

Of course, to call test.sh from a python script, you need to make sure you have the correct permissions. 当然,要从python脚本调用test.sh,您需要确保拥有正确的权限。

I knew I was just getting twisted around with pipes. 我知道我刚刚用管子扭曲了。 Maya is indeed sending all batch output to stderror. Maya确实将所有批量输出发送到stderror。 This frees stdout entirely once you properly pipe stderr away. 一旦你正确地管道stderr,这将完全释放stdout。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM