Since there's been no reply, I'll fill in the details I've gathered so far. Still haven't got it to work as I'm getting a TypeError when executing a command, but maybe someone can help me understand if this is a bug in Maya or something I can work around?
1. Create a userSetup.mel if you haven't got one already (could be py also, but we'll stick with .mel here) in
C:\Users\<user>\Documents\maya\<mayaversion>\scripts
2. Add the following inside that file (you may change the port numbers to anything you like):
commandPort -name "localhost:7001" -sourceType "mel" -echoOutput;
commandPort -name "localhost:7002" -sourceType "python" -echoOutput;
3. Then create a Python file (in this example I'll call it commandPort_Template.py). Note: As I wish to send MEL commands, I'll use the MEL port (7001) as assigned in Step 2):
import socket
HOST = '127.0.0.1'
PORT = 7001
ADDR=(HOST,PORT)
def SendCommand():
client = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
command = 'polyCube'
client.send(command)
client.close()
if __name__=='__main__':
SendCommand()
4. Run the Python file. In my case, I run it like so:
"C:\Python39\python.exe" "D:\3D\Maya\SCRIPTS\commandPort_Template.py"
That should work, but it doesn't. As said, I'm getting a TypeError when sending the command to Maya:
# # File "C:\Program Files\Autodesk\Maya2022\Python37\lib\socketserver.py", line 720, in __init__
self.handle()
# TypeError: a bytes-like object is required, not 'str'
# ----------------------------------------
# ----------------------------------------
# Exception happened during processing of request from ('127.0.0.1', 54756)
# Traceback (most recent call last):
# # File "C:\Program Files\Autodesk\Maya2022\Python37\lib\socketserver.py", line 316, in _handle_request_noblock
self.process_request(request, client_address)
# # File "C:\Program Files\Autodesk\Maya2022\Python37\lib\socketserver.py", line 347, in process_request
self.finish_request(request, client_address)
# # File "C:\Program Files\Autodesk\Maya2022\Python37\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
# # File "C:\Program Files\Autodesk\Maya2022\Python37\lib\site-packages\maya\app\general\CommandPort.py", line 134, in handle
self.wfile.write(self.server.commandMessageQueue.get() + self.resp_term)
# # File "C:\Program Files\Autodesk\Maya2022\Python37\lib\socketserver.py", line 799, in write
self._sock.sendall(b)