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"
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:
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.
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.
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:
GetLastError(): Returns, as a string, the error message associated with the last error encountered while running the Init(), Execute(), or ExecuteFile() methods.