操作流程:

1. VS运行代码,生成插件

2. 打开Maya绘制曲线,加载插件

3. 选中绘制的曲线,运行插件

Posts1.0

代码:

  1. #include <maya/MSimple.h>
  2. #include <maya/MGlobal.h>
  3. #include <maya/MFnPlugin.h>
  4. #include <maya/MPxCommand.h>
  5. #include <maya/MSelectionList.h>
  6. #include <maya/MDagPath.h>
  7. #include <maya/MFnNurbsCurve.h>
  8. #include <maya/MItSelectionList.h>
  9. #include <maya/MPoint.h>// 命令类
  10. class PostsCmd : public MPxCommand
  11. {
  12. public :
  13. // 执行命令时调用,完成命令的实际工作
  14. virtual MStatus doIt(const MArgList&);
  15.  
  16. // 返回的是命令已分配过的一个实例
  17. static void *creator() {
  18. return new PostsCmd;
  19. }
  20.  
  21. };
  22.  
  23. MStatus PostsCmd::doIt(const MArgList&)
  24. {
  25. // 初始化数目、半径、高度
  26. const int nPosts = ;
  27. const double radius = 0.5;
  28. const double height = 5.0;
  29.  
  30. // 创建当前已选定对象的一个清单,Selection对象用语保存清单对象
  31. MSelectionList selection;
  32. MGlobal::getActiveSelectionList(selection);
  33.  
  34. MDagPath dagPath;
  35. MFnNurbsCurve curveFn;
  36. // cylinder不允许显示设置其高度,必须通过heightRatio的值来设置
  37. double heightRatio = height / radius;
  38.  
  39. // 设置了一个过滤器的迭代器,排除不是NURBS曲线的所有其他对象
  40. MItSelectionList iter(selection, MFn::kNurbsCurve);
  41.  
  42. MGlobal::displayInfo("Now...\n");
  43. int it = ;
  44. // 迭代NURBS曲线
  45. for (; !iter.isDone(); iter.next())
  46. {
  47. MGlobal::displayInfo("NURBS Curve " + it);
  48. it++;
  49. // 找出当前曲线的完整DAG路径
  50. iter.getDagPath(dagPath);
  51. // 将NURBS曲线函数集MFnNurbsCurve与DAG路径相关联
  52. // 这样就规定了以后的所有函数集操作均被应用到DAG路径给出的对象上
  53. curveFn.setObject(dagPath);
  54.  
  55. // 得到曲线参数范围的始末值
  56. double tStart, tEnd;
  57. curveFn.getKnotDomain(tStart, tEnd);
  58.  
  59. MPoint pt;
  60. unsigned int i;
  61. double t;
  62.  
  63. // 沿曲线长度创建了数目为nPosts的圆柱,参数范围按圆柱数目来分割
  64. double tIncr = (tEnd - tStart) / (nPosts - );
  65. // t值没变一步,就沿曲线生成一个圆柱
  66. for (i = , t = tStart; i < nPosts; i++, t += tIncr)
  67. {
  68. // 返回曲线上的一个点(世界坐标)
  69. curveFn.getPointAtParam(t, pt, MSpace::kWorld);
  70. // 中心点在轴心,做出调整使其地面位于曲线上
  71. pt.y += 0.5 * height;
  72.  
  73. // 执行MEL命令创建圆柱
  74. // pivot-轴心
  75. MGlobal::executeCommand(MString("cylinder -pivot ") +
  76. pt.x + " " + pt.y + " " + pt.z + " -radius " + radius + " -axis 0 1 0 -heightRatio "
  77. + heightRatio);
  78. }
  79.  
  80. }
  81.  
  82. return MS::kSuccess;
  83. }
  84.  
  85. // 初始化
  86. // obj指向有关插件类型的maya内部数据的一个句柄
  87. MStatus initializePlugin(MObject obj)
  88. {
  89. // 将MFnPlugin关联到MObject上
  90. MFnPlugin pluginFn(obj, "Amber W", "1.0");
  91. MStatus stat;
  92. stat = pluginFn.registerCommand("Posts", PostsCmd::creator);
  93.  
  94. if (!stat) {
  95. stat.perror("registerCommand failed");
  96. }
  97. return stat;
  98. }
  99.  
  100. // 卸载
  101. MStatus uninitializePlugin(MObject obj)
  102. {
  103. MFnPlugin pluginFn(obj);
  104. MStatus stat;
  105. stat = pluginFn.deregisterCommand("Posts");
  106.  
  107. if (!stat) {
  108. stat.perror("deregisterCommand failed");
  109. }
  110. return stat;
  111. }

