Maya编程——沿Curve绘制圆柱
操作流程:
1. VS运行代码,生成插件
2. 打开Maya绘制曲线,加载插件
3. 选中绘制的曲线,运行插件
Posts1.0
代码:
- #include <maya/MSimple.h>
- #include <maya/MGlobal.h>
- #include <maya/MFnPlugin.h>
- #include <maya/MPxCommand.h>
- #include <maya/MSelectionList.h>
- #include <maya/MDagPath.h>
- #include <maya/MFnNurbsCurve.h>
- #include <maya/MItSelectionList.h>
- #include <maya/MPoint.h>// 命令类
- class PostsCmd : public MPxCommand
- {
- public :
- // 执行命令时调用,完成命令的实际工作
- virtual MStatus doIt(const MArgList&);
- // 返回的是命令已分配过的一个实例
- static void *creator() {
- return new PostsCmd;
- }
- };
- MStatus PostsCmd::doIt(const MArgList&)
- {
- // 初始化数目、半径、高度
- const int nPosts = ;
- const double radius = 0.5;
- const double height = 5.0;
- // 创建当前已选定对象的一个清单,Selection对象用语保存清单对象
- MSelectionList selection;
- MGlobal::getActiveSelectionList(selection);
- MDagPath dagPath;
- MFnNurbsCurve curveFn;
- // cylinder不允许显示设置其高度,必须通过heightRatio的值来设置
- double heightRatio = height / radius;
- // 设置了一个过滤器的迭代器,排除不是NURBS曲线的所有其他对象
- MItSelectionList iter(selection, MFn::kNurbsCurve);
- MGlobal::displayInfo("Now...\n");
- int it = ;
- // 迭代NURBS曲线
- for (; !iter.isDone(); iter.next())
- {
- MGlobal::displayInfo("NURBS Curve " + it);
- it++;
- // 找出当前曲线的完整DAG路径
- iter.getDagPath(dagPath);
- // 将NURBS曲线函数集MFnNurbsCurve与DAG路径相关联
- // 这样就规定了以后的所有函数集操作均被应用到DAG路径给出的对象上
- curveFn.setObject(dagPath);
- // 得到曲线参数范围的始末值
- double tStart, tEnd;
- curveFn.getKnotDomain(tStart, tEnd);
- MPoint pt;
- unsigned int i;
- double t;
- // 沿曲线长度创建了数目为nPosts的圆柱,参数范围按圆柱数目来分割
- double tIncr = (tEnd - tStart) / (nPosts - );
- // t值没变一步,就沿曲线生成一个圆柱
- for (i = , t = tStart; i < nPosts; i++, t += tIncr)
- {
- // 返回曲线上的一个点(世界坐标)
- curveFn.getPointAtParam(t, pt, MSpace::kWorld);
- // 中心点在轴心,做出调整使其地面位于曲线上
- pt.y += 0.5 * height;
- // 执行MEL命令创建圆柱
- // pivot-轴心
- MGlobal::executeCommand(MString("cylinder -pivot ") +
- pt.x + " " + pt.y + " " + pt.z + " -radius " + radius + " -axis 0 1 0 -heightRatio "
- + heightRatio);
- }
- }
- return MS::kSuccess;
- }
- // 初始化
- // obj指向有关插件类型的maya内部数据的一个句柄
- MStatus initializePlugin(MObject obj)
- {
- // 将MFnPlugin关联到MObject上
- MFnPlugin pluginFn(obj, "Amber W", "1.0");
- MStatus stat;
- stat = pluginFn.registerCommand("Posts", PostsCmd::creator);
- if (!stat) {
- stat.perror("registerCommand failed");
- }
- return stat;
- }
- // 卸载
- MStatus uninitializePlugin(MObject obj)
- {
- MFnPlugin pluginFn(obj);
- MStatus stat;
- stat = pluginFn.deregisterCommand("Posts");
- if (!stat) {
- stat.perror("deregisterCommand failed");
- }
- return stat;
- }
其中大部分代码都是创建命令必须的,可以用一句话来代替:
- DeclareSimpleCommand( Posts, "", "");
因此,最终代码简化版为
- #include <maya/MSimple.h>
- #include <maya/MGlobal.h>
- #include <maya/MFnPlugin.h>
- #include <maya/MPxCommand.h>
- #include <maya/MSelectionList.h>
- #include <maya/MDagPath.h>
- #include <maya/MFnNurbsCurve.h>
- #include <maya/MItSelectionList.h>
- #include <maya/MPoint.h>
- DeclareSimpleCommand( Posts, "", "");
- MStatus PostsCmd::doIt(const MArgList&)
- {
- // 初始化数目、半径、高度
- const int nPosts = ;
- const double radius = 0.5;
- const double height = 5.0;
- // 创建当前已选定对象的一个清单,Selection对象用语保存清单对象
- MSelectionList selection;
- MGlobal::getActiveSelectionList(selection);
- MDagPath dagPath;
- MFnNurbsCurve curveFn;
- // cylinder不允许显示设置其高度,必须通过heightRatio的值来设置
- double heightRatio = height / radius;
- // 设置了一个过滤器的迭代器,排除不是NURBS曲线的所有其他对象
- MItSelectionList iter(selection, MFn::kNurbsCurve);
- MGlobal::displayInfo("Now...\n");
- int it = ;
- // 迭代NURBS曲线
- for (; !iter.isDone(); iter.next())
- {
- MGlobal::displayInfo("NURBS Curve " + it);
- it++;
- // 找出当前曲线的完整DAG路径
- iter.getDagPath(dagPath);
- // 将NURBS曲线函数集MFnNurbsCurve与DAG路径相关联
- // 这样就规定了以后的所有函数集操作均被应用到DAG路径给出的对象上
- curveFn.setObject(dagPath);
- // 得到曲线参数范围的始末值
- double tStart, tEnd;
- curveFn.getKnotDomain(tStart, tEnd);
- MPoint pt;
- unsigned int i;
- double t;
- // 沿曲线长度创建了数目为nPosts的圆柱,参数范围按圆柱数目来分割
- double tIncr = (tEnd - tStart) / (nPosts - );
- // t值没变一步,就沿曲线生成一个圆柱
- for (i = , t = tStart; i < nPosts; i++, t += tIncr)
- {
- // 返回曲线上的一个点(世界坐标)
- curveFn.getPointAtParam(t, pt, MSpace::kWorld);
- // 中心点在轴心,做出调整使其地面位于曲线上
- pt.y += 0.5 * height;
- // 执行MEL命令创建圆柱
- // pivot-轴心
- MGlobal::executeCommand(MString("cylinder -pivot ") +
- pt.x + " " + pt.y + " " + pt.z + " -radius " + radius + " -axis 0 1 0 -heightRatio "
- + heightRatio);
- }
- }
- return MS::kSuccess;
- }
Posts2.0
可以在命令行出现参数,执行时输入:
- Posts -r 1.0 -n -h 10.0;
修改doIt前面一部分的代码为:
- MStatus Posts::doIt(const MArgList& args)
- {
- // 初始化的数目、半径、高度
- int nPosts = ;
- double radius = 0.5;
- double height = 5.0;
- // 从命令行中获取参数值
- unsigned index;
- // 返回含有给定标记的参数的索引(两种标记)
- index = args.flagIndex("n", "number");
- // 如果命令行中没有设置该值,index就被设置为MArgList::kInvalidArgIndex
- if (MArgList::kInvalidArgIndex != index) {
- args.get(index + , nPosts);
- }
- index = args.flagIndex("r", "radius");
- if (MArgList::kInvalidArgIndex != index) {
- args.get(index + , radius);
- }
- index = args.flagIndex("h", "height");
- if (MArgList::kInvalidArgIndex != index) {
- args.get(index + , height);
- }
- .......
}
Posts3.0
使用MSyntax和MArgsDatabase类。这两个类在可以使用的参数数目和类型方面带来了更大的灵活性。而且提供了更好的参数,类型检查机制。
MSyntax类提供了一种简便的方法来为你的命令行指定所有可能的参数。
MArgsDatabase类被用来分析和分隔不同的标记和它们的值。
- #include <maya/MSimple.h>
- #include <maya/MGlobal.h>
- #include <maya/MFnPlugin.h>
- #include <maya/MPxCommand.h>
- #include <maya/MSelectionList.h>
- #include <maya/MDagPath.h>
- #include <maya/MFnNurbsCurve.h>
- #include <maya/MItSelectionList.h>
- #include <maya/MPoint.h>
- #include <maya/MSyntax.h>
- #include <maya/MArgDatabase.h>
- const char *numberFlag = "-n", *numberLongFlag = "-number";
- const char *radiusFlag = "-r", *radiusLongFlag = "-radius";
- const char *heightFlag = "-h", *heightLongFlag = "-height";
- class PostsCmd : public MPxCommand
- {
- public:
- // 执行命令时调用,完成命令的实际工作
- virtual MStatus doIt(const MArgList&);
- // 返回的是命令已分配过的一个实例
- static void *creator() {
- return new PostsCmd;
- }
- static MSyntax newSyntax();
- };
- // 指定标记的参数的数据类型
- MSyntax PostsCmd::newSyntax()
- {
- MSyntax syntax;
- syntax.addFlag(numberFlag, numberLongFlag, MSyntax::kLong);
- syntax.addFlag(radiusFlag, radiusLongFlag, MSyntax::kDouble);
- syntax.addFlag(heightFlag, heightLongFlag, MSyntax::kDouble);
- return syntax;
- }
- MStatus PostsCmd::doIt(const MArgList& args)
- {
- // 初始化的数目、半径、高度
- int nPosts = ;
- double radius = 0.5;
- double height = 5.0;
- MArgDatabase argData(syntax(), args);
- // 依次检查每个标记,看其值是否已经设置
- if (argData.isFlagSet(numberFlag))
- {
- argData.getFlagArgument(numberFlag, , nPosts);
- }
- if (argData.isFlagSet(radiusFlag))
- {
- argData.getFlagArgument(radiusFlag, , radius);
- }
- if (argData.isFlagSet(heightFlag))
- {
- argData.getFlagArgument(heightFlag, , height);
- }
- // 创建当前已选定对象的一个清单,Selection对象用语保存清单对象
- MSelectionList selection;
- MGlobal::getActiveSelectionList(selection);
- MDagPath dagPath;
- MFnNurbsCurve curveFn;
- // cylinder不允许显示设置其高度,必须通过heightRatio的值来设置
- double heightRatio = height / radius;
- // 设置了一个过滤器的迭代器,排除不是NURBS曲线的所有其他对象
- MItSelectionList iter(selection, MFn::kNurbsCurve);
- int it = ;
- // 迭代NURBS曲线
- for (; !iter.isDone(); iter.next())
- {
- MGlobal::displayInfo("NURBS Curve " + it);
- it++;
- // 找出当前曲线的完整DAG路径
- iter.getDagPath(dagPath);
- // 将NURBS曲线函数集MFnNurbsCurve与DAG路径相关联
- // 这样就规定了以后的所有函数集操作均被应用到DAG路径给出的对象上
- curveFn.setObject(dagPath);
- // 得到曲线参数范围的始末值
- double tStart, tEnd;
- curveFn.getKnotDomain(tStart, tEnd);
- MPoint pt;
- unsigned int i;
- double t;
- // 沿曲线长度创建了数目为nPosts的圆柱,参数范围按圆柱数目来分割
- double tIncr = (tEnd - tStart) / (nPosts - );
- // t值没变一步,就沿曲线生成一个圆柱
- for (i = , t = tStart; i < nPosts; i++, t += tIncr)
- {
- // 返回曲线上的一个点(世界坐标)
- curveFn.getPointAtParam(t, pt, MSpace::kWorld);
- // 中心点在轴心,做出调整使其地面位于曲线上
- pt.y += 0.5 * height;
- // 执行MEL命令创建圆柱
- // pivot-轴心
- MGlobal::executeCommand(MString("cylinder -pivot ") +
- pt.x + " " + pt.y + " " + pt.z + " -radius " + radius + " -axis 0 1 0 -heightRatio "
- + heightRatio);
- }
- }
- return MS::kSuccess;
- }
- // 初始化
- // obj指向有关插件类型的maya内部数据的一个句柄
- MStatus initializePlugin(MObject obj)
- {
- // 将MFnPlugin关联到MObject上
- MFnPlugin pluginFn(obj, "Amber W", "1.0");
- MStatus stat;
- // 为了让maya知道要使用自定义的MSyntax对象
- stat = pluginFn.registerCommand("Posts", PostsCmd::creator,
- PostsCmd::newSyntax );
- if (!stat) {
- stat.perror("registerCommand failed");
- }
- return stat;
- }
- // 卸载
- MStatus uninitializePlugin(MObject obj)
- {
- MFnPlugin pluginFn(obj);
- MStatus stat;
- stat = pluginFn.deregisterCommand("Posts");
- if (!stat) {
- stat.perror("deregisterCommand failed");
- }
- return stat;
- }
Posts4.0
参考:《Maya5.0编程全攻略》
Maya编程——沿Curve绘制圆柱的更多相关文章
- Directx 3D编程实例:绘制3DMesh
最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要.写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客 ...
- 学习windows编程 day4 之 绘制随机矩形和peekMessage
#include <windows.h> #include <strsafe.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT messa ...
- Directx 3D编程实例:绘制可变速旋转的三角形
最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要. 写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博 ...
- cesium编程入门(五)绘制形状
通过Entity添加形状 先来看一个添加立方体的例子 var viewer = new Cesium.Viewer('cesiumContainer'); var redBox = **viewer. ...
- Maya编程——节点&命令
代码写完出现问题: 查了一下原因:
- OpenGL基础图形编程
一.OpenGL与3D图形世界1.1.OpenGL使人们进入三维图形世界 我们生活在一个充满三维物体的三维世界中,为了使计算机能精确地再现这些物体,我们必须能在三维空间描绘这些物体.我们又生活在一个充 ...
- 【转】OpenGL基础图形编程(一)
原文:http://blog.chinaunix.net/uid-20638550-id-1909183.html 分类: 一.OpenGL与3D图形世界 1.1.OpenGL使人们进入三维图形世界 ...
- CG资源网 - Maya教程
Maya中mentalray灯光渲染终极训练视频教程 http://www.cgtsj.com/cg/f/vx3627/index.html Maya无人机建模制作训练视频教程第一季 http://w ...
- Maya FEM节点框架完成
这几天把物理模拟框架移植到maya之中了. maya编程有一点比较关键,就是要让自己的程序逻辑适应maya的节点求值机制.在物理模拟中,往往需要进行时间积分,对此我的解决办法是,写一个节点rigSim ...
随机推荐
- .gitignore 标准模板 -适用于SpringBoot+Vue项目 -Idea+VSCode开发
.gitignore 标准模板 -适用于SpringBoot+Vue项目 node_modules/ target/ !.mvn/wrapper/maven-wrapper.jar ### STS # ...
- utf8mb4版本设置django
'OPTIONS':{ 'init_command':"SET sql_mode='STRICT_TRANS_TABLES'", 'charset':'utf8mb4',
- 《团队名称》第八次团队作业:Alpha冲刺day3
项目 内容 这个作业属于哪个课程 2016计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8-软件测试与ALPHA冲刺 团队名称 快活帮 作业学习目标 (1)掌握 ...
- HDU-4794:Arnold(斐波拉契循环节 二次剩余)
本题我只是个搬运工,主要是抢救补板子,所以自己就没写.https://blog.csdn.net/u013534123/article/details/78058997 题意: 大致题意是给你一个N* ...
- CMDS目的端数据库碎片整理记录
CMDS目的端数据库碎片整理记录 看看数据库里面需要做整理的表有哪些,条件可以根据需求稍微改动一下 SQL> select * from ( 2 select a.owner, 3 a.tabl ...
- 33、安装MySQL
一.Windows安装MySQL 1.下载 打开网址,页面如下,确认好要下载的操作系统,点击Download. 可以不用登陆或者注册,直接点击No thanks,just start my downl ...
- 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 ...
- python - 全局中间件(2.7)
一.场景 在网站的所有页面中可能某些地方都需要相同的数据,此时可以在Django中定义全局数据并存储在session中,或使用模板语言放入页面中 注意:一定要加上 try: 进行潜在的异常捕捉,因为一 ...
- C++后端工程师需要看的书籍
C++基础书籍<C++ primer><深度探索C++对象模型><Effective C++><more effective C++><STL源码 ...
- spring注解式参数校验列表
校验注释列表: @AssertFalse Boolean,boolean 验证注解的元素值是false @AssertTrue Boolean,boolean 验证注解的元素值是true @NotNu ...