In the .NET SDK of Maya, .NET assembly attributes are used to auto-register plug-ins, commands, nodes and so forth. This is different from the C++ SDK of Maya, where the MFnPlugin class provides registration methods for all MPx-prefixed classes of Maya (MPxCommand, MPxNode, and so forth). In .NET, classes are instead decorated with attributes which are parsed by the plug-in loader, which registers them with Maya on your behalf.
Refer to the MFnPlugin class reference documentation for more information.
Specify the ExtensionPlugin assembly attribute in your plug-in.
In general, add this assembly attribute at the top of the class that defines your plug-in:
[assembly: ExtensionPlugin(typeof(PluginClass), "Name1", "Name2",…)]
This attribute is not mandatory; but, if specified, speeds up the loading of your plug-in. Otherwise, Maya searches for the first C# class that implements the IExtensionPlugin interface. This interface contains the following methods:
bool InitializePlugin () ; // called when the plug-in is loaded
bool UninitializePlugin () ; // called when the plug-in is unloaded
System::String^ GetMayaDotNetSdkBuildVersion(); //Returns a string representation of the integer version of Maya (for example, “20135002”)
[assembly: MPxCommandClass(typeof(MPxCommandDerivedClass), "CommandName")]
You need to specify the full path, including the class namespace.
[assembly: MPxCommandClass(typeof(MayaNetTest.WhatIsCmd), "netWhatIs")] namespace MayaNetTest { public class WhatIsCmd : MPxCommand, IMPxCommand { override public void doIt(MArgList args) { … } } }
You may also need some class attributes (located above the definition of your command class) to describe the syntax of your command.
The following are the various attributes you can specify.
If your command takes arguments, you need to add the following attribute for each argument:
[MPxCommandSyntaxArg(typeof(ArgType))]
MPxCommandSyntaxArg: This attribute ensures that this argument is added to the syntax.
[MPxCommandSyntaxArg(typeof(System.String))] // a string argument [MPxCommandSyntaxArg(typeof(System.Int32))] // an integer argument
In the case where the argument is an object, you should use this class attribute with more controls:
[MPxCommandSyntaxSelection( UseSelectionAsDefault = UseSelection, MinObjectCount = min, MaxObjectCount = max, ObjectsType = typeof(ObjType))]
[MPxCommandSyntaxSelection(UseSelectionAsDefault = true, MinObjectCount = 1, MaxObjectCount = 2, ObjectsType = typeof(System.String))]
This command accepts 1 or 2 objects and uses the selection if no object is specified.
If your command takes one or more flags, then you must add the following class attribute for each flag.
[MPxCommandSyntaxFlag( “flag’s short name“, “flag’s long name”, Arg1 = typeof(ArgType), Arg2 = typeof(ArgType), ... ]
[MPxCommandSyntaxFlag("-na", "-name", Arg1=typeof(System.String))] [MPxCommandSyntaxFlag("-ip", "-inPosition", Arg1 = typeof(System.Double), Arg2 = typeof(System.Double), Arg3 = typeof(System.Double))]
Maya commands can be used in three different modes: create, query and edit.
The creation mode is always supported and is the default mode. In addition to the creation mode, commands can support the query or/and the edit mode.
You can specify the different modes using the following class attribute:
[MPxCommandSyntaxMode(CommandMode)]
[MPxCommandSyntaxMode(MPxCommandSyntaxModeAttribute.CommandMode.kQuery)]
This command supports the creation (default) and the query mode (-q).
Use the syntax() method in a command to retrieve the arguments and flags using the MArgDatabase class.
Consider a command with the following syntax attributes:
[MPxCommandSyntaxFlag("-i", "-index")] [MPxCommandSyntaxSelection(MinObjectCount = 1, MaxObjectCount = 1, ObjectsType = typeof(MSelectionList))] [MPxCommandSyntaxModeAttribute(MPxCommandSyntaxModeAttribute.CommandMode.kNone)]
You could retrieve the arguments as follows:
bool isIndex; MxPlug fPlug; const string kIndexFlag = "-i" ; const string kIndexFlagLong = "-index" ; // the following transfers all the class attributes information var argData = new MArgDatabase( syntax(), args); if ( argData.isFlagSet (kIndexFlag) ) isIndex =true ; // Get the plug specified on the command line. var slist = argData.getObjects() as MSelectionList;