Maya API编程快速入门

一.Maya API编程简介 node

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. app

· 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. less

· 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. ide

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

相关文章
相关标签/搜索