<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do I use commandPort? in Maya Forum</title>
    <link>https://forums.autodesk.com/t5/maya-forum/how-do-i-use-commandport/m-p/10917085#M8768</link>
    <description>&lt;P&gt;Someone, please! I'm so close being able to use my &lt;A href="https://www.elgato.com/en/stream-deck" target="_blank" rel="noopener"&gt;Stream Deck&lt;/A&gt; to send commands to Maya, removing the necessity to use hotkeys, which I've ran out of since long ago &lt;STRONG&gt;... How do I get past this error?!&lt;/STRONG&gt; (Sorry for the bold text.)&lt;/P&gt;</description>
    <pubDate>Sun, 30 Jan 2022 20:34:57 GMT</pubDate>
    <dc:creator>Henrik_Cederblad</dc:creator>
    <dc:date>2022-01-30T20:34:57Z</dc:date>
    <item>
      <title>How do I use commandPort?</title>
      <link>https://forums.autodesk.com/t5/maya-forum/how-do-i-use-commandport/m-p/10866099#M8766</link>
      <description>&lt;P&gt;I wish to get started using Maya's &lt;A href="https://help.autodesk.com/cloudhelp/2022/ENU/Maya-Tech-Docs/Commands/commandPort.html" target="_blank" rel="noopener"&gt;&lt;STRONG&gt;commandPort&lt;/STRONG&gt;&lt;/A&gt; on Windows in order to trigger external (MEL) commands from my &lt;A href="https://www.elgato.com/en/stream-deck-xl" target="_blank" rel="noopener"&gt;Elgato Stream Deck&lt;/A&gt;. My only issue is, I'm really not a programmer and I don't know how to set it up.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm primarily looking for the MEL method, but a Python version would also be relevant. Here's my Python info:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; sys.version&lt;BR /&gt;'3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)]'&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; sys.executable&lt;BR /&gt;'C:\\Python39\\python.exe'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;How do I do this? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Jan 2022 03:11:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-forum/how-do-i-use-commandport/m-p/10866099#M8766</guid>
      <dc:creator>Henrik_Cederblad</dc:creator>
      <dc:date>2022-01-15T03:11:59Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use commandPort?</title>
      <link>https://forums.autodesk.com/t5/maya-forum/how-do-i-use-commandport/m-p/10917075#M8767</link>
      <description>&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="5"&gt;1.&lt;/FONT&gt; Create a &lt;STRONG&gt;userSetup.mel&lt;/STRONG&gt;&amp;nbsp;if you haven't got one already (could be py also, but we'll stick with .mel here) in&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;C:\Users\&amp;lt;user&amp;gt;\Documents\maya\&amp;lt;mayaversion&amp;gt;\scripts&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="5"&gt;2.&lt;/FONT&gt; Add the following inside that file (you may change the port numbers to anything you like):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;commandPort -name "localhost:7001" -sourceType "mel" -echoOutput;
commandPort -name "localhost:7002" -sourceType "python" -echoOutput;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="5"&gt;3.&lt;/FONT&gt; 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):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="5"&gt;4.&lt;/FONT&gt; Run the Python file. In my case, I run it like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;"C:\Python39\python.exe" "D:\3D\Maya\SCRIPTS\commandPort_Template.py"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That should work, but it doesn't. As said, I'm getting a TypeError when sending the command to Maya:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;# # 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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 20:35:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-forum/how-do-i-use-commandport/m-p/10917075#M8767</guid>
      <dc:creator>Henrik_Cederblad</dc:creator>
      <dc:date>2022-01-30T20:35:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use commandPort?</title>
      <link>https://forums.autodesk.com/t5/maya-forum/how-do-i-use-commandport/m-p/10917085#M8768</link>
      <description>&lt;P&gt;Someone, please! I'm so close being able to use my &lt;A href="https://www.elgato.com/en/stream-deck" target="_blank" rel="noopener"&gt;Stream Deck&lt;/A&gt; to send commands to Maya, removing the necessity to use hotkeys, which I've ran out of since long ago &lt;STRONG&gt;... How do I get past this error?!&lt;/STRONG&gt; (Sorry for the bold text.)&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 20:34:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-forum/how-do-i-use-commandport/m-p/10917085#M8768</guid>
      <dc:creator>Henrik_Cederblad</dc:creator>
      <dc:date>2022-01-30T20:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use commandPort?</title>
      <link>https://forums.autodesk.com/t5/maya-forum/how-do-i-use-commandport/m-p/10929435#M8769</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. I've created a &lt;A href="https://help.autodesk.com/view/MAYAUL/2022/ENU/?guid=GUID-F3D60949-2372-47F5-B8D6-78D73F78D587" target="_blank" rel="noopener"&gt;userSetup&lt;/A&gt;.py file here: &amp;lt;drive&amp;gt;:\&amp;lt;yourusername&amp;gt;\Documents\maya\&amp;lt;20XX&amp;gt;\scripts\userSetup.py&lt;/P&gt;&lt;P&gt;2. In userSetup.py, I'ved added these lines:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import maya.cmds as cmds
cmds.commandPort(name=":54321", sourceType="mel")
cmds.commandPort(name=":54322", sourceType="python")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note: Someone told me it can be a good idea to open the commandPorts using&amp;nbsp;&lt;A href="https://help.autodesk.com/view/MAYAUL/2022/ENU/index.html?guid=__CommandsPython_evalDeferred_html" target="_blank" rel="noopener"&gt;evalDeferred&lt;/A&gt;, like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;cmds.evalDeferred('cmds.commandPort(name=":54321", sourceType="mel")')
cmds.evalDeferred('cmds.commandPort(name=":54322", sourceType="python")')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Which, according to the person I talked to, was to make sure it's executed at the correct time.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;3. Then I've created a first template_command.py file with the following (creating a cube):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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&amp;nbsp;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;That's it!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Note: the above pertains to &amp;gt;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.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Feb 2022 21:45:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-forum/how-do-i-use-commandport/m-p/10929435#M8769</guid>
      <dc:creator>Henrik_Cederblad</dc:creator>
      <dc:date>2022-02-04T21:45:42Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use commandPort?</title>
      <link>https://forums.autodesk.com/t5/maya-forum/how-do-i-use-commandport/m-p/11157847#M8770</link>
      <description>&lt;P&gt;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&amp;nbsp;template_command.pyw (same if I drag that file to my viewport).&amp;nbsp;&lt;/P&gt;&lt;P&gt;For my userSetup.py I tried both version, with and without evalDeferred. And I also set the security to off in Maya's preferences.&lt;/P&gt;&lt;P&gt;Any advice on how to make this work?&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 09 May 2022 21:26:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-forum/how-do-i-use-commandport/m-p/11157847#M8770</guid>
      <dc:creator>fedepuopolo</dc:creator>
      <dc:date>2022-05-09T21:26:29Z</dc:date>
    </item>
  </channel>
</rss>

