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?
Solved! Go to Solution.
Solved by Swordslayer. Go to Solution.
Solved by Swordslayer. Go to Solution.
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
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)
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
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
@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
Use directly a MAXScript call instead of going through Python.
FilterString "testA_testB" "_"
Can't find what you're looking for? Ask the community or share your knowledge.