2

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"!

2 Answers2

2

Try:

command = 'import maya.cmds as mc\n mc.polyCube()'
pajton
  • 15,828
  • 8
  • 54
  • 65
  • Thanks, that worked! but what is the workflow if I want to actually send an entire 20 or 30 lined script to Maya? Am I forced to put a "\n" character after each command or statements! – Mohammad Farjamfard Jun 26 '11 at 18:43
  • `\n` is a newline character. You should enter it in your command every time you need to separate it with a new line. – pajton Jun 26 '11 at 19:44
  • I want to some what automate this process; for example by saving the file first and then read each line and add the "\n" character at the end of the lines. Is that possible? – Mohammad Farjamfard Jun 27 '11 at 10:24
1

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()"
TorelTwiddler
  • 5,996
  • 2
  • 32
  • 39