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.

七、context command的更多相关文章

  1. 八、MPxToolCommand, tool command

    1. Tool Property Sheets: 是用来更改context属性的编辑框,类似于attribute editor.(property和attribute本质上是一个意思)作用于activ ...

  2. C#设计模式(15)——命令模式(Command Pattern)

    一.前言 之前一直在忙于工作上的事情,关于设计模式系列一直没更新,最近项目中发现,对于设计模式的了解是必不可少的,当然对于设计模式的应用那更是重要,可以说是否懂得应用设计模式在项目中是衡量一个程序员的 ...

  3. .NET设计模式(17):命令模式(Command Pattern)(转)

    概述 在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”.但在某些场合,比如要对行为进行“记录.撤销/重做.事务”等处理,这种无法抵御变化的紧耦合是不合适的.在这种情况下,如何将“行为 ...

  4. NET设计模式 第二部分 行为型模式(16):命令模式(Command Pattern)

    命令模式(Command Pattern) ——.NET设计模式系列之十七 TerryLee,2006年7月 概述 在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”.但在某些场合,比 ...

  5. 【16】命令模式(Command Pattern)

    一.前言 最近项目中发现,对于设计模式的了解是必不可少的,当然对于设计模式的应用那更是重要,可以说是否懂得应用设计模式在项目中是衡量一个程序员的技术水平,因为对于一个功能的实现,高级工程师和初级工程师 ...

  6. Docker Context基本原理

    Docker Context基本原理 介绍 本指南介绍了上下文如何使单个Docker CLI轻松管理多个Swarm集群.多个Kubernetes集群和多个单独的Docker节点. 单个Docker C ...

  7. 美图DPOS以太坊教程(Docker版)

    一.前言 最近,需要接触区块链项目的主链开发,在EOS.BTC.ethereum.超级账本这几种区块链技术当中,相互对比后,最终还是以go-ethereum为解决方案. 以ethereum为基准去找解 ...

  8. Linux操作系统的进程管理

    Linux操作系统的进程管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.进程相关概念 1>.进程概述 内核的功用: 进程管理.文件系统.网络功能.内存管理.驱动程序. ...

  9. ps示例

    博客PS示例 一显示指定真正用户名(RUID)或用户ID的进程 打开另外一个终端 [root@centos72 ~]# id wang uid=1000(wang) gid=1000(wang) gr ...

随机推荐

  1. html a 链接标签title属性换行鼠标悬停提示内容的换行效果

    鼠标经过悬停于对象时提示内容(title属性内容)换行排版方法,html title 换行方法总结. html的title属性默认是显示一行的.如何换行呢? 这里DIVCSS5总结介绍两种换行方法为大 ...

  2. find_first_of()和 find_last_of() 【获取路径、文件名】

    find_first_of()和 find_last_of() [获取路径.文件名](2011-06-11 12:44:46)转载▼标签: 杂谈 分类: c  string 类提供字符串处理函数,利用 ...

  3. C# base64图片数据传送加号(+)变空格问题

    今天遇到一个问题,将图片的base64数据转发,客户收到base64数据后生成图片失真. 通过日志监控,对比客户收到的数据和我发出的数据,最终发现客户base64数据中原本应该显示为加号(" ...

  4. (转)javascript匿名函数的写法、传参和递归

    (原)http://www.veryhuo.com/a/view/37529.html (转)javascript匿名函数的写法.传参和递归 http://www.veryhuo.com 2011-0 ...

  5. C++虚函数的实现机制示例

    C++虚函数的实现机制是通过一个vtable表,指向子类的虚函数地址. 另外,如果不是虚函数,则不能实现用父类引用调用子类方法. #include <windows.h> #include ...

  6. Caffe 源碼閱讀(一) Blob.hpp

    Blob 四維度(N K H W) N : SGD 一次 mini-batch 個數 K : 如果是圖片表示圖片通道數 如果是中間結果 則理解爲 feature map 個數 H.W : 如果是圖片理 ...

  7. Myeclipse+Axis2+Tomcat开发webService

    1.  下载文件: 需要在axis2官网下载两种类型的axis2文件,bin版和war版(下载地址:http://axis.apache.org/axis2/java/core/download.cg ...

  8. 解决 xx.h has been modified since the precompiled header 系统头文件被修改

    fatal error: file '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Dev ...

  9. OC 中 类目、延展和协议

    Category : 也叫分类,类目. *是 为没有源代码的类 扩充功能 *扩充的功能会成为原有类的一部分,可以通过原有类或者原有类的对象直接调用,并且可继承 *该方法只能扩充方法,不能扩充实例变量 ...

  10. mm/memory

    /* *  linux/mm/memory.c * *  Copyright (C) 1991, 1992  Linus Torvalds */ /* * demand-loading started ...