下面的maya履带动画 api源码,需要大家自己在vs里编译。
//首先是头文件代码:
//头文件名称:glideDeformer.h
#ifndef glideDeformer_h
#define glideDeformer_h
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class glideDeformer:public MPxDeformerNode
{
public:
glideDeformer(){};
virtual
~glideDeformer(){};
static
void * creator(){return new glideDeformer;};
static
MStatus initializeAttr();
virtual
MStatus deform(MDataBlock
&block, MItGeometry &iter, const MMatrix &mat, unsigned
int multiIndex);
bool
getClosestUVParam(MPoint & point,MFnNurbsSurface &
surfaceFn,MObject surfaceData,double & paramU,double &
paramV);
bool
getPointFroamSurfacePoint(MPoint & point,MFnNurbsSurface
& surfaceFn,MObject surfaceData,double & paramU,double
& paramV,MPoint & outPoint);
bool
getPointAfterDeformed(MPoint &
pointInSurfaceMatrix,MFnNurbsSurface & surfaceFn,MObject
surfaceData,double & paramU,double & paramV,MPoint &
pointAffterDeformed);
static
MString nodeName;
static
MTypeId nodeId;
static
MObject glide;
static
MObject fixedBase;
static
MObject aimDirection;
static
MObject drivenSurface;
static
MObject baseSurface;
};
#endif
//CPP文件
#include "glideDeformer.h"
MString glideDeformer::nodeName=MString("glideDeformer");
MTypeId glideDeformer::nodeId=MTypeId(0x87031);
MObject glideDeformer::glide;
MObject glideDeformer::fixedBase;
MObject glideDeformer::aimDirection;
MObject glideDeformer::drivenSurface;
MObject glideDeformer::baseSurface;
using std::cout;
using std::endl;
MStatus glideDeformer::initializeAttr()
{
MFnNumericAttribute
numattr;
MFnTypedAttribute
typedattr;
MFnEnumAttribute
enumattr;
glide=numattr.create("glide","gld",MFnNumericData::kFloat,0.0);
numattr.setKeyable(true);
numattr.setStorable(true);
fixedBase=numattr.create("fixedBase","fxb",MFnNumericData::kBoolean,0);
numattr.setKeyable(true);
numattr.setStorable(true);
aimDirection=enumattr.create("aimDirection","adr",0);
enumattr.addField("directionU",0);
enumattr.addField("directionV",1);
enumattr.setKeyable(true);
enumattr.setStorable(true);
drivenSurface=typedattr.create("drivenSurface","dsf",MFnData::kNurbsSurface);
typedattr.setStorable(false);
baseSurface=typedattr.create("baseSurface","bsf",MFnData::kNurbsSurface);
typedattr.setStorable(false);
//addAttribtue
addAttribute(glide);
addAttribute(fixedBase);
addAttribute(drivenSurface);
addAttribute(baseSurface);
addAttribute(aimDirection);
attributeAffects(glide,outputGeom);
attributeAffects(fixedBase,outputGeom);
attributeAffects(aimDirection,outputGeom);
attributeAffects(drivenSurface,outputGeom);
attributeAffects(baseSurface,outputGeom);
return
MS::kSuccess;
};
bool glideDeformer::getClosestUVParam(MPoint &
point,MFnNurbsSurface & surfaceFn,MObject surfaceData,double
& paramU,double & paramV)
{
surfaceFn.setObject(surfaceData);
MPoint
tmpPoint=surfaceFn.closestPoint(point,0,0,false,0.001,MSpace::kWorld);
MStatus
stat=surfaceFn.getParamAtPoint(tmpPoint,paramU,paramV,true,MSpace::kWorld,0.001);
return
true;
};
bool glideDeformer::getPointFroamSurfacePoint(MPoint &
point,MFnNurbsSurface & surfaceFn,MObject surfaceData,double
& paramU,double & paramV,MPoint & outPoint)
{
surfaceFn.setObject(surfaceData);
MVector
tangentU;
MVector
tangentV;
MVector
normal;
MPoint
surfacePoint;
surfaceFn.getPointAtParam(paramU,paramV,surfacePoint,MSpace::kWorld);
normal=surfaceFn.normal(paramU,paramV,MSpace::kWorld);
surfaceFn.getTangents(paramU,paramV,tangentU,tangentV,MSpace::kWorld);
tangentU.normalize();
tangentV.normalize();
normal.normalize();
double
matrix[4][4]={{tangentU.x,tangentU.y,tangentU.z,0.0},{normal.x,normal.y,normal.z,0.0},{tangentV.x,tangentV.y,tangentV.z,0.0},{surfacePoint.x,surfacePoint.y,surfacePoint.z,1.0}};
MMatrix
surfacePointMatrix=MMatrix(matrix);
//cout<<"baseSurfacePointMatrix:"<
outPoint=point*surfacePointMatrix.inverse();
//cout<<"pointInSurfaceMatrixPosition:"<
return
true;
};
bool glideDeformer::getPointAfterDeformed(MPoint &
pointInSurfaceMatrix,MFnNurbsSurface & surfaceFn,MObject
surfaceData,double & paramU,double & paramV,MPoint &
pointAffterDeformed)
{
surfaceFn.setObject(surfaceData);
MVector
tangentU;
MVector
tangentV;
MVector
normal;
MPoint
surfacePoint;
surfaceFn.getPointAtParam(paramU,paramV,surfacePoint,MSpace::kWorld);
normal=surfaceFn.normal(paramU,paramV,MSpace::kWorld);
surfaceFn.getTangents(paramU,paramV,tangentU,tangentV,MSpace::kWorld);
tangentU.normalize();
tangentV.normalize();
normal.normalize();
double
matrix[4][4]={{tangentU.x,tangentU.y,tangentU.z,0.0},{normal.x,normal.y,normal.z,0.0},{tangentV.x,tangentV.y,tangentV.z,0.0},{surfacePoint.x,surfacePoint.y,surfacePoint.z,1.0}};
MMatrix
surfacePointMatrix=MMatrix(matrix);
//cout<<"drivenSurfacePointMatrix:"<
pointAffterDeformed=pointInSurfaceMatrix*surfacePointMatrix;
//cout<<"pointAffterDeformedPosition:"<
return
true;
};
MStatus glideDeformer::deform(MDataBlock
&block, MItGeometry & iter, const MMatrix &mat,
unsigned int multiIndex)
{
//cout<<"deform()方法调用..."<
MDataHandle
envelopehandle=block.inputValue(envelope);
MDataHandle
glidehandle=block.inputValue(glide);
MDataHandle
fixedBasehandle=block.inputValue(fixedBase);
MDataHandle
aimDirectionhandle=block.inputValue(aimDirection);
float
envelopevalue=envelopehandle.asFloat();
float
glidevalue=glidehandle.asFloat();
short
aimDirectionvalue=aimDirectionhandle.asShort();
bool
fixedBasevalue=fixedBasehandle.asBool();
if
(envelopevalue==0.0)
{
//cout<<"envelope值为0,跳过deform方法..."<
return
MS::kSuccess;
};
MDataHandle
drivenSurfacehandle=block.inputValue(drivenSurface);
MDataHandle
baseSurfacehandle=block.inputValue(baseSurface);
MFnNurbsSurfaceData
surfaceDataFn;
MFnNurbsSurface
surfaceFn;
MObject
drivenSurfaceData=drivenSurfacehandle.asNurbsSurface();
MObject
baseSurfaceData=baseSurfacehandle.asNurbsSurface();
MObject
drivenSurfaceObj=surfaceDataFn.create();
MObject
baseSurfaceObj=surfaceDataFn.create();
surfaceFn.setObject(drivenSurfaceData);
surfaceFn.copy(drivenSurfaceObj);
int
drivenSurfaceCvU=surfaceFn.numCVsInU();
int
drivenSurfaceCvV=surfaceFn.numCVsInV();
surfaceFn.setObject(baseSurfaceData);
surfaceFn.copy(baseSurfaceObj);
int
baseSurfaceCvU=surfaceFn.numCVsInU();
int
baseSurfaceCvV=surfaceFn.numCVsInV();
cout<
if
(drivenSurfaceCvU==0||drivenSurfaceCvV==0||drivenSurfaceCvU==0||baseSurfaceCvU==0||baseSurfaceCvV==0)
{
//cout<<"drivenSurface或baseSurface不正确,变形不产生..."<
MGlobal::displayWarning("drivenSurface或baseSurface不正确,变形不产生...");
return
MS::kSuccess;
};
if
(drivenSurfaceCvU!=baseSurfaceCvU||drivenSurfaceCvV!=baseSurfaceCvV)
{
//cout<<"drivenSurface与baseSurface不匹配,变形不产生..."<
MGlobal::displayWarning("drivenSurface与baseSurface不匹配,变形不产生...");
return
MS::kSuccess;
};
MPoint
pt;
MPoint
ptAfterDeformed;
MPoint
pointInSurfacePointMatrix;
double
paramU;
double
paramV;
double
newParamU;
double
newParamV;
int
index;
float
weight;
bool
test;
if
(fixedBasevalue==false)
{
//cout<<"履带模式..."<
for(iter.reset();!iter.isDone();iter.next())
{
index=iter.index();
//cout<<"index:"<
weight=weightValue(block,multiIndex,index);
//cout<<"weight:"<
if
(weight==0)
{
//cout<<"权重为0,此点变形不产生..."<
continue;
};
pt=iter.position(MSpace::kObject);
pt=pt*mat;
//cout<<"position:"<