Executing Python from MAXScript

Using Python Libraries in MAXScript

Python libraries can be imported and used in MAXScript, using Python.Import(). In this example, we import the Python built-in methods and constants, and use the pow() function:

bi = Python.Import "builtins"
--> <module 'builtins' (built-in)>
bi.pow 2 3
--> 8

If a module gets dirty (for example, a keyword gets re-defined), you can re-import using Python.Reload():

Python.Reload bi
--> <module 'builtins' (built-in)>

Simple Python types, such as int, bool, float, and str, are copied into their corresponding MAXScript types. Complex types, such as lists, tuples, dictionaries and classes, are marshalled through a wrapper object. The properties and functions of the type are accessible by name. For sequence types such as a list, you can iterate using the index. Python lists passed to MAXScript are 1-based.

For example, to create and access a Python tuple:

bi = Python.Import "builtins"
tp = bi.tuple(#(1,2,3)) --(1, 2, 3)
print tp[1] -- output: 1
print tp[0] -- output: 3, same as tp[3]
bi.type(tp) -- output: <class 'tuple'>

And to create and access a Python dict:

d = bi.dict one:1 two:2 three:3 -- {'one': 1, 'two': 2, 'three': 3}
d["four"] = 4
bi.print(d["four"]) -- 4

Here is an example of 1-based list passed to back to MAXScript from Python:

mystrlist = bi.str.split "an example string"
-- ['an', 'example', 'string']
mystrlist[1]
-- prints "an"

Limitations

Because of the way Python is wrapped in MAXScript, methods and functions bound to Python execution frames will not work as expected, if at all. Some examples of calls that will not work via Python.import() include:

Executing Python from MAXScript

You can execute Python commands and statements (can be a single command/expression or a series of statements); or a Python script (for example, a .py script) from within MAXScript. Use the python.Execute and python.ExecuteFile commands respectively, as follows:

python.Execute "print('hello')"
python.ExecuteFile "demoBentCylinder.py"

If you provide a full path using the ExecuteFile command; for example, as follows; 3ds Max looks for the Python file in the path provided.

python.ExecuteFile @"C:\Program Files\Autodesk\3ds Max 2015\scripts\Python\demoBentCylinder.py"

Otherwise, 3ds Max looks for the Python script under the following directories:

For example, 3ds Max would search under the following directories: userscripts\\python, userscripts\\startup\\python, scripts\\python, scripts\\startup\\python, and subsequently the windows paths.

The output of the results from scripts are printed to the Scripting Listener window.

Note: Python scripts run in MAXScript are not thread-safe. Python commands are always executed in the main 3ds Max thread. You should not attempt to spawn separate threads in your scripts (for example, by using the Python threading module).

Python interface methods

The MAXScript Python interface contains methods for using Python in MAXScript. If you run

showinterface python

in MAXScript, you obtain, in the Scripting Listener, a list of available methods. The following list describes these properties, methods and arguments:

Note:

These are listed as properties in the Python interface, but they behave like methods in that they take an argument (the name of the Python module) and return a reference to the imported module. See the examples above.