The following is an example of a minimalist custom .NET command:
using System; using Autodesk.Maya.Runtime; using Autodesk.Maya.OpenMaya; [assembly: MPxCommandClass(typeof(MayaNetPlugin.helloWorldCmd), "helloWorldCmd")] [assembly: ExtensionPlugin(typeof(MayaNetPlugin.helloWorldPlugin), "Autodesk", "1.0", "Any")] namespace MayaNetPlugin { public class helloWorldPlugin : IExtensionPlugin { bool IExtensionPlugin.InitializePlugin() { return true; } bool IExtensionPlugin.UninitializePlugin() { return true; } String IExtensionPlugin.GetMayaDotNetSdkBuildVersion() { String version = ""; return version; } } public class helloWorldCmd : MPxCommand,IMPxCommand { public override void doIt(MArgList argl) { MGlobal.displayInfo("Hello World\n"); } } }
This simple example demonstrates how to create a helloWorld Maya command in C# that outputs text to the Maya Output window.
The two using Autodesk.Maya… directives add the namespaces of the .NET SDK. This way, you can use the types from these namespaces without qualification. Without using a directive, you must write the full name of the types in the file, such as Autodesk.Maya.OpenMaya.MPxCommand.
This plug-in consists of two logical parts: the IExtensionPlugin interface implementation and the command itself.
The ExtensionPlugin assembly attribute points to the IExtensionPlugin implementation.
When implementing IExtensionPlugin.InitializePlugin(), it returns true if the initialization can proceed. If the plug-in cannot load, then it returns false and Maya discontinues the loading process. The same applies to IExtensionPlugin.UninitializePlugin().
When implementing IExtensionPlugin.GetMayaDotNetSdkBuildVersion(), you can return the API version number you are using to build the plug-in; or return an empty string.
The MPxCommandClass assembly attribute describes your plug-in type to the .NET SDK. The MPxCommandClass attribute requires as parameter the type of the class implementing the user defined command and the name of the command. To provide the type of the class, use the typeof operator. Because the attribute is located outside of the MayaNetPlugin namespace you must specify the full path for your class to the typeof operator including its namespace.
namespace MayaNetPlugin: All types must be declared within a namespace. A C# namespace can contain classes, structs, interfaces, and enums. The namespace is useful for organizing code and preventing name collisions.
public class helloWorldCmd : MPxCommand, IMPxCommand: The helloWorldCmd class derives from the MPxCommand class and implements the IMPxCommand interface. You can have multiple interfaces, but you can only derive from one class. MPxCommand is the base class for user commands and every command is derived from it.
public override void doIt(MArgList argl): Use doIt() to execute the actions of your command.