我尝试使用MEL将菜单项添加到Maya中的现有菜单
例:
menuItem -label "Mylabel" -insertAfter "someEntry" -parent $gMainFileMenu;
menuItem -label "Mylabel2" -insertAfter "someEntry" -parent $gMainFileMenu;
Run Code Online (Sandbox Code Playgroud)
问题是菜单不会像普通条目那样填充,但只能通过我用这两行代码添加的条目来填充.
例如,文件菜单通常包含"新场景","打开场景","保存场景"等,但是当我执行这两行时,它只包含"Mylabel"和"Mylabel2".
是否有修复或解决方法以确保菜单完全填充?有没有办法迫使Maya在没有用户实际点击的情况下构建它?
实际上问题是Maya首次在用户打开菜单时填充菜单.它检查菜单长度是否大于0,它不会填充它.既然你已经添加了2项,菜单的长度大于0而不会被填充的标准条目.
要解决这个问题,有两种方法可以解决.您可以通过强制构建菜单项或注册构建menuItem来实现.Bot方式可用于不同的情况.
你需要做的是找到Maya调用的功能来构建你需要的菜单.您可以在文件夹中找到这些功能<MayaInstall>/scripts/startup/*.找到程序名称的一个好方法是打开玛雅控制台,使"回声所有命令",然后点击你想看到号称要建它的功能菜单.
在您的情况下,该函数被调用buildFileMenu(),可以在脚本中找到FileMenu.mel.
现在你有了这个函数名,你必须检查它的参数.有时它需要一个菜单名称作为参数.如果需要,请参阅底部有关如何查找菜单名称的信息.
现在让我们构建它.
global string $gMainFileMenu; // Retrieve the menu name
buildFileMenu(); // Build it
// Now build your menu
menuItem -divider true -parent $gMainFileMenu SuperMenuDivider;
menuItem -label "MyLabel1" -parent $gMainFileMenu SuperMenuLab1;
menuItem -label "MyLabel2" -parent $gMainFileMenu SuperMenuLab2;
// Create a proc to remove your menu items
global proc RemoveMyMenuItems()
{
if(`menuItem -ex SuperMenuDivider`) deleteUI -mi SuperMenuDivider;
if(`menuItem -ex SuperMenuLab1`) deleteUI -mi SuperMenuLab1;
if(`menuItem -ex SuperMenuLab2`) deleteUI -mi SuperMenuLab2;
}
// You can then call that function when in the unload function of your plugin.
Run Code Online (Sandbox Code Playgroud)
什么,你需要做的是使用一种称为函数addMenuItemSafe这需要3个参数,要填充的菜单,其中填充菜单中的函数的名称,和一个全局变量的名称来存放回调.
因此,首先要做的是创建将填充菜单的函数,然后是删除它的函数,然后调用该AddMenuItemSafe方法.请注意,在该功能中,您必须检查菜单是否已创建,因为每次显示菜单时,Maya都会调用该功能.
首先,添加和删除菜单条目的两个功能:
global proc string AddMyMenuItems()
{
// Global variable to hold the test to see if the menu is populated.
global int $gMyMenuItemsTest;
// Menu var needed in our case because we are inserting in the middle of the menu
global string $gMainFileMenu;
if( $gMyMenuItemsTest == 0 )
{
// Actually build your menu.
// Note that if you don't need to insert it after a specific entry,
// You can just do `menuItem -label "blep"`. No need of -ia and -p
// Also, for inserting in the middle you have to put stuff in reverse order.
// If you are just appending, put it in the normal order.
menuItem -label "Mylabel2" -insertAfter "someEntry" -parent $gMainFileMenu MyMenuLab2;
menuItem -label "Mylabel" -insertAfter "someEntry" -parent $gMainFileMenu MyMenuLab;
menuItem -divider true -parent $gMainFileMenu MyMenuDiv;
$gMyMenuItemsTest = 1;
}
return "RemoveMyMenuItems()"; // Returns the callback
}
global proc RemoveMyMenuItems()
{
global int $gMyMenuItemsTest;
if( $gMyMenuItemsTest == 1 )
{
// Delete your items if they exist (yes we are kind of
// doing the check twice, but I find it safe.
// The user could have deleted it from Maya in the command
// line for whatever reason, more robustness is always good.
if(`menu -ex MyMenuDiv`) deleteUI -mi MyMenuDiv;
if(`menu -ex MyMenuLab`) deleteUI -mi MyMenuLab;
if(`menu -ex MyMenuLab2`) deleteUI -mi MyMenuLab2;
}
}
Run Code Online (Sandbox Code Playgroud)
然后实际调用AddMenuItemSafe:
// The menu we want to use ... here it is the File Menu.
global string $gMainFileMenu;
// Our variables needed for the addSafe call
global int $gMyMenuItemsTest;
global string $gMyMenuVariable;
$gMyMenuItemsTest = 0;
$gMyMenuVariable = "";
// The menu creation
addMenuItemSafe($gMainFileMenu, "AddMyMenuItems", "gMyMenuVariable");
Run Code Online (Sandbox Code Playgroud)
您可以自由地将该函数调用放在插件实例化中或者它让您高兴的地方.
有关该函数的更多信息AddMenuItemSafe,您可以在Maya脚本目录中找到"AddMenuItemSafe.mel".
注意菜单变量名称,您可以使用以下约定"构建"它们
"g"+查看+姓名+"菜单"
地点:
查看是您可以找到该菜单的视图.例如:主要,多边形,动画等
名称是菜单的名称.例如:文件,编辑,网格等
注意Autodesk有时会重命名菜单并使用旧的globalVariable名称,因此使用此方法可能并不总是有效