其中大部分代码都是创建命令必须的,可以用一句话来代替:

  1. DeclareSimpleCommand( Posts, "", "");

因此,最终代码简化版为

  1. #include <maya/MSimple.h>
  2. #include <maya/MGlobal.h>
  3. #include <maya/MFnPlugin.h>
  4. #include <maya/MPxCommand.h>
  5. #include <maya/MSelectionList.h>
  6. #include <maya/MDagPath.h>
  7. #include <maya/MFnNurbsCurve.h>
  8. #include <maya/MItSelectionList.h>
  9. #include <maya/MPoint.h>
  10.  
  11. DeclareSimpleCommand( Posts, "", "");
  12.  
  13. MStatus PostsCmd::doIt(const MArgList&)
  14. {
  15. // 初始化数目、半径、高度
  16. const int nPosts = ;
  17. const double radius = 0.5;
  18. const double height = 5.0;
  19.  
  20. // 创建当前已选定对象的一个清单,Selection对象用语保存清单对象
  21. MSelectionList selection;
  22. MGlobal::getActiveSelectionList(selection);
  23.  
  24. MDagPath dagPath;
  25. MFnNurbsCurve curveFn;
  26. // cylinder不允许显示设置其高度,必须通过heightRatio的值来设置
  27. double heightRatio = height / radius;
  28.  
  29. // 设置了一个过滤器的迭代器,排除不是NURBS曲线的所有其他对象
  30. MItSelectionList iter(selection, MFn::kNurbsCurve);
  31.  
  32. MGlobal::displayInfo("Now...\n");
  33. int it = ;
  34. // 迭代NURBS曲线
  35. for (; !iter.isDone(); iter.next())
  36. {
  37. MGlobal::displayInfo("NURBS Curve " + it);
  38. it++;
  39. // 找出当前曲线的完整DAG路径
  40. iter.getDagPath(dagPath);
  41. // 将NURBS曲线函数集MFnNurbsCurve与DAG路径相关联
  42. // 这样就规定了以后的所有函数集操作均被应用到DAG路径给出的对象上
  43. curveFn.setObject(dagPath);
  44.  
  45. // 得到曲线参数范围的始末值
  46. double tStart, tEnd;
  47. curveFn.getKnotDomain(tStart, tEnd);
  48.  
  49. MPoint pt;
  50. unsigned int i;
  51. double t;
  52.  
  53. // 沿曲线长度创建了数目为nPosts的圆柱,参数范围按圆柱数目来分割
  54. double tIncr = (tEnd - tStart) / (nPosts - );
  55. // t值没变一步,就沿曲线生成一个圆柱
  56. for (i = , t = tStart; i < nPosts; i++, t += tIncr)
  57. {
  58. // 返回曲线上的一个点(世界坐标)
  59. curveFn.getPointAtParam(t, pt, MSpace::kWorld);
  60. // 中心点在轴心,做出调整使其地面位于曲线上
  61. pt.y += 0.5 * height;
  62.  
  63. // 执行MEL命令创建圆柱
  64. // pivot-轴心
  65. MGlobal::executeCommand(MString("cylinder -pivot ") +
  66. pt.x + " " + pt.y + " " + pt.z + " -radius " + radius + " -axis 0 1 0 -heightRatio "
  67. + heightRatio);
  68. }
  69. }
  70.  
  71. return MS::kSuccess;
  72. }

 Posts2.0

