As a first excercise in creating a command plug-in, create a command plug-in that prints "Hello World" in the Maya output window.
Because the plug-in in this example does not query the scene but only writes text to the Script Editor, it does not need to be undoable. Because of this you will be able to use DeclareSimpleCommand(). If the command altered the scene in any way, it would need to be undoable, and it would need to implement initializePlugin() and uninitializePlugin(). A version of this plug-in that implements these methods is also available here.
Before beginning, ensure that you have set up your environment according to the instructions in Setting up your build environment.
Create a directory called helloWorld and copy the following to a file named helloWorld.cpp in that directory:
#include <maya/MSimple.h> #include <maya/MIOStream.h> DeclareSimpleCommand( helloWorld, "Autodesk", "2021" ); MStatus helloWorld::doIt( const MArgList& ) { cout << "Hello World\n" << endl; return MS::KSuccess; }
Create a CMakeLists.txt file for this project and save it to the helloWorld directory:
cmake_minimum_required(VERSION 2.8) set(PROJECT_NAME helloWorld) project(${PROJECT_NAME}) include($ENV{DEVKIT_LOCATION}/cmake/pluginEntry.cmake) set(SOURCE_FILES helloWorld.cpp ) set(LIBRARIES OpenMaya Foundation ) build_plugin()
Use CMake and the appropriate generator to build a project for your code:
Once you have built your project successfully, you can either open the project in Visual Studio or Xcode and build your plug-in from there, or use CMake again to build your plug-in:
cmake --build build
You can now load your plug-in into Maya using the Plug-in Manager, which is accessed from Window > Settings/Preferences > Plug-in Manager from the Maya menu.
Once loaded, run helloWorld from the Maya command window:
The output will be written to the Maya output window:
You can also create a version which allows you to customize the greeting using arguments passed to the plug-in.
#include <maya/MSimple.h> #include <maya/MIOStream.h> DeclareSimpleCommand( helloWorld, "Autodesk", "2020"); MStatus helloWorld::doIt( const MArgList& args ) { cout << args.asString( 0 ).asChar() << " " << args.asString( 1 ).asChar() << endl; return MS::kSuccess; }
Before recompiling the plug-in, unload the plug-in from Maya using the unloadPlugin command.
You do not need to remake the plug-in project. You only need to recompile the plug-in using cmake --build build.
Reload the plug-in, and run it with two arguments:
Your greeting will be printed to the Maya output window: