一、Maya API简介

时间:2023-03-09 22:50:06
一、Maya API简介
#include <maya/MSimple.h>
#include <maya/MIOStream.h>
DeclareSimpleCommand( hello, "Autodesk", "");
MStatus hello::doIt( const MArgList& args )
{
cout << "Hello " << args.asString( ).asChar() << endl;
return MS::kSuccess;
}

MArgList接受来自MEL command输入的命令参数。

一个创建NURBS CURVE的例子: 可以学到1命令的parse, 2创建MObject的方法。3. Maya中的数据结构使用

#include <math.h>
#include <maya/MSimple.h>
#include <maya/MIOStream.h>
#include <maya/MFnNurbsCurve.h>
#include <maya/MPointArray.h>
#include <maya/MDoubleArray.h>
#include <maya/MPoint.h>
DeclareSimpleCommand( helix, "Autodesk - Example", "3.0");
MStatus helix::doIt( const MArgList& args )
{
MStatus stat;
const unsigned deg = ; // Curve Degree
const unsigned ncvs = ; // Number of CVs
const unsigned spans = ncvs - deg; // Number of spans
const unsigned nknots = spans+*deg-;// Number of knots
double radius = 4.0; // Helix radius
double pitch = 0.5; // Helix pitch
unsigned i;
// Parse the arguments.
for ( i = ; i < args.length(); i++ )
if ( MString( "-p" ) == args.asString( i, &stat )
&& MS::kSuccess == stat)
{
double tmp = args.asDouble( ++i, &stat );
if ( MS::kSuccess == stat )
pitch = tmp;
}
else if ( MString( "-r" ) == args.asString( i, &stat )
&& MS::kSuccess == stat)
{
double tmp = args.asDouble( ++i, &stat );
if ( MS::kSuccess == stat )
radius = tmp;
}
MPointArray controlVertices;
MDoubleArray knotSequences;
// Set up cvs and knots for the helix
//
for (i = ; i < ncvs; i++)
controlVertices.append( MPoint( radius * cos( (double)i ),
pitch * (double)i, radius * sin( (double)i ) ) );
for (i = ; i < nknots; i++)
knotSequences.append( (double)i );
// Now create the curve
//
MFnNurbsCurve curveFn;
MObject curve = curveFn.create( controlVertices,
knotSequences, deg,
MFnNurbsCurve::kOpen,
false, false,
MObject::kNullObj,
&stat );
if ( MS::kSuccess != stat )
cout << "Error creating curve.\n";
return stat;
}

MObject是maya中所有node的父类,function sets用来对object进行操作。

Proxies用来创建除了maya中除了MObject之外对象。包括 command对象,file translator对象,和shader的对象。

二、关于和MAYA交互的方法:

1. 有4中方式:wrappers, objects, function sets, proxies

2. 操纵maya的objects:

(1)object类型:curves, surfaces, DAG nodes, dependency graph nodes, lights, shaders, textures,etc)

(2)操作这些object 的handle是MObject, MObject的析构函数不会delete object本身,只会delete the handle object

不能使用指针指向MObject,而是用MobjectHandle

3. Wrappers:简单的object,例如math classes, vectors, matrices, 他们是普通c++类的包装,例如MPointArray 和MDoubleArray。

在循环中不要申明wrapper, 除了静态的MGlobal

4. object 和 function set通常一起使用。

创建一条curve

MFnNurbsCurve curveFn;
MObject curve = curveFn.create( ... );
  • MFnNurbsCurve curveFn; creates a new curve function set which contains methods for operating on curve objects, and the create method in particular is used to create new curves.
  • MObject curve = curveFn.create( ... );creates a new Maya curve object which you can then use however you like

If you add a third line:

curve = curveFn.create( ... );

a second curve is created and the curve MObject now references the new curve. The first curve still exists—it simply is no longer referenced by the MObject handle.

5. Proxies:

  • MPx - Classes with this prefix are all Proxies, API classes designed for you to derive from and create your own object types.

6.MIt - These classes are iterators and work on MObjects much the way a function set does.

7. Error Check:

The addition of &stat to the asString() and asDouble() methods allows you to check if the casting operation succeeds.

For example, args.asString(i, &stat) could return MS::kFailure if the index is greater than the number of arguments。

MStatus类的作用:(1)重载了逻辑运算符,可以直接if进行判断。(2)可以获得MstatusCode枚举出错原因

(3)通过errorString()方法获得包含error信息的Mstring

(4)通过perror()函数打印

(5)重载== /!=运算符,可以和Mstatuscode进行比较