可以在命令行出现参数,执行时输入:

  1. Posts -r 1.0 -n -h 10.0;

修改doIt前面一部分的代码为:

  1. MStatus Posts::doIt(const MArgList& args)
  2. {
  3. // 初始化的数目、半径、高度
  4. int nPosts = ;
  5. double radius = 0.5;
  6. double height = 5.0;
  7.  
  8. // 从命令行中获取参数值
  9. unsigned index;
  10. // 返回含有给定标记的参数的索引(两种标记)
  11. index = args.flagIndex("n", "number");
  12. // 如果命令行中没有设置该值,index就被设置为MArgList::kInvalidArgIndex
  13. if (MArgList::kInvalidArgIndex != index) {
  14. args.get(index + , nPosts);
  15. }
  16.  
  17. index = args.flagIndex("r", "radius");
  18. if (MArgList::kInvalidArgIndex != index) {
  19. args.get(index + , radius);
  20. }
  21.  
  22. index = args.flagIndex("h", "height");
  23. if (MArgList::kInvalidArgIndex != index) {
  24. args.get(index + , height);
  25. }
  26.  
  27. .......
    }

Posts3.0

使用MSyntax和MArgsDatabase类。这两个类在可以使用的参数数目和类型方面带来了更大的灵活性。而且提供了更好的参数,类型检查机制。

MSyntax类提供了一种简便的方法来为你的命令行指定所有可能的参数。

