Including Scripts Within Scripts

MAXScript provides a compile-time source-file include mechanism, allowing you to break up large scripts into smaller files that can be included at nearly any point in a script. You can use the include <file> construct at any point in your code to effectively insert the contents of a file at that point in your code.

By convention, scripts that are only included in other scripts, and not intended to run on their own, use the .mxs extension. This extension an extension that is supported by the MAXScript editor's file open/save dialog, but not the Scripting > Run Script dialog. Colorization and autocomplete are supported in the MAXScript editor for .mxs files.

The form is:

include "filename_string"

If filename_string contains a path, the file is looked for in that path. See File Access Function Search Behavior for a list of directories the filename_string is searched for if the full path is not specified.

This is a compile-time construct, therefore the file name specification must be a string literal, and not a variable or an expression.

EXAMPLE

   utility foo "Baz"
   (
   local a, b, c
   include "foo-ui.ms"
   rollout bar "Bar"
   (
   include "bar-rollout.ms"
   )
   include "foo-handlers.ms"
   )

The include <file> construct is effectively replaced in the source code at that point with the contents of the named file.

You can nest included files as deeply as needed (included files can include other files, and so on).

Because include is a compile-time construct, the current MAXScript scope is maintained within the included file. This is opposed to the fileIn() method described in Running Scripts, whose script file content is compiled in a global scope context. For more information, see Scope_of_Variables.

The include <file> can appear at any point a new token is expected such as a name, literal, or punctuation. This means that you can complete a partial expression with an included file.

FOR EXAMPLE

   include "op1.ms"+ include "op2.ms"
   if include "test2.ms" then print a else print b

You cannot place an include <file> within a token. For example, the following is not valid:

NOT VALID:

   timeval = 2:include "framenum.ms".0