小技巧, 如何保持DG节点之间的连接不被破坏或者不被创建

如果你有节点A 和 B,并且他们这样相连 A.output -> B.input ,如果你希望保持这个连接不被删除,那么你可以考虑重载 MPxNode 中的两个虚函数 legalConnection 和 legalDisconnection 来实现这个目的。

重载这两个虚函数可以用来确定指定的接口之间的 连接/删除连接 是否被允许,当这个节点上的连接被建立或者删除的时候,Maya都会先调用这个虚函数来确定这个操作是否合法。让我们来看一个例子:

如果我希望 NodeForTest.output 不能连接到任何节点 radius 属性上,那我就可以通过设置 isLegal 标志来阻止这个连接,

MStatus NodeForTest::legalConnection(const MPlug & plug, const MPlug & otherPlug, bool asSrc, bool & isLegal ) const

{

    MFnDependencyNode node( otherPlug.node() );

    MPlug radius = node.findPlug("radius");

    if( plug == output && otherPlug == radius )

    {

        isLegal = false;

        return MS::kSuccess;

    }

    return MS::kNotImplemented;

}

当用户试图调用Mel命令 (connectAttr -f NodeForTest1.output polyCylinder1.radius; )来创建连接的时候,Maya就会报错,并且提示:

// Error: line 1: Connection not made: 'NodeForTest1.output' -> 'polyCylinder1.radius'.  Source node will not allow the connection. //

// Error: line 1: The attribute 'NodeForTest1.output' cannot be connected to 'polyCylinder1.radius'. // 

同样的,如果我们设置了删除 NodeForTest.output 到任何节点的height属性为非法的话,那么我们就能保持这个连接不被破坏,

MStatus NodeForTest::legalDisconnection(   const MPlug & plug, const MPlug & otherPlug,  bool asSrc, bool & isLegal ) const

{

    MFnDependencyNode node( otherPlug.node() );

    MPlug height = node.findPlug("height");

    if( plug == output && otherPlug == height )

    {

        isLegal = false;

        return MS::kSuccess;

    }

    return MS::kNotImplemented;

}

当用户试图通过disconnectAttr NodeForTest1.output polyCylinder1.height;来删除该连接时,Maya会阻止该行为,并且提示:

// Result: Can not disconnect. //

# Error: line 1: Connection not made: 'NodeForTest1.output' -> 'polyCylinder1.height'.  Source node will not allow the disconnection. #

原文地址:https://www.cnblogs.com/johnonsoftware/p/3173317.html