Maya: duplicate AnimCurve in the API/C++

2019-08-16 08:00发布

问题:

Is there any easy way to copy any type of AnimCurve? I see using MFnAnimCurve it may become quite bloated.

P.S.: using Maya 2013 right now.

回答1:

All right - I knew nobody cares as this problem is quite needless for 99.9999% of all c0ders.

The answer is NO.

This is the API/C++ solution I chose:

bool copyAnimCurve(MFnDependencyNode &srcDN, MFnDependencyNode &destDN, MObject &destNode)
{
    MFnAnimCurve s( srcDN.object() ), d;
    MAngle a; double w;

    destNode = d.create( s.animCurveType() );
    destDN.setObject( destNode );
    if ( !destNode.isNull() )
    {
        bool unitless( s.isUnitlessInput() ), weighted( s.isWeighted() );
        d.setName( newName( s.name() ) );
        d.setIsWeighted( weighted );
        d.setPreInfinityType( s.preInfinityType() );
        d.setPostInfinityType( s.postInfinityType() );
        // copy keys...
        MFnAnimCurve::TangentType tt[2];
        for ( uint i = 0; i < s.numKeys(); ++i )
        {
            tt[ true ] = s.inTangentType( i );
            tt[ false ] = s.outTangentType( i );
            if ( unitless )
                d.addKey( s.unitlessInput( i ), s.value( i ), tt[ true ], tt[ false ] );
            else
                d.addKey( s.time( i ), s.value( i ), tt[ true ], tt[ false ] );
            // tangents and weights are locked by default - so let's unlock them before setting any values!
            d.setTangentsLocked( i, false );
            d.setWeightsLocked( i, false );
            for ( int j=1; j>=0; --j )  if ( tt[ bool( j ) ] == MFnAnimCurve::TangentType::kTangentFixed )
            {
                // tangents are internally stored using angle and weights - so we'll only use those.
                s.getTangent( i, a, w, bool( j ) );
                d.setTangent( i, a, w, bool( j ) );
            }
            d.setWeightsLocked( i, s.weightsLocked( i ) );
            d.setTangentsLocked( i, s.tangentsLocked( i ) );
            d.setIsBreakdown( i, s.isBreakdown( i ) );
        }
        return true;
    }
    cerr << "\nERROR CREATING AnimCurve COPY OF " << srcDN.name() << endl;
    return false;
}

P.S.: you'll need to implement your own MString newName(const MString& oldName).



标签: maya-api