Adding Commands to Menus

 
 
 

To make one or more custom commands available from a menu in Softimage, you first register a custom menu and then add the commands to that custom menu.

A custom menu is not a menu in the same sense as the standard File, Edit, and Help menus that you see in most applications. Rather, a custom menu is a set of commands that Softimage inserts into an existing menu.

Registering Custom Menus

This XSILoadPlugin function registers two custom menus that will be inserted in the Help menu on the main Softimage menubar:

function XSILoadPlugin( in_reg )
{
	in_reg.Author = "Me";
	in_reg.Name = "MyCommands";

	// Register commands
	in_reg.RegisterCommand("MyCommand1","MyCommand1");
	in_reg.RegisterCommand("MyCommand2","MyCommand2");

	// Register a custom menu
	in_reg.RegisterMenu(
				siMenuMainHelpID,// Anchor point for the Help menu on the main menubar
					"MyCommands_Menu",	// Internal name for the Menu object
					false,					// DisplayAsSubmenu
					false					// Dynamic (if true, Init is called everytime menu opens)
				);

	// Register another menu as a submenu
	in_reg.RegisterMenu( siMenuMainHelpID, "MyCommands_SubMenu", true, false );

	return true;
}
  • The first argument to RegisterMenu or RegisterMenu is the menu anchor point, which specifies where the custom menu is inserted. The anchor point siMenuMainHelpID is on the Help menu of the main menubar.

  • The second argument is name of the custom menu. You use this name to name the menu callback functions (for example, MyCommands_Menu_Init).

  • The third argument specifies whether to add commands directly to the Help menu or to put them on a submenu. For example, the following screen shot shows the two custom menus. Both menus have the same commands ("My Command 1 and My Command 2), but one menu was added with DisplayAsSubmenu= false, and the other with DisplayAsSubmenu=true.

  • The fourth argument specifies whether the menu is dynamic. Dynamic means that Softimage calls the menu Init callback every time the menu is displayed. This allows you to dynamically change the contents of the menu.

Adding Commands

To add commands to a menu, you write an Init callback for the custom menu:

// Init callback for the menu named "MyCommands_Menu"
function MyCommands_Menu_Init( ctxt )
{
	var oMenu;
	oMenu = ctxt.Source;

	// Add the commands to the menu
	oMenu.AddCommandItem(
			"My Command 1",// The text that appears on the menu
			"MyCommand1"// The command that is invoked by the menu item
			);

	oMenu.AddCommandItem( "My Command 2", "MyCommand2" );

	return true;
}

This Init callback adds the commands to a submenu.

// Init callback for the menu named "MyCommands_SubMenu"
function MyCommands_SubMenu_Init( ctxt )
{
	var oMenu;
	oMenu = ctxt.Source;

	// Set the text that appears on the Softimage menu for this submenu
	oMenu.name = "My Commands";

	// Add the commands
	oMenu.AddCommandItem("My Command 1","MyCommand1");
	oMenu.AddCommandItem("My Command 2","MyCommand2");

	return true;
}