Quick Start for Maya API programming

Source: Internet
Author: User

A. Introduction to Maya API Programming

Autodesk? Maya? is an open product. This means, anyone outside of Autodesk can change Maya ' s existing features or add entirely new features. There is several ways you can modify Maya:

· mel?-(Maya Embedded Language) is a powerful and easy to learn scripting Language. Most common operations can is 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 APIs, and code executes approximately ten times faster than when you perform the Same task using MEL. Also, you is 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 a 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 are available on the following platforms:microsoft? Windows?, Linux?, and Apple? Mac OS? X. can use the API to implement 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 built in both ways:

· As dynamic or relocatable libraries which is 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 is provided with both C + + and Python source codes. To allow both versions to being loaded into Maya at the same we have adopted the Convention of Prefixing the Commands and nod Es from Python plugins with "SP" (e.g. Sphelix). You is not a required to follow this convention.

When your use of the dynamic libraries, the operating system, the develop on place various restrictions on what to build and NA Me plug-ins. The file extensions for Plug-ins is:

· Linux:. So

· Windows:. MLL

· Mac OS X:. Bundle

· All platforms for Python Plug-ins:. py

Content Link: http://help.autodesk.com/view/MAYAUL/2015/ENU/?guid=__files_API_Introduction_htm

Summarize:

Autodesk? Maya is an open product that can change or extend the existing features of Maya or add new features in the following four ways:

1. MEL? -(Maya embedded language) is a powerful and easy-to-learn scripting language. The most common operation can be done using Mel.

2. python-It is a powerful and easy-to-Learn scripting language that provides an interface for Maya commands.

3. C + + API (Application interface)-it can provide better performance than Mel or Python. You can use the API?? Adds a new object to Maya, and the code executes 10 times times faster than when you perform the same task with Mel. In addition, you can execute the MEL command from the API.

4. The Maya Python API-based API and allows the API to be used through the Python scripting language??。

two. Visual Studio under the Maya API programming development environment installation and use

In this article, the Microsoft Visual Studio version of Microsoft Visual Studio 2008,maya is version Maya 2009, and the installation path is: F:\Program Files (x86) \maya2009. Other versions can refer to this article.

Maya provides a wizard for Microsoft Visual Studio which can is used to quickly and easily create Visual studio Proje CTS for Maya Plug-ins. The wizard isn't installed automatically by the Maya installer so must copy some files by hand before you can start U Sing 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, 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 were working with a non-english installation of Microsoft Visual Studio and then your may has to alter the folders that The files is copied into above.

follow these steps to use the wizard

1. Start Microsoft visual Studio, Invoke File, New, Project, Visual C + + Projects and select Mayapluginwizar D.

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

3. Fill in the information to 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 informat Ion.

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

6. Click Finish to create the project.

Content Link:

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

three. Command plug-in project creation

1. Create a project, build a plugin

After the wizard tool is successfully installed, open the New Project Wizard for VS2008 and we can see the following interface

We chose Mayapluginwizard to create a new project with the project name HelloWorld, which determines the following options for displaying the Project Wizard, such as:

By default, the developer Kit location is pointing to the C drive, and if your Maya is installed elsewhere, you need to specify the appropriate Maya installation path, which is used to tell vs where the Maya-related header files and library files are needed at compile time:

Then click on the left plug-in type, such as:

Click Finish to create the simplest Maya plugin, a Maya command plug-in project without the Undo/redo feature.

The code is simple, the entire project has only one CPP file, the code is as follows:

#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");//Change 2010 to 2009

Mstatus MayaPluginWizard1::d oit (const marglist& args)

Description:

Implements the MEL MayaPluginWizard1 command.

Arguments:

Args-the argument list that is passes to the command from MEL

Return Value:

Ms::ksuccess-command succeeded

