繁体   English   中英

在一个系统中运行的Python脚本将在另一个系统中执行结果

[英]Python script running in one system will perform the result in another system

我正在尝试编写一个python脚本,该脚本在执行时会在另一台计算机上打开Maya文件并在其中创建其playblast。 这可能吗? 另外,我还要补充一点,我使用的系统都是Windows。 谢谢

为了在远程计算机上执行某些操作,您必须在其中运行某种服务。

如果它是Linux机器,则只需通过ssh连接并运行命令。 在python中,您可以使用paramiko来做到这一点

import paramiko
ssh = paramiko.SSHClient()
ssh.connect('127.0.0.1', username='foo', password='bar')
stdin, stdout, stderr = ssh.exec_command("echo hello")

否则,您可以使用python服务,但必须事先运行它。 您可以使用前面提到的Celery或ZeroMQ ,或更简单地使用RPyC

只需在目标计算机上运行rpyc_classic.py脚本,然后即可在其上运行python:

conn = rpyc.classic.connect("my_remote_server")
conn.modules.os.system('echo foo') 

或者,您可以创建自定义RPyC服务(请参阅文档)。

最后的选择是使用HTTP服务器,如先前建议的那样。 如果您不想开始安装所有内容,这可能是最简单的。 您可以使用Bottle ,这是python中的简单HTTP框架:

服务器端:

from bottle import route, run

@route('/run_maya')
def index(name):
    # Do whatever
    return 'kay'

run(host='localhost', port=8080)

客户端:

import requests
requests.get('http://remote_server/run_maya')

是的,有可能,我一直在多台计算机上执行此操作。 首先,您需要访问计算机。 已经回答了。 然后从外壳程序中调用maya,如下所示:

maya -command myblast -file filetoblast.ma

您将在脚本路径中的某处需要myblast.mel

myblast.mel:

global proc myblast(){
    playblast -widthHeight 1920 1080 -percent 100 
              -fmt "movie" -v 0 -f (`file -q -sn`+".avi"); 
    evalDeferred("quit -f");
}

在该文件中配置所需的内容,例如着色选项等。请注意,调用Maya GUI会用完一个许可证,而playblast需要该GUI(您可以通过不使用默认GUI来节省几秒钟)

廉价rpc的最后一个选择是从maya python(“ mayapy”,通常安装在maya二进制文件旁边)运行maya.standalone。 独立版本将在常规python脚本中运行,因此它可以使用KimiNewts答案中的任何远程过程技巧。

您还可以使用基本python创建自己的小型服务器。 服务器可以使用maya命令端口,也可以使用内置的wsgiref模块的wsgi服务器 是一个使用独立运行的wsgiref来通过http远程控制Maya的示例。

我们一直在处理相同的问题。 我们将Celery用作任务管理器,并且在Celery任务内部具有类似这样的代码,用于在工作机上进行爆破。 这是在Windows上完成的,并使用Python。

import os
import subprocess
import tempfile
import textwrap

MAYA_EXE = r"C:\Program Files\Autodesk\Maya2016\bin\maya.exe"

def function_name():
    # the python code you want to execute in Maya
    pycmd = textwrap.dedent('''
        import pymel.core as pm

        # Your code here to load your scene and playblast

        # new scene to remove quicktimeShim which sometimes fails to quit
        # with Maya and  prevents the subprocess from exiting
        pm.newFile(force=True)
        # wait a second to make sure quicktimeShim is gone
        time.sleep(1)
        pm.evalDeferred("pm.mel.quit('-f')")
        ''')
    # write the code into a temporary file
    tempscript = tempfile.NamedTemporaryFile(delete=False, dir=temp_dir)
    tempscript.write(pycmd)
    tempscript.close()
    # build a subprocess command
    melcmd = 'python "execfile(\'%s\')";' % tempscript.name.replace('\\', '/')
    cmd = [MAYA_EXE, '-command', melcmd]
    # launch the subprocess
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    proc.wait()
    # when the process is done, remove the temporary script
    try:
        os.remove(tempscript.name)
    except WindowsError:
        pass

暂无
暂无

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

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