Maya API編程快速入門

來源:互聯網
上載者:User

標籤:data   需要   off   ima   總結   bottom   ace   fas   types   

一.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的建立工程嚮導,我們可以看到以下的介面

我們選擇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

Maya API編程快速入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.