MacroScripts are scripts that are associated with Customize User Interface ActionItems (toolbar buttons, Menu or QuadMenu items, keyboard shortcuts) and are executed when the corresponding ActionItem is invoked.
MacroScripts have to be defined with the macroScript definition construct and can then be associated with a toolbar button by right-clicking a Shortcut toolbar or Tab and choosing Customize. The Customize User Interface dialog is displayed, which lets you choose from either Command shortcuts or MacroScripts. MacroScripts are essentially pieces of MAXScript code that have a name and category, and optionally a tooltip and icon.
MacroScripts do not automatically create user interface items. If you want a MacroScript to display a dialog, you will need to create a rollout floater window and rollout(s) as described in Rollout Floater Windows, and create and install the user-interface items in the rollout(s) as described in Rollout User-Interface Controls.
A MacroScript is defined using the following syntax:
macroScript <name> [category:<string>] [buttonText:<string>] [toolTip:<string>] [icon:#(<string>, <index>)] [silentErrors:<boolean>] [autoUndoEnabled:boolean>] (<macro_script_body>)
Since 3ds Max 2011, the strings defining the name properties of the MacroScript definition can be replaced with localized Resources strings whose definitions are enclosed in tildes. Please see the Resources String Replacement topic for details.
After MAXScript evaluates a macroScript construct, the MacroScript definition will show up in the appropriate category list in the Customize User-Interface dialog. The following figure shows a Customize User Interface dialog containing the previous two MacroScripts.
If a tooltip is specified, that tooltip is the name shown in the Customize User Interface dialog. If a tooltip is not specified, <name> is the name shown. Unlike other similar constructs (Scripted Utilities, Functions, and right-click menus), macroScript does not create a variable with this name. Rather, MacroScripts are stored as pointers into files, as described below.
The category: argument specifies in which category in the Customize User-Interface dialog the MacroScript name will be listed. The use of categories is intended to help you organize your MacroScripts into groupings so that the MacroScript names are less likely to clash. If you do not specify a category, a default category of "unknown" is used.
The internalCategory argument is intended to identify operations in .cui, .mnu, and .kbd files.
The toolTip: argument specifies the tooltip for the button. If no tooltip is specified, <name> is displayed for the button.
The buttonText: argument specifies the text that will appear in the button, and the icon: argument specifies the icon that will appear in the button. You can choose in the Customize User Interface dialog whether the buttonText or icon appears in the button. If no buttonText: argument is specified, the MacroScript name is used as the buttonText .
The icon: argument specifies the icon bitmap file and the icon image within the icon bitmap file. The icon bitmap file must be located in the current 3ds Max user-interface directory. Icon bitmap files have a base name, such as "MyToolbar", followed by a suffix, such as "_24i.bmp", that specifies the individual icon size and icon bitmap file type. The icon: argument string is just the base name, with no extensions present. This base name is the name shown in the Image Group list in the Customize User-Interface dialog.Each icon bitmap file can have any number of individual icons, lined up side-by-side in the file. If the icon bitmap file contains multiple icons, <index> specifies which icon in the icon bitmap file to use. The left-most icon has an <index> of 1. The 3ds Max internal icons (Image Group Internal in the Customize User-Interface dialog) are not stored in an icon file, and are referenced by using an empty string as the icon: argument.
So, the icon: argument can be either a two-element array containing the icon bitmap file’s base name as a string and the icon’s index in that file, or just a base name string, with the index 1 assumed.
See Creating Icon Bitmap Files for more information.
The silentErrors: parameter gives control over whether MAXScript runtime error messages are displayed while executing the MacroScript. If this parameter is set to true , error messages will not be displayed. This may be useful for distributed MacroScripts that may confuse the user with MAXScript error messages. The default value is false .
The autoUndoEnabled: parameters controls whether the MacroScript body will be automatically wrapped in a theHold begin/accept calls internally.
When set to false , the execution of the MacroScript body will not generate an undo record for the whole MacroScript.
When set to true or not specified, the whole MacroScript body will create an undo record when executed.
Available in 3ds Max 2010 and higher.
The <macro_script_body> can be one of two forms.
The body can be either a single MAXScript expression, or a set of event handlers.
An <event_handler> is a special function definition local to the macroscript that handles events generated by 3ds Max.
If <expr> returns true and the macroscript item is in a menu or quad menu, a check mark is placed next to the macroscript item. If the macroscript is a toolbar button, the button will appear as "pressed in". The do is optional for this event handler. If this event handler is not specified, the item will not be checked.
If <expr> returns false and the macroscript item is in a menu or quad menu, the macroscript item will not appear in the menu or quad menu. If the macroscript is a toolbar button, the button will be disabled. The do is optional for this event handler. If this event handler is not specified, the item will be enabled.
If <expr> returns false and the macroscript item is in a menu or quad menu, the macroscript item will not appear in the menu or quad menu. If the macroscript is a toolbar button, this handler has no affect. The do is optional for this event handler. If this event handler is not specified, the item will be visible.
The expression evaluated when the menu or quad menu item is chosen, or the toolbar button clicked. A runtime error will be generated if this event handler is not specified.
If a macroScript implements the altExecute event handler, then a mouse icon (with dark click button) appears in the quad menu item that corresponds to the macroScript. When the icon is clicked, the event handler is executed with <type> being #default .
For example, the following adds alternative execute functionality to the standard "Wire Parameters" quad menu. When you click the icon, it pops the Param Wiring Editor instead of putting into the Wiring mode.
The closeDialogs is handler is called instead of the on Execute handler whenever the isChecked handler returns true (the button / icon /item is checked). It can be used to TOGGLE back to the unchecked state. This handler should implement any cleanup code that closes any open dialogs and basically returns the macroScript to a pre-executed state.
Here is an test of a MacroScript which toggles its rollout on and off:
These optional MacroScript event handlers can be used to define so-called DropScripts that can be used in conjunction with the i-Drop technology. See DropScript Events for details.
The <macro_script_body> can contain global and local variables, functions, and structure definitions. The <macro_script_body> is compiled in its own local scope, and the locals are only visible inside the scope of the MacroScript. MacroScript locals are heap-based locals, which are slightly different from normal (stack-based) locals.
Normal locals are visible in the current scope and have a lifetime of one execution of that scope. Heap-based locals are also visible only in the current scope, but have a lifetime equal to the lifetime of the top-level expression where they are defined. A MacroScript’s locals are created the first time you execute the MacroScript, initialized to a value of undefined , or to their specified initialization value. These values are stored in a separate memory stack. On entry to each function (or top level script) in the MacroScript, a ’r;frame’ in the memory stack is marked and when the function (or top level script) exits, all of the values in the frame are freed from the memory.
Because a MacroScript’s name is not created as a variable, you cannot access a MacroScript’s locals outside the scope of the MacroScript. So, for example, you can create a rollout in a MacroScript, and the rollout’s event handlers can access the locals defined in the MacroScript because the rollout is executing within the scope of the MacroScript. However, you cannot access the MacroScript’s locals from another utility or from the Listener, because they are not executing within the scope of the MacroScript. See Scope of Variables for more information.
When you execute a macroScript definition, the return value is an integer MacroScript ID value. MAXScript internally stores information about each MacroScript in an array, and the returned MacroScript ID value is the array index for that MacroScript. The information stored for each MacroScript consists of the file in which that MacroScript is defined and a pointer into that file specifying where the MacroScript definition begins. The MacroScript definition is only compiled when you first press a toolbar button that contains the script, or execute the MacroScript using the macros.run() method.
The default user-interface directory and its subdirectories will be automatically scanned at startup for files of type .mcr, which will be parsed for MacroScript definitions.
A MacroScript definition is executed in an Editor window, in a script file that you execute, or in a startup script file.
If you evaluate a MacroScript definition in the Listener window, a file will automatically be created to contain the MacroScript definition. This file is stored in the MacroScripts directory under the current user-interface directory.
MAXScript supports text drag onto toolbars to create MacroScript buttons. You can select and drag text from any text window, such as Listener window panes or Editor windows, onto any visible toolbar. The cursor changes to an arrow with + sign when it is OK to drop the text. If you drop it, a MacroScript button is added to the toolbar with the dropped text as the body of the MacroScript. The classic case here would be to drag some text from the Macro Recorder pane in the Listener window onto a toolbar to make a button that does the sequence of events just recorded. Remember that tabbed elements on the Shelf are toolbars and can accept dropped text. MAXScript will automatically enclose the text in a MacroScript definition with a MacroScript name of DragAndDrop-MacroN , where N is a number that will make the MacroScript name unique. The category will be DragAndDrop and no tooltip is specified. As with MacroScripts defined in the Listener window, a file will automatically be created to contain the MacroScript definition. This file is stored in the MacroScripts directory under the current user-interface directory.
A MacroScript can be defined using the macros.new() method described next. As with MacroScripts defined in the Listener window, a file will automatically be created to contain the MacroScript definition. This file is stored in the MacroScripts directory under the current user-interface directory.
If you move or delete a file that contains a MacroScript definition that has been loaded, and try to execute the MacroScript, you will get an error message. Further, if you edit a file containing MacroScript definitions, take care to save and re-evaluate the entire file so any other MacroScripts defined in that file will have their file pointer updated. If you don't do this, you may get an error message saying the currently loaded definition no longer matches its file.
If you reevaluate a MacroScript definition, any button using that MacroScript will see any changes you make.
Any macroScript definition evaluated in MAXScript or created by dropping text onto a toolbar has a separate definition .mcr file created in the MacroScripts directory under the current user-interface directory (typically UI\MacroScripts). The name of the file is <category_name>-<macro_name>.mcr,
If you evaluate a macroScript definition in the Listener or drop text on a toolbar, its recorded definition file is this backing file in UI\MacroScripts. This definition file is the one that gets opened if you hit Edit MacroScript in the CUI customize menus or dialogs. For macroScript definitions evaluated in Listener, this means the same definition will be updated each time you evaluate it, rather than having separate backing file for each evaluation.
If you evaluate macroScript definitions in a .ms or .mcr file that does not already reside in the UI\MacroScripts directory, a copy for each will be placed in a separate file in UI\MacroScripts, but the recorded definition file will be the original source file, so that hitting Edit MacroScript will go to that file.
This means that if any buttons containing such macroScript definitions are added to toolbars in the startup.cui file, the backing .mcr file in UI\MacroScripts will be used as its definition at the next 3ds Max startup. When you start 3ds Max, the macroScript definitions will taken from the backing files in UI\MacroScripts. If these MacroScripts are also defined in MAXScript startup script files, they will be re-defined at MAXScript startup and so the definition file of record will be updated to point to the script file.
MAXScript provides several methods that allow you to access and run MacroScripts from within a script. These MacroScript functions are in a structure package named macros . For a description of the available methods, see the Macro Scripts topic.
The following MacroScript allows you to render directly to the RAMPlayer. This MacroScript shows the use of rollouts and rollout floater windows in MacroScripts.