一.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.
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:
· 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
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.
內容鏈接:
三.命令式插件項目創建
1. 創建項目,生成插件
在向導工具安裝成功后,打開VS2008的新建工程向導,我們可以看到以下的界面
我們選擇MayaPluginWizard來新建一個項目,項目名為HelloWorld,確定后顯示項目向導有關選項,如下圖:
默認情況下,developer Kit location是指向C盤的,如果你的MAYA安裝在其它地方,則需要指定相應的MAYA安裝路徑,它用來告訴VS在編譯時所需的 Maya相關頭文件與庫文件在何處:
再點擊左側Plug-in type,如下圖:
點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打開插件加載窗口:
把我們的HelloWorld.mll插件加載進來,然后在我們的maya命令行窗口中輸入HelloWorld命令對插件進行測試,Hello World!就會顯示在腳本編輯器中,如下圖所示。
項目源代碼鏈接:http://pan.baidu.com/s/1pLujp2r 密碼:qplx
四.節點式插件項目創建
MAYA的插件大體上分為兩大類型,命令(Command)和結點(Node),多數情況下,命令都是為結點服務的,那么什么maya的結點呢?我們可以把結點想像為一個數據流處理器,每個結點,它都有輸入接口,輸出接口,及對數據進行處理的內核,如下圖:
我們說MYAY是基於結點的插件式軟件架構,所以在MAYA底層,對所有的數據都是通過把大量這些的結點連接起來,一層層地進行運算和處理才得到最終的結果。這種基於結點的軟件架構,其最大的好處就是制作人員可以根據需求把各種節點隨意地連接起來,從而實現讓制作人員可以最大限度在發揮自已的想像空間和創意能力。
下面我們來實現一個功能簡單的結點,該結點只有一個輸入接口和一個輸出接口(注:一個結點可以有多個輸入接口和輸出接口),要實現的功能是把輸入的數據乘以0.5變成原來的一半,然后輸出。
1. 創建項目,生成插件
打開MayaPluginWizard,新建一個Dependency Graph Node插件,如下圖:
點擊Finish后,項目生成三個文件,分別是:halfScaleNodeNode.h, halfScaleNodeNode.cpp, pluginMain.cpp,如下圖:
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;
從超圖上我們可以后清晰地看到,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