Maya API编程快速入门

一.Maya API编程简介

Autodesk® Maya® is an open product. This means that anyone outside of Autodesk can change Maya’s existing features or add entirely new features. There are several ways you can modify Maya:

· MEL™—(Maya Embedded Language) is a powerful and easy to learn scripting language. Most common operations can be done using MEL.

· Python™—is a powerful and easy to learn scripting language, which provides an interface to the Maya commands.

· C++ API—(Application Programmer Interface) provides better performance than MEL or Python. You can add new objects to Maya using the API, and code executes approximately ten times faster than when you perform the same task using MEL. Also, you are able to execute MEL commands from the API.

· Maya Python API—Based on the API and allows the API to be used through the Python scripting language.

See MEL and Expressions in the Maya User's Guide for an introduction to MEL, and Python for its equivalent interface.

The Maya Developer Help provides a technical introduction to the Maya API and the Maya Python API.

Overview

The Maya API is a C++ API that provides internal access to Maya and is available on the following platforms: Microsoft® Windows®, Linux®, and Apple® Mac OS® X. You can use the API to implement two types of code resources: plug-ins which extend the functionality of Maya, or stand-alones such as console applications which can access and manipulate a Maya model.

Plug-ins can be built in two ways:

· As dynamic or relocatable libraries which are loaded into Maya using standard operating system functionality. Plug-ins work by accessing the symbol space of the host application Maya. Access to the symbol space of other loaded plug-ins is not available.

· As scripts that use the Maya Python API.

Many of the examples in the API Developer Kit are provided with both C++ and Python source codes. To allow both versions to be loaded into Maya at the same we have adopted the convention of prefixing the commands and nodes from Python plugins with "sp" (e.g. spHelix). You are not required to follow this convention.

When you use dynamic libraries, the operating system that you develop on place various restrictions on how to build and name plug-ins. The file extensions for plug-ins are:

· Linux: .so

· Windows: .mll

· Mac OS X: .bundle

· All platforms for Python plug-ins: .py

内容链接:http://help.autodesk.com/view/MAYAUL/2015/ENU/?guid=__files_API_Introduction_htm

总结:

Autodesk®Maya是一款开放式产品,可以通过下面四种方法来更改或扩展Maya的现有功能或添加全新功能:

1. MEL™ - (Maya嵌入式语言)是一种功能强大且易于学习的脚本语言。最常见的操作可以使用MEL进行。

2. Python- 它是一个强大且易于学习的脚本语言,为Maya命令提供了一个界面。

3. C ++ API(应用程序接口)-它能提供比MEL或Python更好的性能。您可以使用API​​向Maya添加新对象,并且代码比使用MEL执行相同任务时执行的速度快十倍。此外,您可以从API执行MEL命令。

4. Maya Python API - 基于API,并允许通过Python脚本语言使用API​​。

二.Visual Studio 下Maya API编程开发环境安装与使用

本文中Microsoft Visual Studio版本为Microsoft Visual Studio 2008,Maya版本为Maya 2009,安装路径为:f:\Program Files (x86)\Maya2009。其它版本可以参考本文操作。

Maya provides a wizard for Microsoft Visual Studio 2008 which can be used to quickly and easily create Visual Studio projects for Maya plug-ins. The wizard is not installed automatically by the Maya installer so you must copy some files by hand before you can start using it.

Follow these steps to install the wizard

1. The wizard can be found in devkit\pluginwizard\MayaPluginWizard2.0.zip under Maya's install folder. Unzip this file into a local folder.

2. Copy the following files to the C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcprojects folder:

MayaPluginWizard.vsdir

MayaPluginWizard.vsz

MayaPluginWizard.ico

3. Notice that the unzipped file contains a top-level folder named MayaPluginWizard and within that a sub-folder which is also named MayaPluginWizard. Copy the top-level MayaPluginWizard folder to C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\VCWizards

NOTE:

If you are working with a non-english installation of Microsoft Visual Studio, then you may have to alter the folders that the files are copied into above.

Follow these steps to use the wizard

1. Start Microsoft Visual Studio, invoke File -> New -> Project -> Visual C++ Projects and select MayaPluginWizard.

2. Enter a name and solution name and select the OK button.

3. Fill in the information for the Plug-in setup screen.

4. Click Next to get the Plug-in type dialog, or select it from the link in the sidebar, and fill in any required information.

5. Click Next to get the Included libraries dialog, or select it from the link in the sidebar, and fill in any required information.

6. Click Finish to create the project.

内容链接:

http://help.autodesk.com/view/MAYAUL/2015/ENU/?guid=__files_GUID_13D86D23_65DB_49A2_BD9C_DB9EF9F8644A_htm

三.命令式插件项目创建

1. 创建项目,生成插件

在向导工具安装成功后,打开VS2008的新建工程向导,我们可以看到以下的界面

clip_image002

我们选择MayaPluginWizard来新建一个项目,项目名为HelloWorld,确定后显示项目向导有关选项,如下图:

默认情况下,developer Kit location是指向C盘的,如果你的MAYA安装在其它地方,则需要指定相应的MAYA安装路径,它用来告诉VS在编译时所需的 Maya相关头文件与库文件在何处:

clip_image004

再点击左侧Plug-in type,如下图:

clip_image006

点Finish,就会创建一个最简单的MAYA插件,就是一个不带Undo/Redo功能的maya命令插件项目。

代码很简单,整个工程只有一个CPP文件,代码如下:

#include <maya/MSimple.h>

// Use helper macro to register a command with Maya. It creates and

// registers a command that does not support undo or redo. The

// created class derives off of MPxCommand.

//

//DeclareSimpleCommand( MayaPluginWizard1, "", "2010");

DeclareSimpleCommand( MayaPluginWizard1, "", "2009");//将2010改为2009

MStatus MayaPluginWizard1::doIt( const MArgList& args )

//// Description:

// implements the MEL MayaPluginWizard1 command.

//// Arguments:

// args - the argument list that was passes to the command from MEL

//// Return Value:

// MS::kSuccess - command succeeded

// MS::kFailure - command failed (returning this value will cause the

// MEL script that is being run to terminate unless the

// error is caught using a "catch" statement.

//

{

MStatus stat = MS::kSuccess;

// Since this class is derived off of MPxCommand, you can use the

// inherited methods to return values and set error messages

//

displayInfo("Hello World!");//显示Hello World,自己添加的代码

setResult( "MayaPluginWizard1 command executed!\n" );

return stat;

}

我们在doIt()函数中加入一行:displayInfo("Hello World!");

然后进行编译,如果一切顺利,在我们工程的Debug文件夹中就生成了一个叫HelloWorld.mll文件,这就是一个所生成的MAYA插件。

2. 加载插件并测试

把HelloWorld.mll文件拷贝到f:\Program Files (x86)\Maya2009\bin\plug-ins目录下,然后重新打开maya2009,从菜单window->settings/preferences->Plug-In Manager打开插件加载窗口:

clip_image008

把我们的HelloWorld.mll插件加载进来,然后在我们的maya命令行窗口中输入HelloWorld命令对插件进行测试,Hello World!就会显示在脚本编辑器中,如下图所示。

clip_image010

项目源代码链接:http://pan.baidu.com/s/1pLujp2r 密码:qplx

四.节点式插件项目创建

MAYA的插件大体上分为两大类型,命令(Command)和结点(Node),多数情况下,命令都是为结点服务的,那么什么maya的结点呢?我们可以把结点想像为一个数据流处理器,每个结点,它都有输入接口,输出接口,及对数据进行处理的内核,如下图:

clip_image011
我们说MYAY是基于结点的插件式软件架构,所以在MAYA底层,对所有的数据都是通过把大量这些的结点连接起来,一层层地进行运算和处理才得到最终的结果。这种基于结点的软件架构,其最大的好处就是制作人员可以根据需求把各种节点随意地连接起来,从而实现让制作人员可以最大限度在发挥自已的想像空间和创意能力。

下面我们来实现一个功能简单的结点,该结点只有一个输入接口和一个输出接口(注:一个结点可以有多个输入接口和输出接口),要实现的功能是把输入的数据乘以0.5变成原来的一半,然后输出。

1. 创建项目,生成插件

打开MayaPluginWizard,新建一个Dependency Graph Node插件,如下图:

clip_image013

点击Finish后,项目生成三个文件,分别是:halfScaleNodeNode.h, halfScaleNodeNode.cpp, pluginMain.cpp,如下图:

clip_image015

2. 代码说明

(1)pluginMain.cpp文件,这是每个MAYA插件的入口,MAYA在加载该结点的时候,会自动运行initializePlugin( MObject obj )函数,因此我们就可以在这里做一些初始化的操作,其中最重要的就是要注册这个maya结点。

status = plugin.registerNode( "halfScaleNode", halfScaleNode::id, halfScaleNode::creator,  halfScaleNode::initialize );

if (!status) {

     status.perror("registerNode");

     return status;

}

所有自定义的maya结点及命令,都必须在初始化的时候注册,才能被MAYA识别和使用。注册的时候我们要注意的是,新的结点名和结点ID不能与已有的结点冲突,也就是说结点名和结点ID在整个MAYA系统中都必须是唯一的。所以在halfScaleNodeNode.cpp文件的开始有这样一个定义结点ID的代码

// You MUST change this to a unique value!!!  The id is a 32bit value used

// to identify this type of node in the binary file format. 

#error change the following to a unique value and then erase this line

MTypeId     halfScaleNode::id( 0x00001 );

这里就提示我们必须要给该结点分配一个唯一的ID,要不就没法通过编译。我们可以把以上代码改为

//#error change the following to a unique value and then erase this line

MTypeId     halfScaleNode::id( 0x02010 );

这样我们就可以正常地编译代码了。

(2) 在halfScaleNodeNode.h, halfScaleNodeNode.cpp文件中,有个MStatus halfScaleNode::compute( const MPlug& plug, MDataBlock& data )函数,这是每个结点的核心运算函数。

前面我们说过,一个结点是由输入接口、输出接口及运算核心组成,这里的运算核心就是compute()函数,而输入输出接口则被封装在MDataBlock& data这个对像里面,我们通过相应的函数,就可以取得输入口和输出口所对应的地址,然后对这些数据进行操作:

MDataHandle inputData = data.inputValue( input, &returnStatus );

MDataHandle outputHandle = data.outputValue( halfScaleNode::output );

float result = inputData.asFloat();

这里,result所得到的就是输入数据的原始值,如果我们不作任何运算,直接把它赋值给输出接口

outputHandle.set( result );

那么得到的输出结果,也不会有任何改变。现在我们把输入数据乘以0.5然后再赋给输出接口:

float result = inputData.asFloat();

result = result * 0.5;

outputHandle.set( result );

很容易地,我们就达到了我们所想要实现的功能。

3. 加载插件并测试

把工程编译一下,得到一个叫halfScaleNode.mll的MAYA插件,和前面所说的安装方式一样,我们把halfScaleNode.mll拷贝到plug-in文件夹中,然后在MAYA插件管理器中加载该插件。

我们来验检一下该结点插件是否能正确运行。我们要在maya场景中建两个小球,在命令窗口中输入并运行以下mel代码:

polySphere;

polySphere;

createNode halfScaleNode;

connectAttr pSphere1.translateX halfScaleNode1.input;

connectAttr halfScaleNode1.output pSphere2.translateX;

clip_image017

clip_image019

从超图上我们可以后清晰地看到,pSphere1的translateX属性被连接到halfScaleNode1的input输入口,经过运算后,输出给pSphere2的translateX属性。现在我们选择pSphere1然后沿X轴平称,我们可以看到,pSphere2会跟随pSphere1一起移动,但总是慢半拍,这正是我们想要的效果。

项目源代码和Maya工程文件链接:http://pan.baidu.com/s/1eRJnPy6 密码:4iec

五.项目调试

调试步骤主要可分为以下五步:

1. 在Visual Studio中以debug模式生成目标mll,设为HelloWorld.mll,文件路径为:g:\users\Projects\HelloWorld\HelloWorld\Debug\HelloWorld.mll;

2. 启动Maya,并在Maya中加载该路径下的此mll,即加载g:\users\Projects\HelloWorld\HelloWorld\Debug\HelloWorld.mll;

3. ctrl+alt+p打开附加到进程,然后将VS进程附加到Maya进程中;

4. 在VS中相关代码处建立断点;

5. 执行mll中的命令或节点,当遇到断点时会自动停下切换到VS中,即可调试运行。

参考文献:

1. Maya API Help: http://help.autodesk.com/view/MAYAUL/2015/ENU/?guid=__files_Maya_API_introduction_htm

2. MAYA API插件编程--入门篇:http://blog.csdn.net/huawenguang/article/details/6557862

3. Maya Plug-in 调试及技巧(1):http://blog.csdn.net/cuckon/article/details/6157403

posted on 2017-07-25 16:49  慢步前行  阅读(3832)  评论(0编辑  收藏  举报

导航