Calling MAXScript from C++

Executing String Expressions

The global function ExecuteMAXScriptScript() can be used to execute MAXScript from C++. This function has the following signature:

BOOL        ExecuteMAXScriptScript(const MCHAR* s, MAXScript::ScriptSource scriptSource, BOOL quietErrors = FALSE, FPValue* fpv = nullptr, BOOL logQuietErrors = TRUE);

This takes a maxscript expression in the form of a string. It does not take a filename. It also takes a scriptSource parameter, indicating the source of the script, which determines what Safe Scene Script Execution policy is applied. If the string is successfully compiled and executed by the MAXScript script engine, and a pointer to an FPValue is provided, any value returned from the script is converted to an FPValue and returned through the fpv parameter.

If the string is not successfully compiled and evaluated, error messages are written to the Scripting Listener or logged using LogSys. If net rendering, or the quietError argument is TRUE and logQuietErrors is TRUE, error messages with only be logged using LogSys. If the quietError argument is TRUE and logQuietErrors is FALSE, errors are not logged.

This function returns TRUE if the script was compiled and executed successfully.

Executing Script Files

3ds Max exposes two functions for executing maxscript files from C++ code. These two methods are detailed below:

void filein_script(const MCHAR* filename = NULL);

This takes a string containing the fully qualified path to a maxscript file. If the filename parameter is NULL, then a dialog will open prompting the user to select a file.

BOOL filein_script_ex(const MCHAR* filename, MSTR* captured_error_message = nullptr, bool performEmbeddedScriptSecurityChecks = false);

This is similar to the method above, and in fact is called by filein_script. This file can handle maxscript files (*.ms), macroscript files (*.mcr), maxscript zip files (*.mzp) and encrypted maxscript files (*.mse). This method takes an extra out parameter which will contain an error message if one should occur. It also takes a boolean parameter indicating whether to consider the script embedded, which if true applies Safe Scene Script Execution policy.

Note all methods described here are found in maxscript.h and are exported from the maxscript dll file.