The Python Core Interface exposes the Python engine's controls to MAXScript. Available in 3ds Max 2015 and higher.
For examples of how to use this interface, see Executing Python from MAXScript
Properties:
<python>.Import : value : Read|Write
This property points to the Import() function used to import a native Python module for use in MAXScript.
<python>.Reload : value : Read|Write
This property points to the Reload() function used to reload a native Python module. This can be useful if the module has become "dirty", or some members have been re-defined.
Methods:
<enum>python.Init throwOnError:<boolean> Init enums: {#success|#pathError|#argumentError|#initError|#scriptFileError|#executeError Init - no automatic redraw after invoked throwOnError default value: true
Initializes the Python engine and returns #success .
If the initialization failed, returns an enum describing the cause of the failure - path error, initialization error, script file error or execution error.
If the optional argument throwOnError: is set to False, no error will be thrown if one is encountered.
<enum>python.Execute <string>script fileName:<filename> throwOnError:<boolean> clearUndoBuffer:<boolean> Execute enums: {#success|#pathError|#argumentError|#initError|#scriptFileError|#executeError fileName default value: undefined throwOnError default value: true clearUndoBuffer default value: true
Executes the Python script specified by the string in the first argument, or, if the optional fileName: argument is provided, executes the specified Python script file.
Returns #success if the execution was successful, or one of the other enums describing the cause of the failure - path error, initialization error, script file error or execution error.
If the optional argument throwOnError: is set to False, no error will be thrown if one is encountered.
If the optional argument clearUndoBuffer: is set to False, the Undo Buffer will not be cleared before execution.
<enum>python.ExecuteFile <filename>fileName throwOnError:<boolean> clearUndoBuffer:<boolean> ExecuteFile enums: {#success | #pathError | #initError | #scriptFileError | #executeError} throwOnError default value: true clearUndoBuffer default value: true
Executes the Python script file specified by the first argument.
Returns #success if the execution was successful, or one of the other enums describing the cause of the failure - path error, initialization error, script file error or execution error.
If the optional argument throwOnError: is set to False, no error will be thrown if one is encountered.
If the optional argument clearUndoBuffer: is set to False, the Undo Buffer will not be cleared before execution.
<value>python.Import <string>moduleName throwOnError:<boolean> throwOnError default value: true
Imports the specified Python module for use in MAXScript.
If the optional argument throwOnError: is set to False, no error will be thrown if one is encountered.
Available in 3ds Max 2017 and higher
-- import the Python built-in methods and constants bi = Python.Import "__builtin__" -- <module '__builtin__' (built-in)> bi.pow 2 3 -- 8 -- use Python dictionary type d = bi.dict one:1 two:2 three:3 -- {u'one': 1, u'three': 3, u'two': 2} -- {u'one': 1, u'three': 3, u'two': 2} d["one"] -- 1 <value>python.Reload <value>module throwOnError:<boolean> throwOnError default value: true
Reloads the specified Python module. This can be useful if a module keyword gets re-defined.
If the optional argument throwOnError: is set to False, no error will be thrown if one is encountered.
Available in 3ds Max 2017 and higher
<string>python.GetLastError()
Returns the last error message as string.
This is especially useful when executing with throwOnError: set to False - if the return value is not #success , you can use this method to display your own error message without throwing an error.
EXAMPLE:
``` ( result = python.Init throwOnError:false if result == #success do ( result = python.Execute "print "Hello World!"" throwOnError:false ) if result != #success then ( format "Python Initialization Failed due to %\n" (result as string) format "ERROR: %\n" (python.GetLastError()) ) else format "The Python Execution Was A World-Wide Success!\n" OK )
```
OUTPUT:
``` Hello World! The Python Execution Was A World-Wide Success! OK
```
CHANGE LINE 5 - MISTYPE PRINT AS PRUNT:
``` result = python.Execute "prunt "Hello World!"" throwOnError:false
```
OUTPUT:
``` Python Initialization Failed due to executeError ERROR: <type 'exceptions.SyntaxError'> invalid syntax (, line 1) OK
```