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"
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()
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:
Path
environment variable. To see a list of these directories, enter echo %path%
in your command prompt.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.
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:
Import
: Imports a Python module.Reload
: Reloads a Python module, for example if it has been re-defined.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.
Init()
: Loads Python if it is not loaded already.
Execute()
and ExecuteFile()
: Loads Python if it is not loaded already. Use Init()
if you know that you will be using Python, and do not want the delay associated with loading it to occur the first time it is used. You would call python.Init()
in a startup script to take the load time hit at startup, rather than while actually using 3ds Max.
GetLastError()
: Returns, as a string, the error message associated with the last error encountered while running the Init()
, Execute()
, or ExecuteFile()
methods.
Result codes (enums
): When executing Python from MAXScript, the result codes can be the following constants:
#success
#pathError
#initError
#scriptFileError
#executeError
throwOnError
keyword argument: Controls whether MAXScript throws a runtime exception if an error occurs while running a method. If set to true
, you always obtain a return result code of #success
because, in all other cases, a runtime exception would be thrown. It would be up to the script writer to make the call within a try/catch in order to handle the exception.