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 "__builtin__"
-- <module '__builtin__' (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 '__builtin__' (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("__builtin__")
tp = bi.Tuple(#(1,2,3)) --(1, 2, 3)
print tp[0] -- output: 1
bi.type(tp) -- output: <type 'tuple'>

And to create and access a Python dict:

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

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

str = python.import("string")
mystrlist = str.split("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:

  • sys._current_frames
  • sys._get_frame
  • module.globals()
  • module.locals()

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 MAXScript 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 MAXScript Listener, a list of available methods. The following list describes these methods and arguments: