Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Maya 2022.3 – TypeError: a bytes-like object is required, not 'str' when using commandPort

Henrik_Cederblad
Advocate

Maya 2022.3 – TypeError: a bytes-like object is required, not 'str' when using commandPort

Henrik_Cederblad
Advocate
Advocate
I'm attempting to send MEL commands to Maya via my Stream Deck using commandPort, but I'm getting TypeError: a bytes-like object is required, not 'str'. What am I doing wrong?

 

 

# # 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)

 

 

Here's my userSetup.mel:

 

 

commandPort -name "localhost:7001" -sourceType "mel" -echoOutput;
commandPort -name "localhost:7002" -sourceType "python" -echoOutput;

 

 

 

Here's my command.py example:

 

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()

 

 

And here's how I send the command to Maya:

 

 

C:\Python39\python.exe "D:\3D\Maya\SCRIPTS\command.py"

 

 

I'd be so thankful if someone could help me get past this error ... I'm so close reaching Maya/Stream Deck nirvana.
 
(Maya 2022.3 / Windows 10)
0 Likes
Reply
5,262 Views
14 Replies
Replies (14)

Henrik_Cederblad
Advocate
Advocate

The thing that looks weird to me, is that ('127.0.0.1', 54756) in the error message. I'm not using port 54756 (this number also seems to differ between launches) ... I'm using 7001 as set in my userSetup.mel and command.py file (?)

0 Likes

Henrik_Cederblad
Advocate
Advocate

I was told by a person on Reddit that the culprit may be that the command sent to Maya needs to be encoded, by changing the line

 

    client.send(command)

 

 to

 

client.send(str.encode(command))

 

 

... Alas, that results in nothing happening at all; no output in Maya.

 

Please, dear community. I need to know the proper way of setting a server up, executing a command via a script and if there's any gothas when invoking that script. E.g. I'm using Python39 to run the script while Maya2022 uses Python37; does that make any difference? ... How do you get this to work? Right now, I'm all at loss here. Not a programmer, but have a desperate need to use my Stream Deck to send commands since I've ran out of bindable hotkeys in Maya!

0 Likes

m.oumoumad
Participant
Participant

Getting the same error here, trying to send commands from Node using this code : 

 

var net = require('net');
var socketPython;
var pythonPort=7002; 
var mayahost='localhost';
 
socketPython = net.createConnection(pythonPort,mayahost);
socketPython.on('error', function(error) { 
      console.log("Unable to connect to port "+ pythonPort +" on Host "+ mayahost+"  in maya for Python " + error.code);
   });
socketPython.on('data', function(data){
      console.log(data.toString());
  });

socketPython.write("print('Hello')");
socketPython.write('\n');

The code I used to open the command port from Maya 2022 (with Python 3.7) : 

import maya.cmds as cmds
cmds.commandPort(name=":7002", sourceType="python", echoOutput=True)

 Of course this works with no issue on Maya 2020 (with python 2.7)

0 Likes

Mark.Bailey9RLE7
Observer
Observer

Did you ever find out what this was? I'm getting the same thing. Code is fine in 2018, borked in 2022. 

Mark.Bailey9RLE7
Observer
Observer

I think that random port number is what the machine uses to communicate once it's finished the initial handshake on the user defined port. 

0 Likes

Mark.Bailey9RLE7
Observer
Observer

Ok, got this to work in 2022 with some help from the internet. 
Two things:

echoOutput needs to be False:
cmds.commandPort(name=":7002", sourceType="python", echoOutput=False)

and anything you send it needs to be bytes
ie:

cube = b'cmds.polyCube()'

 

0 Likes

stevejonn
Community Visitor
Community Visitor

The reason for this error is that in Python 3, strings are Unicode, but when transmitting on the network, the data needs to be bytes instead. We can convert bytes to string using bytes class decode() instance method, So you need to decode the bytes object to produce a string. In Python 3 , the default encoding is "utf-8" , so you can use directly:

 

b"python byte to string".decode("utf-8")

 

Python makes a clear distinction between bytes and strings . Bytes objects contain raw data — a sequence of octets — whereas strings are Unicode sequences . Conversion between these two types is explicit: you encode a string to get bytes, specifying an encoding (which defaults to UTF-8); and you decode bytes to get a string. Clients of these functions should be aware that such conversions may fail, and should consider how failures are handled.

 

gamidk262
Community Visitor
Community Visitor

use this python script to open port in maya

import maya.cmds as mc 
mc.commandPort(name="127.0.0.1:7002", stp="python", echoOutput=True)
mc.commandPort(name="127.0.0.1:7001", stp="mel", echoOutput=True)

 

0 Likes

Fuchs_Leyenda
Participant
Participant

For people facing this issue with Maya 2023, there is a bug in socketserver that makes the commands not work with echoOutput,

Use this python script instead:

import maya.cmds as cmds
cmds.commandPort(name="127.0.0.1:7002", stp="python")
cmds.commandPort(name="127.0.0.1:7001", stp="mel")

 

You can also use MEL:

commandPort -name "localhost:7001" -sourceType "mel";


Keep in mind you need a new maya instance for this if you have already executed the code. Just do not call for echoOutput.

klawchi
Enthusiast
Enthusiast

I am trying to send a python print command through VSCode (MayaCode extension) and I get this error:

 

 

commandPort -name "localhost:7001" -sourceType "mel";
# ----------------------------------------
# Exception occurred during processing of request from ('127.0.0.1', 52825)
# Traceback (most recent call last):
# #   File "C:\Program Files\Autodesk\Maya2024\Python\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
# #   File "C:\Program Files\Autodesk\Maya2024\Python\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
# #   File "C:\Program Files\Autodesk\Maya2024\Python\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
# #   File "C:\Program Files\Autodesk\Maya2024\Python\lib\socketserver.py", line 747, in __init__
    self.handle()
# #   File "C:\Program Files\Autodesk\Maya2024\Python\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\Maya2024\Python\lib\socketserver.py", line 826, in write
    self._sock.sendall(b)
# TypeError: a bytes-like object is required, not 'str'
# ----------------------------------------

 

 

Is it fixable? 

0 Likes

zhalice2012
Community Visitor
Community Visitor

To debug Python code in Maya 2022 or above using VS Code, you can use the "Debugger for Maya" extension available here: https://marketplace.visualstudio.com/items?itemName=zhalice2011.mayadebugger)

 

For guidance on how to use it, check out this video tutorial: (https://www.youtube.com/watch?v=mIKi2tZP0JM)

0 Likes

office
Participant
Participant

Quite easy to fix:
Just make sure you set the ports right in your MayaCode Extension settings:

office_0-1717159368357.png

Then execute following python command in maya:

import maya.cmds as cmds
cmds.commandPort(name="127.0.0.1:7001", stp="mel")
cmds.commandPort(name="127.0.0.1:7002", stp="python")

worked for me.

0 Likes

office
Participant
Participant

Quite easy to fix:
Just make sure you set the ports right in your MayaCode Extension settings:

office_0-1717159368357.png

Then execute following python command in maya:

 

import maya.cmds as cmds
cmds.commandPort(name="127.0.0.1:7001", stp="mel")
cmds.commandPort(name="127.0.0.1:7002", stp="python")

 

worked for me.

0 Likes

guido_gonzalezU9EFY
Observer
Observer

Did you try to setting up this code in userSetup.py? Seems to do nothing into my maya.

0 Likes