The <rcmenu_body> of a right-click menu definition is made up of a sequence of scripted right-click menu clauses as follows:
Scripted right-click menu clauses define the components of a scripted right-click menu and can be one of three basic things:
Local variables, functions or structure definitions are variables, functions, and structures whose scope is the scripted right-click menu. These locals will exist as long as the scripted right-click menu value exists. The scripted right-click menu value will exist until the scripted right-click menu value is redefined or deleted. Local variables are particularly useful for storing working data associated with the right-click menu such as picked objects.
User-interface items are displayed in the right-click menu, such as menu items, separators, and submenus.
Event handlers are special kinds of functions that are associated with particular user-interface items. Event handlers specify the processing you want to occur when a user interacts with an user-interface item, for example pressing a menu item or opening a submenu. These user actions generate named events and the optional event handler you supply for that event is called when the user action occurs.
The visibility of locals, and accessing scripted right-click menu locals from external code, are the same as described for utility rollouts, and described in Visibility of Locals, Functions, Structures, and User-Interface Items in Rollout Code and Accessing Locals and Other Items in a Rollout from External Code. In both utility rollouts and scripted right-click menus, the name given to a user-interface item is used to name an automatically constructed local variable that will hold the value representing the item. It is also used to associate event handler functions with the item. One difference between the scooping of these variables is that they are local to the rollout in utility rollouts, and local to the scripted right-click menu in scripted right-click menus. This means that all user-interface items in a scripted right-click menu must have a unique name within the scripted right-click menu, even for those items present in a subMenu .
Formally, the syntax of a <rcmenu_clause> is defined as follows:
<rcmenu_clause> ::= <local_variable_decl> | <local_function_decl> | <local_struct_decl> | <user_interface_item> | <event_handler>
A <local_variable_decl> , <local_function_decl> , and <local_struct_decl> are exactly the same as local variable, function, and structure definitions in MAXScript:
<local_variable_decl> ::= local <decl> { , <decl> } <decl> ::= <name> [ = <expr> ] -- optional initial value
<local_struct_decl> ::= struct <name> ( <member> { , <member> })<member> ::= ( <name> [ = <expr> ] | <local_function_decl> )
Global variables cannot be declared as a scripted right-click menu clause, however they can be declared within event-handler scripts. If you need to ensure that a variable name references a global variable, declare the variable name as global immediately before defining the right-click menu in your script.
When writing scripts, it is good programming practice to explicitly declare your local and global variables. Implicit declaration is provided as a short-hand, typically used when working in the Listener interactively or developing short scripts. When developing extended scripts, explicitly declaring variables can reduce errors and improve readability of the code. It is also recommend that you declare as local all variables unless you really want them to be global variables. The reasons for this are described in Scope of Variables.
A <user_interface_item> defines an individual menu item, separator, or submenu user-interface item that will appear in the right-click menu. These user-interface items are described in RCMenu User-Interface Items.
An <event_handler> is a special function definition local to a scripted right-click menu that you provide to handle the processing you want to occur when a user clicks on a menu item. This user action generates a named event and any event handler you supply for that event is called when the action occurs. The syntax for defining a handler is as follows:
The <item_name> specifies the name of the item for which this handler is connected. The <event_name> specifies the type of event to be handled.
There is only one type of user-interface item event:
The picked handler expression is executed when a menu item is picked from the right-click menu. Any MAXScript action like creating a object, adding a modifier, and so on can be performed in this expression.
In addition to the event handlers you specify for particular menu items in a right-click menu, you can define a handler function that is called when the entire right-click menu is first opened by the user. This event handler is useful for initialization code. The syntax for this event handler is as follows: