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. Ubuntu user switch

    To list all users you can use: cut -d: -f1 /etc/passwd To add a new user you can use: sudo adduser n ...

  2. Python开发入门与实战6-表单

    6. 表单 从简朴的单个搜索框,到常见的Blog评论提交表单,再到复杂的自定义数据输入接口,HTML表单一直是交互性网站的重要交互手段.本章介绍如何用Django如何对用户通过表单提交的数据进行访问. ...

  3. HTTP通信原理

    HTTP(HyperText Transfer Protocol)是一套计算机通过网络进行通信的规则.计算机专家设计出HTTP,使HTTP客户(如Web浏览器)能够从HTTP服务器(Web服务器)请求 ...

  4. A - Humble Numbers

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  5. samba 配置

    sudo apt-get install samba sudo apt-get install kdenetwork-filesharing vi /etc/samba/smb.conf [Share ...

  6. TextView 跑马灯

    首先,写一个类,让其继承自TextView: 重写focus方法,让TextView始终是focus. public class MarqueeText extends TextView { publ ...

  7. PHP Date Function Base

    /**************格式中可使用字母的含义**************/a – "am" 或是 "pm"  A – "AM" 或是 ...

  8. oracle不能删除,查看引用的外键

    例如我在删除scorm_course_info表中的某条数据时,会报已经找到子目录的错误,说明有另外的表B的某列b1外键引用了它,找到表B的b1列,可以通过如下的sql: select b.table ...

  9. JavaWeb用Jdbc操作MySql数据库(二)

    一.仍然使用前面的环境和示例数据库. 二.建立发出注册请求的页面index3.jsp. <%@ page language="java" import="java. ...

  10. python 类中staticmethod,classmethod,普通方法

    1.staticmethod:静态方法和全局函数类似,但是通过类和对象调用. 2.classmethod:类方法和类相关的方法,第一个参数是class对象(不是实例对象).在python中class也 ...