Community
Maya Forum
Welcome to Autodeskโ€™s Maya Forums. Share your knowledge, ask questions, and explore popular Maya topics.
cancel
Showing results forย 
Showย ย onlyย  | Search instead forย 
Did you mean:ย 

How do I use commandPort?

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Henrik_Cederblad
2956 Views, 4 Replies

How do I use commandPort?

I wish to get started using Maya's commandPort on Windows in order to trigger external (MEL) commands from my Elgato Stream Deck. My only issue is, I'm really not a programmer and I don't know how to set it up.

 

I'm primarily looking for the MEL method, but a Python version would also be relevant. Here's my Python info:

 

>>> sys.version
'3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)]'
>>> sys.executable
'C:\\Python39\\python.exe'

 

How do I do this? ๐Ÿ™‚

4 REPLIES 4
Message 2 of 5

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)

 

 

Message 3 of 5

Someone, please! I'm so close being able to use my Stream Deck to send commands to Maya, removing the necessity to use hotkeys, which I've ran out of since long ago ... How do I get past this error?! (Sorry for the bold text.)

Message 4 of 5

For anyone looking how to use commandPort, I'm happy to report that I've got a working solution that won't give any TypeErrors (which my previous attempts gave) and now I can use my Stream Deck XL rather than running out of hotkeys for all my commands and scripts. This more or less makes Maya's shelves redundant also.

 

1. I've created a userSetup.py file here: <drive>:\<yourusername>\Documents\maya\<20XX>\scripts\userSetup.py

2. In userSetup.py, I'ved added these lines:

 

 

import maya.cmds as cmds
cmds.commandPort(name=":54321", sourceType="mel")
cmds.commandPort(name=":54322", sourceType="python")

 

 

Note: Someone told me it can be a good idea to open the commandPorts using evalDeferred, like so:

 

 

cmds.evalDeferred('cmds.commandPort(name=":54321", sourceType="mel")')
cmds.evalDeferred('cmds.commandPort(name=":54322", sourceType="python")')

 

 

Which, according to the person I talked to, was to make sure it's executed at the correct time.

3. Then I've created a first template_command.py file with the following (creating a cube):

 

 

import socket

HOST = '127.0.0.1'  # the local host
PORT = 54322  # The same port as used by the server
ADDR = (HOST, PORT)


def send_command():
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(ADDR)
    command = 'cmds.polyCube()'

    message = command
    client.send(str.encode(message))
    data = client.recv(1024)  # receive the result info
    client.close()

    print(data)


if __name__ == '__main__':
    
    # Run this within Maya to open the ports. The port can be any 5 digits
    """
    import maya.cmds as cmds
    cmds.commandPort(name=":54322", sourceType="python")
    """
    
    send_command()

 

 

4. (N.B. Windows only). To prevent a momentary console window popping up when execiting scripts, I've renamed the script so that it has the .pyw suffix rather than .py, e.g. changed template_command.py to template_command.pyw. There are other ways to accomplish this in UNIX OSes like macOS and Linux by instead keeping the .py extension and first chmod +x template_command.py ot make it executable without invoking it via a Python interpreter and then adding a shebang like #!/usr/bin/python (there are variations to this shebang line that allows better control over which Python executable you're running, etc. but I won't go into that here) at the very first line in the script.

 

That's it!

 

Note: the above pertains to >Maya 2022.3 which uses Python3. If you're on older Maya versions, they use Python2 and the code might have to be tweaked to worked for that Python version first.

Message 5 of 5

Hi, thanks for sharing this. I'm trying to implement it but following these steps Maya opens a new scene and I'm getting an error "Unrecognized file type" when doing double click on template_command.pyw (same if I drag that file to my viewport). 

For my userSetup.py I tried both version, with and without evalDeferred. And I also set the security to off in Maya's preferences.

Any advice on how to make this work?

Thanks!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report