问题
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"!
回答1:
Try:
command = 'import maya.cmds as mc\n mc.polyCube()'
回答2:
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()"
来源:https://stackoverflow.com/questions/6485059/sending-multiline-commands-to-maya-through-python-socket