PDA

View Full Version : MFnPlugin::addMenuItem


stuh505
09-03-2005, 05:38 PM
The Maya doc has this to say:


It is often necessary to make the functionality of a plug-in command available through the Maya menus. This can be accomplished using the MFnPlugin class. The two methods of interest are:

MStringArray addMenuItem( const MString& menuItemName, const MString& parentName, const MString& commandName, const MString& commandParams, bool needOptionBox = false, MString *optBoxFunction = NULL, MStatu *retStatus = NULL );MStatus removeMenuItem(MStringArray& menuItemNames); The addMenuItem() method can be called in the initializePlugin() method while removeMenuItem() would be invoked from uninitializePlugin().

The MFnPlugin header has this to say (nothing new):


MStringArray addMenuItem(

const MString& menuItemName,

const MString& parentName,

const MString& commandName,

const MString& commandParams,

bool needOptionBox = false,

MString *optBoxFunction = NULL,

MStatus *retStatus = NULL

);


I'm trying to test this out. Without the "plugin.AddMenuItem" line, the code can be loaded by Maya properly and MStatus remains kSuccess. First I register the creator function of the new "shelf" class, under the command name of "SHELFCOMMAND". Then I try to add this command to the file menu commands in Maya during plugin initialization. I assume that MenuItemNAme is simple the string that is displayed in the menu, so I use a string like "MENU NAME" to be easily spotted in the menu. For parentName I am not quite sure -- I assume that this is the name of the category that I want it to appear under (eg, File / Edit / Modify / Skin / Polygons / Edit Polygons / ...), I also assume that making up a new name will create a new category. I have tried listing existing category names as well as new names, neither solves the problem. Since this extremely simple test plugin takes no args, I enter a blank string as args. No matter what,
it seems to always set status to kFailure! If I don't update kStatus with the result of this operation (leaving it at kSuccess) then the plugin can still load but it doesn't create anything in the menu. Can't find any more documentation or examples anywhere...could someone show me how I'm using this wrong?


//

// Copyright (C) Slipstream

//

// File: pluginMain.cpp

//

// Author: Maya Plug-in Wizard 2.0

//

#include <stdio.h>

#include <maya/MString.h>

#include <maya/MArgList.h>

#include <maya/MFnPlugin.h>

#include <maya/MPxCommand.h>

#include <maya/MIOStream.h>

#include <maya/MStringArray.h>



class shelf : public MPxCommand

{

public:

MStatus doIt (const MArgList& args );

staticvoid* creator();

};

MStatus shelf::doIt(const MArgList &args)

{

cout<<"Hello"<<endl;

return MS::kSuccess;

}

void* shelf::creator()

{

returnnew shelf;

}

MStatus initializePlugin( MObject obj )

//

// Description:

// this method is called when the plug-in is loaded into Maya. It

// registers all of the services that this plug-in provides with

// Maya.

//

// Arguments:

// obj - a handle to the plug-in object (use MFnPlugin to access it)

//

{

MStatus status = MS::kSuccess;

MFnPlugin plugin( obj, "Slipstream", "7.0", "Any");

// Add plug-in feature registration here

//

plugin.registerCommand( "SHELFCOMMAND", shelf::creator );

cout<<"before menu item: "<<status<<endl;

plugin.addMenuItem( "MENU NAME", "NEW", "SHELFCOMMAND", "", false, NULL, &status);

cout<<"after menu item: "<<status<<endl;

return status;

}

MStatus uninitializePlugin( MObject obj )

//

// Description:

// this method is called when the plug-in is unloaded from Maya. It

// deregisters all of the services that it was providing.

//

// Arguments:

// obj - a handle to the plug-in object (use MFnPlugin to access it)

//

{

MStatus status = MS::kSuccess;

MFnPlugin plugin( obj );

// Add plug-in feature deregistration here

//

plugin.deregisterCommand("SHELFCOMMAND");

MStringArray myMenu;

myMenu.append( "MyMenuItemName");

status = plugin.removeMenuItem( myMenu );

return status;

}

edit: the problem seems to be that "parentName" must reference some kind of existing parent...but what kind of parent this is and how to get it is a mystery to me

Robert Bateman
09-06-2005, 12:08 PM
$gMainWindow perhaps ???

CGTalk Moderation
09-06-2005, 12:08 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.