Using Python.execute() to return data to Maxscript

Using Python.execute() to return data to Maxscript

Ray.Collett
Participant Participant
1,109 Views
2 Replies
Message 1 of 3

Using Python.execute() to return data to Maxscript

Ray.Collett
Participant
Participant

Because the Execute() command only returns result codes, there is no way to return data back to a max variable, but I discovered that if the python code stores data into an environment variable, that Maxscript can then read that variable in.

 

Python:

 

import os
os.environ['pythoncache'] = myData

 

 

Maxscript:

 

returnData = systemTools.getEnvVariable 'pythoncache'

 

 

This is WAY faster than dumping data to the filesystem.

 

Example:

 

sysVar = "PYTHON_RESULT"
myName = "Ray"
pythonCode = stringstream ""
format "import os\n" to:pythonCode
format "def hello_max(username):\n" to:pythonCode
format "    message = f\"Hello {username}\"\n" to:pythonCode
format "    os.environ['%'] = message\n" sysVar to:pythonCode
format "hello_max(\"%\")\n" myName to:pythonCode
python.execute pythonCode
result = systemTools.getEnvVariable sysVar

Output:
"Hello Ray"

 

 

I hope this helps someone.

 

0 Likes
Accepted solutions (1)
1,110 Views
2 Replies
Replies (2)
Message 2 of 3

trykle
Contributor
Contributor
Accepted solution

I found the pymxs.

global teapotFromPymxs
pythonCode = "
import pymxs
rt = pymxs.runtime
t = rt.Teapot()
rt.globalVars.set(rt.name('teapotFromPymxs'), t)
"
python.execute pythonCode 


teapotFromPymxs -- return: $Teapot:...

 

0 Likes
Message 3 of 3

Ray.Collett
Participant
Participant

I admit that I hadn't spent that much time on the Python side of the help file, there needs to be a mention of the pymxs system on the Maxscript side of the help docs, specifically in the python.execute page.   The more you know!

0 Likes