通过Python Socket向Maya发送多行命令

时间:2011-06-26 16:08:02

标签: python maya

我想知道有没有办法通过python套接字和Maya自己的“commandPort”命令向maya发送多行命令?

我正在使用下面的代码将代码发送到maya(“message”值是命令):

import socket
#HOST = '192.168.1.122' # The remote host

HOST = '127.0.0.1' # the local host
PORT = 54321 # The same port as used by the server
ADDR=(HOST,PORT)

def SendCommand():
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(ADDR)
    command = 'import maya.cmds as mc mc.polyCube()' # the commang from external editor to maya

    MyMessage = command
    client.send(MyMessage)
    data = client.recv(1024) #receive the result info
    client.close()

    print 'The Result is %s'%data

if __name__=='__main__':
     SendCommand()

当我发送像'polyCube()'这样的单个命令时,它可以工作,但是例如发送一个python 命令如:

import maya.cmds as mc 
mc.polyCube()

引发“无效的语法错误”!

2 个答案:

答案 0 :(得分:2)

尝试:

command = 'import maya.cmds as mc\n mc.polyCube()'

答案 1 :(得分:1)

对于向Maya发送小命令,@ pajton的方法有效,或者您可以使用;作为分隔符:

command = "import maya.cmds as mc; mc.polyCube()"

如果可能,一次发送多行的最简单方法是创建一个Maya可以访问的单独.py文件。

command = "import sys; sys.append(r'c:\path to my_script');"
command += "import my_script; my_script.run()"