当前位置:   article > 正文

maya api 初接触

maya api

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

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

maya python api :

  1. def findOpoVertex_pythonApi():
  2. ret = []
  3. mSel = om.MSelectionList()
  4. om.MGlobal.getActiveSelectionList(mSel)
  5. mDagPath = om.MDagPath()
  6. component = om.MObject()
  7. mSel.getDagPath(0,mDagPath,component)
  8. meshIter = om.MItMeshVertex(mDagPath,component)
  9. while(not meshIter.isDone()):
  10. pt = om.MPoint()
  11. pt = meshIter.position(om.MSpace.kObject)
  12. meshIter_1 = om.MItMeshVertex(mDagPath)
  13. while(not meshIter_1.isDone()):
  14. pt_1 = om.MPoint()
  15. pt_1 = meshIter_1.position(om.MSpace.kObject)
  16. 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 :
  17. ret.append(meshIter_1.index())
  18. break
  19. meshIter_1.next()
  20. meshIter.next()
  21. return ret

c++ api:

  1. #include <maya/MSimple.h>
  2. #include<maya/MGlobal.h>
  3. #include<maya/MDagPath.h>
  4. #include<maya/MSelectionList.h>
  5. #include<maya/MFnDagNode.h>
  6. #include<maya/MIOStream.h>
  7. #include<maya/MFnMesh.h>
  8. #include<maya/MFloatPointArray.h>
  9. #include<maya/MString.h>
  10. #include<maya/MItSelectionList.h>
  11. #include<maya/MItMeshVertex.h>
  12. #include<maya/MStringArray.h>
  13. DeclareSimpleCommand(pickExample,"xdh","1.0");
  14. MStatus pickExample::doIt(const MArgList &args)
  15. {
  16. MStatus stat = MS::kSuccess;
  17. MSelectionList selection;
  18. MGlobal::getActiveSelectionList(selection);
  19. MDagPath dagPath,dagPath_1;
  20. MObject component,component_1;
  21. MItSelectionList iter(selection);
  22. selection.getDagPath(0,dagPath,component);
  23. MItMeshVertex meshIter(dagPath,component,&stat);
  24. MStringArray verIndexArray;
  25. if(stat == MS::kSuccess)
  26. {
  27. for(;!meshIter.isDone();meshIter.next())
  28. {
  29. MPoint pt = meshIter.position(MSpace::kObject);
  30. MItMeshVertex meshIter_1(dagPath);
  31. for(;!meshIter_1.isDone();meshIter_1.next())
  32. {
  33. MPoint pt_1 = meshIter_1.position(MSpace::kObject);
  34. 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)
  35. {
  36. verIndexArray.append(MString("")+meshIter_1.index());
  37. break;
  38. }
  39. }
  40. }
  41. }
  42. setResult(verIndexArray);
  43. return MS::kSuccess;
  44. }

然后在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的博客真好,贴代码,贴图片都好方便  ^ ^

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/418599
推荐阅读
相关标签
  

闽ICP备14008679号