Maya教程之maya 履带动画绑定插件
凌雪 2018-07-30 来源 :网络 阅读 3026 评论 0

摘要:本文将带你了解Maya教程之maya 履带动画绑定插件,希望本文对大家学Maya有所帮助

本文将带你了解Maya教程之maya 履带动画绑定插件,希望本文对大家学Maya有所帮助


下面的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:"<<surfacePointMatrix<<endl;
   
       
   
        outPoint=point*surfacePointMatrix.inverse();
   
          //cout<<"pointInSurfaceMatrixPosition:"<<outPoint<<endl;
   
        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:"<<surfacePointMatrix<<endl;
   
        pointAffterDeformed=pointInSurfaceMatrix*surfacePointMatrix;
   
          //cout<<"pointAffterDeformedPosition:"<<pointAffterDeformed<<endl;
   
        return
    true;
    };
   
   
    MStatus  glideDeformer::deform(MDataBlock
    &block, MItGeometry & iter, const MMatrix &mat,
    unsigned int multiIndex)
    {
        //cout<<"deform()方法调用..."<<endl;
   
        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方法..."<<endl;
   
            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<<drivenSurfaceCvU<<"  "<<drivenSurfaceCvV<<"  "<<baseSurfaceCvU<<"  "<<baseSurfaceCvV<<" "<<endl;
   
        if
      (drivenSurfaceCvU==0||drivenSurfaceCvV==0||drivenSurfaceCvU==0||baseSurfaceCvU==0||baseSurfaceCvV==0)
   
        {
   
              //cout<<"drivenSurface或baseSurface不正确,变形不产生..."<<endl;
   
              MGlobal::displayWarning("drivenSurface或baseSurface不正确,变形不产生...");
   
            return
    MS::kSuccess;
        };
   
     if
    (drivenSurfaceCvU!=baseSurfaceCvU||drivenSurfaceCvV!=baseSurfaceCvV)
   
     {
              //cout<<"drivenSurface与baseSurface不匹配,变形不产生..."<<endl;
   
              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<<"履带模式..."<<endl;
   
              for(iter.reset();!iter.isDone();iter.next())
   
            {
   
                index=iter.index();
   
                  //cout<<"index:"<<index<<endl;
   
                weight=weightValue(block,multiIndex,index);
   
                  //cout<<"weight:"<<weight<<endl;
   
                if
    (weight==0)
                  {
   
                    //cout<<"权重为0,此点变形不产生..."<<endl;
   
                      continue;
   
                  };
   
                pt=iter.position(MSpace::kObject);
   
                  pt=pt*mat;
   
                //cout<<"position:"<<pt<<endl;
   
               
   
              

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Maya频道!

本文由 @凌雪 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程