0

Autodesk Maya 2012 提供了“mayapy”——一个修改过的 python 构建,其中包含加载 Maya 文件所需的包并充当批处理工作的无头 3D 编辑器。我从 bash 脚本中调用它。如果该脚本在其中打开一个场景文件,cmds.file(filepath, open=True)它会喷出一页页的警告、错误和其他我不想要的信息。我只想在 cmds.file 命令运行时关闭所有这些。

我尝试从我发送的 Python 命令内部重定向到 shell 脚本中的 mayapy,但这不起作用。我可以通过在对 bash 脚本的调用中将 stdout/err 重定向到 /dev/null 来使所有内容静音。有什么方法可以在对 shell 的调用中使其静音,但仍然允许我在脚本中传入的命令打印出信息?

测试.sh:

#!/bin/bash

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

调用它:

$ ./test.sh                  # spews info, then prints 'hello'
$ ./test.sh > /dev/null 2>&1 # completely silent
4

2 回答 2

2

基本上,我认为解决这个问题的最好方法是实现一个包装器,它将执行 test.sh 并清理到 shell 的输出。为了清理输出,我会简单地添加一些字符串来通知您的包装器该文本适合输出。我对包装文件的灵感来自于:https ://stackoverflow.com/a/4760274/2030274

内容如下:

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,

现在您可以想象 test.sh 类似于

#!/bin/bash

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

这只会打印 hello 行。由于我们将 python 调用包装在子进程中,因此通常显示给 shell 的所有输出都应该被捕获,并且您应该截取不需要的行。

当然,要从 python 脚本调用 test.sh,您需要确保您拥有正确的权限。

于 2013-02-02T08:40:11.873 回答
0

我知道我只是被管道扭曲了。Maya 确实将所有批处理输出发送到 stderror。一旦您正确地管道 stderr,这将完全释放 stdout。这是一个有效的全bash单线。

# 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
于 2013-02-06T00:57:52.740 回答