一.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. 函數
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