MArgsDatabase类被用来分析和分隔不同的标记和它们的值。

  1. #include <maya/MSimple.h>
  2. #include <maya/MGlobal.h>
  3. #include <maya/MFnPlugin.h>
  4. #include <maya/MPxCommand.h>
  5. #include <maya/MSelectionList.h>
  6. #include <maya/MDagPath.h>
  7. #include <maya/MFnNurbsCurve.h>
  8. #include <maya/MItSelectionList.h>
  9. #include <maya/MPoint.h>
  10. #include <maya/MSyntax.h>
  11. #include <maya/MArgDatabase.h>
  12.  
  13. const char *numberFlag = "-n", *numberLongFlag = "-number";
  14. const char *radiusFlag = "-r", *radiusLongFlag = "-radius";
  15. const char *heightFlag = "-h", *heightLongFlag = "-height";
  16.  
  17. class PostsCmd : public MPxCommand
  18. {
  19. public:
  20. // 执行命令时调用,完成命令的实际工作
  21. virtual MStatus doIt(const MArgList&);
  22.  
  23. // 返回的是命令已分配过的一个实例
  24. static void *creator() {
  25. return new PostsCmd;
  26. }
  27.  
  28. static MSyntax newSyntax();
  29.  
  30. };
  31.  
  32. // 指定标记的参数的数据类型
  33. MSyntax PostsCmd::newSyntax()
  34. {
  35. MSyntax syntax;
  36. syntax.addFlag(numberFlag, numberLongFlag, MSyntax::kLong);
  37. syntax.addFlag(radiusFlag, radiusLongFlag, MSyntax::kDouble);
  38. syntax.addFlag(heightFlag, heightLongFlag, MSyntax::kDouble);
  39. return syntax;
  40. }
  41.  
  42. MStatus PostsCmd::doIt(const MArgList& args)
  43. {
  44. // 初始化的数目、半径、高度
  45. int nPosts = ;
  46. double radius = 0.5;
  47. double height = 5.0;
  48.  
  49. MArgDatabase argData(syntax(), args);
  50.  
  51. // 依次检查每个标记,看其值是否已经设置
  52. if (argData.isFlagSet(numberFlag))
  53. {
  54. argData.getFlagArgument(numberFlag, , nPosts);
  55. }
  56. if (argData.isFlagSet(radiusFlag))
  57. {
  58. argData.getFlagArgument(radiusFlag, , radius);
  59. }
  60. if (argData.isFlagSet(heightFlag))
  61. {
  62. argData.getFlagArgument(heightFlag, , height);
  63. }
  64.  
  65. // 创建当前已选定对象的一个清单,Selection对象用语保存清单对象
  66. MSelectionList selection;
  67. MGlobal::getActiveSelectionList(selection);
  68.  
  69. MDagPath dagPath;
  70. MFnNurbsCurve curveFn;
  71. // cylinder不允许显示设置其高度,必须通过heightRatio的值来设置
  72. double heightRatio = height / radius;
  73.  
  74. // 设置了一个过滤器的迭代器,排除不是NURBS曲线的所有其他对象
  75. MItSelectionList iter(selection, MFn::kNurbsCurve);
  76.  
  77. int it = ;
  78. // 迭代NURBS曲线
  79. for (; !iter.isDone(); iter.next())
  80. {
  81. MGlobal::displayInfo("NURBS Curve " + it);
  82. it++;
  83. // 找出当前曲线的完整DAG路径
  84. iter.getDagPath(dagPath);
  85. // 将NURBS曲线函数集MFnNurbsCurve与DAG路径相关联
  86. // 这样就规定了以后的所有函数集操作均被应用到DAG路径给出的对象上
  87. curveFn.setObject(dagPath);
  88.  
  89. // 得到曲线参数范围的始末值
  90. double tStart, tEnd;
  91. curveFn.getKnotDomain(tStart, tEnd);
  92.  
  93. MPoint pt;
  94. unsigned int i;
  95. double t;
  96.  
  97. // 沿曲线长度创建了数目为nPosts的圆柱,参数范围按圆柱数目来分割
  98. double tIncr = (tEnd - tStart) / (nPosts - );
  99. // t值没变一步,就沿曲线生成一个圆柱
  100. for (i = , t = tStart; i < nPosts; i++, t += tIncr)
  101. {
  102. // 返回曲线上的一个点(世界坐标)
  103. curveFn.getPointAtParam(t, pt, MSpace::kWorld);
  104. // 中心点在轴心,做出调整使其地面位于曲线上
  105. pt.y += 0.5 * height;
  106.  
  107. // 执行MEL命令创建圆柱
  108. // pivot-轴心
  109. MGlobal::executeCommand(MString("cylinder -pivot ") +
  110. pt.x + " " + pt.y + " " + pt.z + " -radius " + radius + " -axis 0 1 0 -heightRatio "
  111. + heightRatio);
  112. }
  113. }
  114.  
  115. return MS::kSuccess;
  116. }
  117.  
  118. // 初始化
  119. // obj指向有关插件类型的maya内部数据的一个句柄
  120. MStatus initializePlugin(MObject obj)
  121. {
  122. // 将MFnPlugin关联到MObject上
  123. MFnPlugin pluginFn(obj, "Amber W", "1.0");
  124. MStatus stat;
  125.  
  126. // 为了让maya知道要使用自定义的MSyntax对象
  127. stat = pluginFn.registerCommand("Posts", PostsCmd::creator,
  128. PostsCmd::newSyntax );
  129.  
  130. if (!stat) {
  131. stat.perror("registerCommand failed");
  132. }
  133. return stat;
  134. }
  135.  
  136. // 卸载
  137. MStatus uninitializePlugin(MObject obj)
  138. {
  139. MFnPlugin pluginFn(obj);
  140. MStatus stat;
  141. stat = pluginFn.deregisterCommand("Posts");
  142.  
  143. if (!stat) {
  144. stat.perror("deregisterCommand failed");
  145. }
  146. return stat;
  147. }

Posts4.0

参考:《Maya5.0编程全攻略》

