maya api 初接触

最近在学习maya api相关知识,书和教程看了不少,今天正好来练兵,也算是进入csdn的第一篇博文吧。

</pre><p></p><p></p><p>最近项目里要用一个查找x轴对称点的功能。分别写了三段代码:</p><p></p><p>maya.cmds:</p><pre name="code" class="python">def getOppsiteVertex_mayacmds():
    sel = cmds.filterExpand( ex=True, sm=31 )
    obj = sel[0].split('.')[0]
    verNum = cmds.polyEvaluate(obj,vertex=1)
    ret = []
    for i in sel:
        pos = cmds.pointPosition(i,l=1)
        for j in xrange(verNum):
            pos_1 = cmds.pointPosition(obj+'.vtx['+str(j)+']')
            if math.fabs(-pos_1[0]-pos[0])<0.01 and math.fabs(pos_1[1]-pos[1])<0.01 and math.fabs(pos_1[2]-pos[2])<0.01:
                ret.append(j)
                break
    return ret    

maya python api :

def findOpoVertex_pythonApi():
    ret = []
    mSel = om.MSelectionList()
    om.MGlobal.getActiveSelectionList(mSel)
    mDagPath = om.MDagPath()
    component = om.MObject()
    mSel.getDagPath(0,mDagPath,component)
    meshIter = om.MItMeshVertex(mDagPath,component)
    while(not meshIter.isDone()):
        pt = om.MPoint()
        pt = meshIter.position(om.MSpace.kObject)
        meshIter_1 = om.MItMeshVertex(mDagPath)
        while(not meshIter_1.isDone()):
            pt_1 = om.MPoint()
            pt_1 = meshIter_1.position(om.MSpace.kObject)
            if math.fabs(-pt.x-pt_1.x)<0.01 and math.fabs(pt.y-pt_1.y) <0.01 and math.fabs(pt.z-pt_1.z)<0.01 :
                ret.append(meshIter_1.index())
                break
            meshIter_1.next()
        meshIter.next()
    return ret    

c++ api:

#include <maya/MSimple.h>
#include<maya/MGlobal.h>
#include<maya/MDagPath.h>
#include<maya/MSelectionList.h>
#include<maya/MFnDagNode.h>
#include<maya/MIOStream.h>
#include<maya/MFnMesh.h>
#include<maya/MFloatPointArray.h>
#include<maya/MString.h>
#include<maya/MItSelectionList.h>
#include<maya/MItMeshVertex.h>
#include<maya/MStringArray.h>

DeclareSimpleCommand(pickExample,"xdh","1.0");
MStatus pickExample::doIt(const MArgList &args)
{
    MStatus stat = MS::kSuccess;
    MSelectionList selection;
    
    MGlobal::getActiveSelectionList(selection);
    MDagPath dagPath,dagPath_1;
    MObject component,component_1;
    MItSelectionList iter(selection);
    selection.getDagPath(0,dagPath,component);
    MItMeshVertex meshIter(dagPath,component,&stat);
    MStringArray verIndexArray;
    if(stat == MS::kSuccess)
    {
        for(;!meshIter.isDone();meshIter.next())
        {
            MPoint pt = meshIter.position(MSpace::kObject);
            MItMeshVertex meshIter_1(dagPath);
            for(;!meshIter_1.isDone();meshIter_1.next())
            {
                MPoint pt_1 = meshIter_1.position(MSpace::kObject);
                if (abs(-pt.x-pt_1.x)<0.01 && abs(pt.y-pt_1.y)<0.01 && abs(pt.z-pt_1.z)<0.01)
               {
                    verIndexArray.append(MString("")+meshIter_1.index());
                   break;
                }
            
            
            }
            


        }
    }
    

    setResult(verIndexArray);
    return MS::kSuccess;
        
        
}

然后在maya里跑了下,三种方式写出来的速度对比。


模型一:球模型,总共382个顶点,选择了76个。

结果如图:


使用 maya.cmds耗时 0.5秒

使用python api 耗时 0.238秒

使用c++ api 耗时 0.0625秒


模型二:球模型,总共6242个顶点,选择641个顶点。

结果如图:



使用 maya.cmds耗时 80秒

使用python api 耗时 32秒

使用c++ api 耗时 0.31秒


模型三:球模型,总共24962个顶点,选择472个顶点。

结果如图:



使用 maya.cmds耗时 236秒

使用python api 耗时 95秒

使用c++ api 耗时 0.8秒



后记:

1.三种方式,效率高下立判。

2.csdn的博客真好,贴代码,贴图片都好方便  ^ ^

猜你喜欢

转载自blog.csdn.net/xdhstc/article/details/40355155