Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

(MAX 2020) Is there any chance to cast MAX String to python str?

(MAX 2020) Is there any chance to cast MAX String to python str?

denisT.MaxDoctor
Advisor Advisor
954 Views
9 Replies
Message 1 of 10

(MAX 2020) Is there any chance to cast MAX String to python str?

denisT.MaxDoctor
Advisor
Advisor

for example, this one doesn't work in 2020:

 

 

 

bi = python.import "__builtin__"
mystrlist = bi.str.split "an example string"

 

 

 

 

TypeError: descriptor 'split' requires a 'str' object but received a 'unicode'
-- Runtime error: TypeError: descriptor 'split' requires a 'str' object but received a 'unicode'

 

This of course works in MAX 2023, but I need to get working something like this in MAX 2020:

 

bi = python.import "builtins"
qc = python.import "PySide2.QtCore"

b = bi.str.encode "1234" "utf-8"
qc.QByteArray.fromBase64 b

 

 

Any ideas? 

 

 
0 Likes
Accepted solutions (1)
955 Views
9 Replies
Replies (9)
Message 2 of 10

Swordslayer
Advisor
Advisor
Accepted solution

 

mystrlist = bi.unicode.split "an example string" as array

alternatively

str = python.import "string"
mystrlist = str.split "an example string" as array

 

0 Likes
Message 3 of 10

denisT.MaxDoctor
Advisor
Advisor

Thank you. 

But I asked wrong.
Actually I need to turn MAX String into python str as many python and Qt methods require str.
For example QByteArray...

py = python.import "builtins"
qc = python.import "PySide2.QtCore"

bs = py.str.encode "1234" "utf-8"
ba = qc.QByteArray b
bb = qc.QByteArray.fromBase64 b
0 Likes
Message 4 of 10

Swordslayer
Advisor
Advisor

Since it accepts bytearray, you can use that:

 

str = "an example string"
bs = py.bytearray (for i = 1 to str.count collect py.ord str[i])
ba = qc.QByteArray bs
0 Likes
Message 5 of 10

denisT.MaxDoctor
Advisor
Advisor

here is another example that works fine in 2023 and doesn't work in 20202:

qcore = python.import "PySide2.QtCore"
ico = getdir #maxroot + @"\icons\icon_main.ico"
f = qcore.QFile ico
f.open qcore.QIODevice.ReadOnly
data = f.readAll()
f.close()

 

and as I can guess there is the same problem... the way how these versions convert MAX String to python str (bytes, bytearray).

see:

2020:

py = python.import "__builtin__"

py.bytes "test" "utf-8"
TypeError: str() takes at most 1 argument (2 given)
-- Runtime error: TypeError: str() takes at most 1 argument (2 given)

-- MAXScript callstack:
--	thread data: threadID:28720
--	------------------------------------------------------
--	[stack level: 0]
--	In top-level

py.bytes "test"
-- "test"

py.bytearray "test"
TypeError: unicode argument without an encoding
-- Runtime error: TypeError: unicode argument without an encoding

-- MAXScript callstack:
--	thread data: threadID:28720
--	------------------------------------------------------
--	[stack level: 0]
--	In top-level
py.bytearray "test" "utf-8"
-- test

 

2023:

py = python.import "builtins"

py.bytes "test" "utf-8"
py.bytearray "test" "utf-8"

-- b'test'
-- bytearray(b'test')

 

 

0 Likes
Message 6 of 10

denisT.MaxDoctor
Advisor
Advisor

Oh!!!

technically the bytearray conversion works, but doesn't want to show in the Listener. Internal conversion seems to work:

 

 

 

ico = getdir #maxroot + @"\icons\icon_main.ico"

(
	f = qcore.QFile ico
	f.open qcore.QIODevice.ReadOnly
	data = f.readAll()
	f.close()
	dialog.setWindowIcon (qui.QIcon (qui.QPixmap (qui.QImage.fromData data)))
)

 

 

0 Likes
Message 7 of 10

Swordslayer
Advisor
Advisor

Max-side behavior haven't changed between versions, that's a difference between python 2 where str was same as bytes and python 3 (2021+ default) where str is unicode and so most everything accepts unicode.

 

That's why I constructed bytearray that way.

Message 8 of 10

denisT.MaxDoctor
Advisor
Advisor

I assumed something like this, but the main thing that confused me is this:

ico = getdir #maxroot + @"\icons\icon_main.ico"
f = qcore.QFile ico
f.open qcore.QIODevice.ReadOnly
data = f.readAll()
-- <Error getting string representation>

 

...when I called the code line by line in the listener. All others were attempts to solve the "problem". 😉

0 Likes
Message 9 of 10

denisT.MaxDoctor
Advisor
Advisor

@Swordslayer wrote:

Max-side behavior haven't changed between versions, that's a difference between python 2 where str was same as bytes and python 3 (2021+ default) where str is unicode and so most everything accepts unicode.


Auto-converting of MXS strings is not a good idea. I'd rather have both str and unicode, which makes more sense to me.

0 Likes
Message 10 of 10

denisT.MaxDoctor
Advisor
Advisor

@Swordslayer wrote:

 

alternatively

 

 

str = python.import "string"
mystrlist = str.split "an example string" as array

 

 

 


I have looked again at this message, and I see that I missed a nice solution ... thanks and sorry for the remissness 
👍

0 Likes