context command是用来新建自己的工具,可以调用OPENGL,获取鼠标操作函数,在view窗口画自己想画的东西。(我是这麽理解的,可以以后再确定一下)
下面是一个context command的例子,通过例子来说明context command的代码怎么写,又怎么样来activate。
包含的头文件:
#include <maya/MIOStream.h> #include <math.h> #include <stdlib.h> #include <maya/MFnPlugin.h> #include <maya/MString.h> #include <maya/MGlobal.h> #include <maya/M3dView.h> #include <maya/MDagPath.h> #include <maya/MItSelectionList.h> #include <maya/MSelectionList.h> #include <maya/MPxContextCommand.h> #include <maya/MPxContext.h> #include <maya/MEvent.h> #include <maya/MUIDrawManager.h> #include <maya/MFrameContext.h> #include <maya/MPoint.h> #include <maya/MColor.h> #include <GL/gl.h> #include <GL/glu.h>
1. 定义继承于MPxContext的类:
const char helpString[] = "Click with left button or drag with middle button to select"; class marqueeContext : public MPxContext { public: marqueeContext(); virtual void toolOnSetup( MEvent & event ); // Default viewport or hardware viewport methods override, will not be triggered in viewport 2.0. virtual MStatus doPress( MEvent & event ); virtual MStatus doDrag( MEvent & event ); virtual MStatus doRelease( MEvent & event ); virtual MStatus doEnterRegion( MEvent & event ); // Viewport 2.0 methods, will only be triggered in viewport 2.0. virtual MStatus doPress ( MEvent & event, MHWRender::MUIDrawManager& drawMgr, const MHWRender::MFrameContext& context); virtual MStatus doRelease( MEvent & event, MHWRender::MUIDrawManager& drawMgr, const MHWRender::MFrameContext& context); virtual MStatus doDrag ( MEvent & event, MHWRender::MUIDrawManager& drawMgr, const MHWRender::MFrameContext& context); private: // Marquee draw method in default viewport or hardware viewport with immediate OpenGL call void drawMarqueeGL(); // Common operation to handle when pressed void doPressCommon( MEvent & event ); // Common operation to handle when released void doReleaseCommon( MEvent & event ); short start_x, start_y; short last_x, last_y; bool fsDrawn; MGlobal::ListAdjustment listAdjustment; M3dView view; };
其中doPress(), doRelease(), doDrag()函数,为函数重载,以方便不同版本的view进行绘制。
doPressCommon(), doReleaseCommon(), drawMarqueeGL()函数为上面三个函数不同版本之间的共同部分。以doDrag()为例子,具体参看document。
MStatus marqueeContext::doDrag( MEvent & event ) // // Drag out the marquee (using OpenGL) // { view.beginXorDrawing(); if (fsDrawn) { // Redraw the marquee at its old position to erase it. drawMarqueeGL(); } fsDrawn = true; // Get the marquee's new end position. event.getPosition( last_x, last_y ); // Draw the marquee at its new position. drawMarqueeGL(); view.endXorDrawing(); return MS::kSuccess; }
void marqueeContext::drawMarqueeGL()
{
glBegin( GL_LINE_LOOP );
glVertex2i( start_x, start_y );
glVertex2i( last_x, start_y );
glVertex2i( last_x, last_y );
glVertex2i( start_x, last_y );
glEnd();
}
2. 编好自己的MPxContext之后,创建MPxContextCommand的子类来创建context 命令,该命令用来创建context工具。
class marqueeContextCmd : public MPxContextCommand { public: marqueeContextCmd(); virtual MPxContext* makeObj(); static void* creator(); }; marqueeContextCmd::marqueeContextCmd() {} MPxContext* marqueeContextCmd::makeObj() { return new marqueeContext(); } void* marqueeContextCmd::creator() { return new marqueeContextCmd; }
3. 最后通过MFnPlugin函数注册context command:
MStatus initializePlugin( MObject obj ) { MStatus status; MFnPlugin plugin( obj, PLUGIN_COMPANY, "12.0", "Any"); status = plugin.registerContextCommand( "marqueeToolContext", marqueeContextCmd::creator ); return status; } MStatus uninitializePlugin( MObject obj ) { MStatus status; MFnPlugin plugin( obj ); status = plugin.deregisterContextCommand( "marqueeToolContext" ); return status; }
4. Adding a context command to the Maya shelf
There are two ways to "activate" or make your context the current context in Maya. The first is through the use of the setToolTo command. This command takes the name of a context (tool) and makes it the current context.
A second method is by making an icon to represent your context and putting it in the Maya tool shelf. The Maya tool shelf can store two kinds of buttons, command buttons and tool buttons. When the tool is activated, its icon is displayed next to the standard Maya tools in the toolbar.
The following is a set of MEL commands you can use to create a context and tool button for the context.
marqueeToolContext marqueeToolContext1;
setParent Shelf1; //注意这的shelf1应该用shelf子目录的名字代替,例如Custom
toolButton -cl toolCluster
-t marqueeToolContext1
-i1 "marqueeTool.xpm" marqueeTool1;
This MEL code instantiates an instance of the marqueeToolContext and adds it to the "Common" tools shelf.
marqueeTool.xpm, the icon for the tool, must be in the XBMLANGPATH to be found and added to the UI. If it is not found, a blank spot will appear on the shelf, but the tool will still be usable.
This code could either be sourced by hand from the MEL command window, or it could be invoked withMGlobal::sourceFile() in the initializePlugin() method of the plug-in.