Telnet or Socket: no result back from Maya


#1

Hello,

I am currently writing a little external application with python in order to be able to communicate with Maya to retrieve information such as the current opened project name, the frame start and end frame to render, stuff like that.

Since now, establishing a connection with commandPort on the Maya side and telnet or socket on the python side is fine. I can send mel or python commands just fine but never do I receive anything back from Maya except for “None” and ([] or square). Actually yes, I get all the error messages and the commands if I set echoOutput=True.

Anyone has an idea about what’s wrong?
Huge thanks!

Maya 2014 (python 2.7)
MacPro – Yosemite
python 3.5, PyQt 5, Qt 5.5 and PyCharm 5

Maya:
import maya.cmds as mc
mc.commandPort(name=“127.0.0.1:7777”, ,echoOutput=True, sourceType=“python”)

Pycharm
maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
maya.connect((“127.0.0.1”, 7777))
message = bytes(“import maya.cmds as mc;mc.file(query=True,sceneName=True)”, “utf-8”)
maya.write(message)
data = maya.recv(4096)
stringData = data.decode(“utf-8”, “ignore”)
print (‘The Result is: %s’%stringData)
maya.close()


The Result is: None


#2

I found the answer!

For those interested, if you want to send multiple commands and receive the messages back from maya, you need to send each command on a separate line and receive the message each time, not just at the end.
For example In PyCharm, I would write

import socket

maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
maya.connect((“127.0.0.1”, 7777))
maya.send(bytes("import maya.cmds as cmds
", “utf-8”))
data = maya.recv(4096)
maya.send(bytes("cmds.file(query=True, sn=True)
",“utf-8”))
data = maya.recv(4096)
print (data.decode(“utf-8”))

result:
V/Volumes/MacPro Data/Planetarium/Rotation Order/ISS test Rotation Order Python.mb
p1
.
\0

Even better is using pickle for python 3.5 or cPickle for python 2.7

import socket
import pickle

maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
maya.connect((‘localhost’, 6001))
maya.send(bytes("import maya.cmds as cmds
", “utf-8”))
ret = maya.recv(1024)
maya.send(bytes("cmds.file(query=True, sn=True)
",“utf-8”))
ret = maya.recv(1024)
print (pickle.loads(ret))

result:

/Volumes/MacPro Data/Planetarium/Rotation Order/ISS test Rotation Order Python.mb