Ms::kfailure-command failed (returning this value would cause the

MEL script that's being run to terminate unless the

Error is caught using a "catch" statement.

//

{

Mstatus stat = ms::ksuccess;

Since This class was derived off of mpxcommand, you can use the

Inherited methods to return values and set error messages

//

DisplayInfo ("Hello world!"); /Show Hello World, add your own code

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

return stat;

}

We add a line to the doit () function: DisplayInfo ("Hello world!");

Then compile, if all goes well, in our Project Debug folder generated a called HELLOWORLD.MLL file, this is a generated Maya plug-in.

2. Load the plugin and test

Copy the HELLOWORLD.MLL file to the F:\Program files (x86) \maya2009\bin\plug-ins directory, and then reopen maya2009, from the menu window->settings/ Preferences->plug-in Manager opens the plug-in loading window:

Load our HELLOWORLD.MLL plugin and then test the plug-in by entering the HelloWorld command in our Maya command Line window, and the Hello world! will appear in the Script Editor as shown in.

Project source Code Link: http://pan.baidu.com/s/1pLujp2r Password: QPLX

Four. node plug-in project creation

Maya's plug-ins are broadly divided into two major types, command and node (node), and in most cases, commands are for nodes, so what is the Maya node? We can think of nodes as a data flow processor, each node, which has an input interface, an output interface, and a kernel for processing data, such as:


We say that Myay is a node-based, plug-in software architecture, so at the bottom of Maya, all of the data is connected by a large number of these nodes, and the results are calculated and processed on a layer-by-point basis. This node-based software architecture, the biggest advantage is that the production staff can be based on the needs of the various nodes freely connected, so that the production staff can maximize the use of their own imagination space and creative ability.

Here we implement a simple node, the node has only one input interface and an output interface (note: A node can have multiple input interface and output interface), to achieve the function is to multiply the input data by 0.5 into the original half, and then output.

1. Create a project, build a plugin

Open Mayapluginwizard and create a new dependency Graph node plugin, such as:

After clicking Finish, the project generates three files, namely: HalfScaleNodeNode.h, HalfScaleNodeNode.cpp, pluginMain.cpp, such as:

2. Code Description

(1) PluginMain.cpp file, which is the entry for each Maya plugin, Maya automatically runs the initializeplugin (Mobject obj) function when loading the node, so we can do some initialization here. The most important of these is to register this Maya node.

Status = Plugin.registernode ("Halfscalenode", Halfscalenode::id, Halfscalenode::creator, Halfscalenode::initialize) ;

if (!status) {

Status.perror ("Registernode");

return status;

}

All custom Maya nodes and commands must be registered at initialization time to be recognized and used by Maya. When registering, we should note that the new node name and Node ID cannot conflict with existing nodes, which means that the node name and Node ID must be unique across the Maya system. So at the beginning of the HalfScaleNodeNode.cpp file there is a code that defines the node ID

You must 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);

Here we are prompted to assign a unique ID to the node, or we cannot compile it. We can change the above code to

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

Mtypeid Halfscalenode::id (0x02010);

This allows us to compile the code normally.

(2) in halfScaleNodeNode.h, halfScaleNodeNode.cpp file, there is a mstatus halfscalenode::compute (const mplug& Plug, mdatablock& data) function, which is the core operation function for each node.

As we said earlier, a node is composed of input interface, output interface and operation Core, here the core of the operation is the compute () function, and the input and output interface is encapsulated in the mdatablock& data of the image inside, we pass the corresponding function, You can obtain the address of the input port and the output, and then manipulate the data:

Mdatahandle inputdata = Data.inputvalue (input, &returnstatus);

Mdatahandle Outputhandle = Data.outputvalue (halfscalenode::output);

float result = Inputdata.asfloat ();

Here, result is the original value of the input data, if we do not do any operation, directly assign it to the output interface

Outputhandle.set (result);

Then there will be no change in the resulting output. Now we multiply the input data by 0.5 and then assign it to the output interface:

float result = Inputdata.asfloat ();

result = result * 0.5;

Outputhandle.set (result);

Very easily, we have achieved what we want to achieve.

3. Load the plugin and test

Compile the project and get a Maya plugin called HALFSCALENODE.MLL, as in the previous installation, we'll copy the HALFSCALENODE.MLL into the plug-in folder and load the plugin in the Maya plugin manager.

Let's check to see if the node plugin works correctly. To build two small balls in the Maya scene, enter and run the following Mel code in a command window:

Polysphere;

Polysphere;

CreateNode Halfscalenode;

Connectattr Psphere1.translatex Halfscalenode1.input;

Connectattr Halfscalenode1.output Psphere2.translatex;

From the hyper-map we can clearly see that the Translatex attribute of the PSphere1 is connected to the input port of the halfScaleNode1, and after the operation, the Translatex attribute is output to pSphere2. Now we choose PSphere1 and then we can see that the pSphere2 will move with the pSphere1, but it's always slower, which is exactly what we want to do with the x-axis.

Project source code and Maya project file Link: http://pan.baidu.com/s/1eRJnPy6 Password: 4iec

Five. Project Commissioning

The main debugging steps can be divided into the following five steps:

1. In Visual Studio, build the target MLL in Debug mode, set to HELLOWORLD.MLL, and the file path is: g:\users\Projects\HelloWorld\HelloWorld\Debug\ HELLOWORLD.MLL;

2. Start Maya and load the MLL under this path in Maya, that is, load G:\USERS\PROJECTS\HELLOWORLD\HELLOWORLD\DEBUG\HELLOWORLD.MLL;

3. Ctrl+alt+p opens the attach to process and then attaches the VS process to the Maya process;

4. Create breakpoints in the relative code at vs;

5. Execute the command or node in the MLL, and when a breakpoint is encountered, it will automatically stop switching to VS and the debug run.

Reference documents:

1. Maya API help:http://help.autodesk.com/view/mayaul/2015/enu/?guid=__files_maya_api_introduction_htm

2. MAYA API Plugin Programming--Getting Started: http://blog.csdn.net/huawenguang/article/details/6557862

3. Maya plug-in Debugging and Tricks (1): http://blog.csdn.net/cuckon/article/details/6157403

Quick Start for Maya API programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.