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

发布于 2024-11-17 00:28:11 字数 845 浏览 6 评论 0原文

我想知道有没有办法通过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()

引发“无效语法错误”!

I want to know is there a way to send a multiline command to maya through python socket and the Maya's own "commandPort" command?

I'm using below code for sending the code to maya("message" value is the command):

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()

When I send a single command like 'polyCube()' it works but for example sending a python
Command such as:

import maya.cmds as mc 
mc.polyCube()

Raises an "invalid syntax error"!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮子 2024-11-24 00:28:12

尝试:

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

Try:

command = 'import maya.cmds as mc\n mc.polyCube()'
独守阴晴ぅ圆缺 2024-11-24 00:28:12

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

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

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

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

For sending small commands to Maya, @pajton's method works, or you can use ; as a separator:

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

If possible, the easiest way to send many lines at once is to create a separate .py file that Maya has access to.

command = "import sys; sys.append(r'c:\path to my_script');"
command += "import my_script; my_script.run()"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文