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. Linux摄像头驱动学习之:(四)UVC-摄像头驱动框架分析

    UVC: USB Video ClassUVC驱动:drivers\media\video\uvc\ uvc_driver.c分析:1. usb_register(&uvc_driver.dr ...

  2. win7下用python3.3抓取网上图片并下载到本地

    这篇文章是看了网上有人写了之后,才去试试看的,但是因为我用的是python3.3,与python2.x有些不同,所以就写了下来,以供参考. get_webJpg.py #coding=utf-8 im ...

  3. rails之 Migrations (转)

    1.简介 在rails中用migration可以很方便的管理数据库的结构.可以创建数据库,创建表,删除表,添加字段,删除字段,整理数据. migration就是一系列的class,这些类都继承了Act ...

  4. JS三元

    ((productDatas[i].Img.indexOf("http") == -1) ? ("/upload/190-160/" + productData ...

  5. C++ Primer----智能指针类 2

    指针带给了 C++巨大的灵活性,然而同样也带来无数的问题,悬挂指针,内存泄漏等. int *pInt = new int(1); // Do not forget delete pInt; 智能指针就 ...

  6. ffmepg-nginx-nginx-rtmp-module配置脚本

    把上个月写的的配置脚本贴一下: #!/bin/bash #version:-- #create by itn #dis: this is used to auto install ffmpeg+ngi ...

  7. RabbitMQ/JAVA 客户端连接测试

    这里是一个简单的helloworld测试. 这里在eclipse平台中实现 细节不再赘述.重点是导入rabbitmq-java-client的jar包 下载地址:http://www.rabbitmq ...

  8. c++嵌套类-内存分配

    首先看下列代码:int main(){    double *p;    printf("sizeof(int):%d\nsizeof(double):%d\nsizeof(ptr):%d\ ...

  9. 监听调试web service的好工具TCPMon

    监听调试web service的好工具TCPMonhttp://ws.apache.org/commons/tcpmon/download.cgi TCPMon Tutorial Content In ...

  10. CSS缩放函数, 旋转函数与倾斜函数

       1 :缩放        scale(x,y)函数让元素根据中心原点对对象进行缩放,大于1进行放大,小于1则缩小,如果为负值,则先进行翻转再进行缩放操作. 实例: HTML: <div c ...