// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== // // Get the value of a global variable using its name. // Also verify that the name respects the 'gVariableName' convention, // just print a warning message for now. // // $globVarName Name of the global variable, it must be a string. // /////////////////////////////////////////////////////////////////////////////// proc string getGlobalValue (string $globVarName) { if (!gmatch($globVarName, "g[0-9A-Z]*")) warning ((uiRes("m_addMenuItemSafe.kWrongGlobalName"))); // Get the variable value string $tmp = "global string $" + $globVarName + "; print $" + $globVarName; string $val = eval($tmp); return $val; } // // Safe way to add menu items from a plug-in. This method will test if // the menu is already built or not, and either create the item right // away, or add the command to the menu construction callback. // // $mainMenu is one of the top level menus. // $cmd is the mel script to be executed to build the item. It must // return the mel procedure to remove the added items. // $globVarName must be a global variable name, variable must be // initialized to "". This variable is used to keep track of the // built or registered menus. // // Example: // global string $gMyGlobVariable = ""; // global string $gModelingMenus[]; // string $menu = $gModelingMenus[4]; // Edit polygon menu // addMenuItemSafe($menu, "addMyItem", "gMyGlobVariable"); // // global proc string addMyItem() // { // menuItem...; // return "deleteUI ..."; // } // /////////////////////////////////////////////////////////////////////////////// global proc addMenuItemSafe(string $mainMenu, string $cmd, string $globVarName) { string $val = getGlobalValue ($globVarName); // Make sure menu was not already dealt with... if ($val != "") return; $tmp = ";global string $"+$globVarName+ "; if (($"+$globVarName+" == \"\") || ($"+ $globVarName+" == \"CB:\")) {$"+$globVarName+" = `"+$cmd+"`;}"; if (`menu -q -ni $mainMenu`) // Add a new entry to the menu eval ($tmp); else { // Otherwise, we have to add a call back to the menu. string $buildMethod = `menu -q -pmc $mainMenu`; menu -e -pmc ($buildMethod + $tmp) $mainMenu; string $tmp2 = "global string $"+$globVarName+ ";$"+$globVarName+" = \"CB:\""; eval ($tmp2); } return; } // // Remove menu items created with addMenuItemSafe. // $mainMenu and $globVarName arguments must be the same as for // addMenuItemSafe. // /////////////////////////////////////////////////////////////////////////////// global proc removeMenuItemSafe(string $mainMenu, string $globVarName) { string $val = getGlobalValue ($globVarName); if ($val != "") { // Remove the callback if it was added. string $buildMethod = `menu -q -pmc $mainMenu`; $buildMethod = `substitute (";global string \$"+$globVarName+".*`;}") $buildMethod ""`; menu -e -pmc $buildMethod $mainMenu; if ($val != "CB:") // The menu was built, remove it. eval $val; $tmp = "global string $"+$globVarName+ ";$"+$globVarName+" = \"\""; eval ($tmp); } }