沿曲线创建圆柱体 mayaAPI

这是自己在看《maya 5 编程全攻略》,按照里面的例子,自己讲mayaAPI插件流程走了一遍,也有一些自己的感想,还是先学C语言,再学C++,这样有更好的后期发展。

附上代码, 里面包含了redo, undo,help等功能:

#include <maya/MPxCommand.h>
#include <maya/MGlobal.h>
#include <maya/MFnPlugin.h>
#include <maya/MSelectionList.h>
#include <maya/MItSelectionList.h>
#include <maya/MDagPath.h>
#include <maya/MFnNurbsCurve.h>
#include <maya/MPoint.h>
#include <maya/MArgList.h>
#include <maya/MSyntax.h>
#include <maya/MArgDatabase.h>
#include <maya/MDGModifier.h>

class Posts1Cmd : public MPxCommand
{
public:
    virtual MStatus doIt (const MArgList & );
    virtual MStatus undoIt();
    virtual MStatus redoIt();
    virtual bool isUndoable() const { return true; }

    static void *creator() { return new Posts1Cmd; }
    static MSyntax newSyntax();

private:
    MDGModifier dgMod;
};

const char *numberFlag = "-n", *numberLongFlag = "-number";
const char *radiusFlag = "-r", *radiusLongFlag = "-radius";
const char *heightFlag = "-he", *heightLongFlag = "-height";
const char *helpFlag = "-h", *helpLongFlag = "-help";

MSyntax Posts1Cmd::newSyntax()
{
    MSyntax syntax;

    syntax.addFlag( numberFlag, numberLongFlag, MSyntax::kLong );
    syntax.addFlag( radiusFlag, radiusLongFlag, MSyntax::kDouble );
    syntax.addFlag( heightFlag, heightLongFlag, MSyntax::kDouble );
    syntax.addFlag( helpFlag, helpLongFlag );

    return syntax;
}

const char *helpText = 
"\nThe posts4 command is used to create a series of posts(cylinders) along all the selected curves."
"\nIt is possible to set the number of posts, as well as their width and height."
"\nFor further details consult the help documentation."
"\nFor quick help use: help posts4";

MStatus Posts1Cmd::doIt (const MArgList &args )
{
     int nPosts = 5;
     double radius = 0.5;
     double height = 5.0;

     MArgDatabase argData( syntax(), args );

     if ( argData.isFlagSet( numberFlag ) )
         argData.getFlagArgument( numberFlag, 0, nPosts );

     if ( argData.isFlagSet( radiusFlag ) )
         argData.getFlagArgument( radiusFlag, 0, radius );

     if ( argData.isFlagSet( heightFlag ) )
         argData.getFlagArgument( heightFlag, 0, height );

     if ( argData.isFlagSet( helpFlag ) )
        {
            setResult (helpText);
            return MS::kSuccess;
        }

    MSelectionList selection;
	MGlobal::getActiveSelectionList( selection );

    MDagPath dagPath;
    MFnNurbsCurve curveFn;
    double heightRatio = height / radius;

    MItSelectionList iter(selection, MFn::kNurbsCurve);
    for ( ; !iter.isDone(); iter.next())
    {
    iter.getDagPath( dagPath );
    curveFn.setObject( dagPath );

    double tStart, tEnd;
    curveFn.getKnotDomain(tStart, tEnd);

    MPoint pt;
    int i;
    double t;
    double tIncr = (tEnd - tStart)/(nPosts - 1);
    for (i = 0, t = tStart; i < nPosts; i++, t += tIncr)
        {
        curveFn.getPointAtParam(t, pt, MSpace::kWorld);
        pt.y += 0.5*height;

        dgMod.commandToExecute(MString("cylinder -pivot") + " " + pt.x + " " + pt.y + " " + pt.z 
            + "-radius" + " " + radius + " " + "-axis 0 1 0 -heightRatio" + " " + heightRatio);

        }
    }
    return redoIt();

}

MStatus Posts1Cmd::undoIt()
{
    return dgMod.undoIt();
}

MStatus Posts1Cmd::redoIt()
{
    return dgMod.doIt();
}

MStatus initializePlugin(MObject obj)
{
    MFnPlugin plugin(obj, "Chuck Bruno", "1.0");
    MStatus stat;
    stat = plugin.registerCommand("posts1", Posts1Cmd::creator, Posts1Cmd::newSyntax);
    if (!stat)
        stat.perror("registerCommand failded");
    return stat;
}

MStatus uninitializePlugin(MObject obj)
{
    MFnPlugin plugin(obj);
    MStatus stat;
    stat = plugin.deregisterCommand("posts1");
    if (!stat)
        stat.perror("deregisterCommand failed");
    return stat;

}
最后执行结果为:



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值