Maya编程——沿Curve绘制圆柱的更多相关文章

  1. Directx 3D编程实例:绘制3DMesh

    最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要.写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客 ...

  2. 学习windows编程 day4 之 绘制随机矩形和peekMessage

    #include <windows.h> #include <strsafe.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT messa ...

  3. Directx 3D编程实例:绘制可变速旋转的三角形

    最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要. 写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博 ...

  4. cesium编程入门(五)绘制形状

    通过Entity添加形状 先来看一个添加立方体的例子 var viewer = new Cesium.Viewer('cesiumContainer'); var redBox = **viewer. ...

  5. Maya编程——节点&命令

    代码写完出现问题: 查了一下原因:

  6. OpenGL基础图形编程

    一.OpenGL与3D图形世界1.1.OpenGL使人们进入三维图形世界 我们生活在一个充满三维物体的三维世界中,为了使计算机能精确地再现这些物体,我们必须能在三维空间描绘这些物体.我们又生活在一个充 ...

  7. 【转】OpenGL基础图形编程(一)

    原文:http://blog.chinaunix.net/uid-20638550-id-1909183.html  分类: 一.OpenGL与3D图形世界 1.1.OpenGL使人们进入三维图形世界 ...

  8. CG资源网 - Maya教程

    Maya中mentalray灯光渲染终极训练视频教程 http://www.cgtsj.com/cg/f/vx3627/index.html Maya无人机建模制作训练视频教程第一季 http://w ...

  9. Maya FEM节点框架完成

    这几天把物理模拟框架移植到maya之中了. maya编程有一点比较关键,就是要让自己的程序逻辑适应maya的节点求值机制.在物理模拟中,往往需要进行时间积分,对此我的解决办法是,写一个节点rigSim ...

随机推荐

  1. .gitignore 标准模板 -适用于SpringBoot+Vue项目 -Idea+VSCode开发

    .gitignore 标准模板 -适用于SpringBoot+Vue项目 node_modules/ target/ !.mvn/wrapper/maven-wrapper.jar ### STS # ...

  2. utf8mb4版本设置django

    'OPTIONS':{ 'init_command':"SET sql_mode='STRICT_TRANS_TABLES'", 'charset':'utf8mb4',

  3. 《团队名称》第八次团队作业:Alpha冲刺day3

    项目 内容 这个作业属于哪个课程 2016计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8-软件测试与ALPHA冲刺 团队名称 快活帮 作业学习目标 (1)掌握 ...

  4. HDU-4794:Arnold(斐波拉契循环节 二次剩余)

    本题我只是个搬运工,主要是抢救补板子,所以自己就没写.https://blog.csdn.net/u013534123/article/details/78058997 题意: 大致题意是给你一个N* ...

  5. CMDS目的端数据库碎片整理记录

    CMDS目的端数据库碎片整理记录 看看数据库里面需要做整理的表有哪些,条件可以根据需求稍微改动一下 SQL> select * from ( 2 select a.owner, 3 a.tabl ...

  6. 33、安装MySQL

    一.Windows安装MySQL 1.下载 打开网址,页面如下,确认好要下载的操作系统,点击Download. 可以不用登陆或者注册,直接点击No thanks,just start my downl ...

  7. IDEA+Maven+Mybatis 巨坑:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.UserMapper.findAll

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.User ...

  8. python - 全局中间件(2.7)

    一.场景 在网站的所有页面中可能某些地方都需要相同的数据,此时可以在Django中定义全局数据并存储在session中,或使用模板语言放入页面中 注意:一定要加上 try: 进行潜在的异常捕捉,因为一 ...

  9. C++后端工程师需要看的书籍

    C++基础书籍<C++ primer><深度探索C++对象模型><Effective C++><more effective C++><STL源码 ...

  10. spring注解式参数校验列表

    校验注释列表: @AssertFalse Boolean,boolean 验证注解的元素值是false @AssertTrue Boolean,boolean 验证注解的元素值是true @NotNu ...