MSX variable in Python script


#1

Hello!

i’m trying to use my MSX variable into a python script but it doesn’t seem to work.
Here’s my python code:
python.Execute("
import sys

SCRIPT_FOLDER = ‘c:\mypath\myscript’
if SCRIPT_FOLDER not in sys.path:
sys.path.append(SCRIPT_FOLDER)

import myscript_ui
myscript_ui.generate_ui()
")

my MSX code is pretty simple:
global pathToScript = @“c:\mypath\myscript”

How can I replace SCRIPT_FOLDER = ‘c:\mypath\myscript’ with pathToScript ?
Thank you!


#2

Hi.
I’m new to python, so I hope this helps with your problem.
As far as I know python does not get windows paths with "\" separation between directories. You have to use a structure like 'c:\\mypath\\myscript' or r'c:\mypath\myscript', or completely replace "\" with "/": so the windows path is like: 'c:/maypath/mayscript'.
It would also help to convert the pathToScript variable in mxs code to a more python friendly structure. Instead of using the "@" symbol try defining the path like: "c:\\mypath\\myscript". If that doesn’t help, try modifying the path string format in python.


#3

To access maxscript in python you need to import pymxs and use the runtime module.

For python to assign a variable to the runtime module, the variable needs to exist globally in maxscript memory first.

Define in maxscript first:

Global pathToScript = undefined

Then in python:

from pymxs import runtime as mxRt

mxRt.pathToScript = "your\\path"
print(mxRt.pathToScript)

Then back in maxscript:

print(pathToScript)