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 scoping 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
Examples of the above (in order) are:
local numSelected = 0
fn onlyOneSelected = selection.count == 1
struct parents (mother="",father="")
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.
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:
Scripted Right-Click Menus