Community
3ds Max Programming
Welcome to Autodesk’s 3ds Max Forums. Share your knowledge, ask questions, and explore popular 3ds Max SDK, Maxscript and Python topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to call Python from MaxScript and get the value obtained in Python

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
YASUSHI_EMOTO
1367 Views, 7 Replies

How to call Python from MaxScript and get the value obtained in Python

I want to call Python from MaxScript and get the value obtained in Python.

 

If I execute the following code, I can call Python from MaxScript.

 

python.Execute "print('test')"

 

However, I will not be able to get the return value.

I tried the following code, but it did not work.

 

test = python.Execute "'one,two,three,four,five'.split(',')"
print(test[1])

 

Is there a way to get the return value in Python?

Labels (4)
7 REPLIES 7
Message 2 of 8
Swordslayer
in reply to: YASUSHI_EMOTO

Use python.import instead and call the python modules from MAXScript:

py = python.import "builtins" /* or "_builtin_" in 2020 and before */
py.str.split "one,two,three,four,five" "," as array

 

Message 3 of 8
YASUSHI_EMOTO
in reply to: Swordslayer

I have one more question.

 

I have the following Python code.
This is the code to retrieve text data from a web site.

Is it possible to call such complex Python code from MaxScript?

 

from urllib import request

response = request.urlopen('https~~~')
content = response.read()
response.close()
html = content.decode()

title = html.split('<title>')[1].split('</title')[0]
print(title)

 

Message 4 of 8
Swordslayer
in reply to: YASUSHI_EMOTO

Of course

request = python.import "urllib.request"
response = request.urlopen @"https~~~"
content = response.read()
response.close()
html = content.decode() as stringStream

skipToString html "<title>"
title = readDelimitedString html "<"
print title
Message 5 of 8
YASUSHI_EMOTO
in reply to: Swordslayer

The following code works in Python3 & max script.

py = python.Import "builtins"
splited = py.str.split "testA_testB" "_" as array

 

However, the following code will not work in Python2 & max script.
What do I need to do to split in Python2?

 

py = Python.Import "__builtin__"
splited = py.str.split "testA_testB" "_" as array

 

In Python2, I get the following error

 

TypeError: descriptor 'split' requires a 'str' object but received a 'unicode'
-- Error occurred in anonymous codeblock; filename: ; position: 74; line: 2
-- Runtime error: TypeError: descriptor 'split' requires a 'str' object but received a 'unicode'

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

 

Message 6 of 8



@YASUSHI_EMOTO wrote:

The following code works in Python3 & max script.

 

py = python.Import "builtins"
splited = py.str.split "testA_testB" "_" as array

 

However, the following code will not work in Python2 & max script.
What do I need to do to split in Python2?


 

 

splited = py.unicode.split "testA_testB" "_" as array

 

 

Message 7 of 8

@YASUSHI_EMOTO ,

Use directly a MAXScript call instead of going through Python.

FilterString "testA_testB" "_"

 

 

Eric Brosseau
Senior Software Developer, 3ds Max, Autodesk
Message 8 of 8

Thanks a lot!

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

Post to forums  

Autodesk Design & Make Report