Chad Vernon
  • Home
  • Reel/Resume
  • Work
  • Resources
  • About
  • Contact
sideBar

Search

Categories

  • CG
  • cvxporter
  • Maya
    • API
    • Plug-ins
  • Personal

Archives

  • May 2012
  • March 2012
  • September 2011
  • June 2011
  • March 2011
  • December 2010
  • November 2010
  • September 2010
  • August 2010
  • July 2010
  • May 2010
  • April 2010
  • March 2010
  • December 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007

Rss

  • Main Entries RSS
  • Comments RSS
Home » Resources » Maya API Programming » MRampAttribute

MRampAttribute

MRampAttribute allows you to create an adjustable curve or color attribute where users can insert and adjust the interpolation of points along the ramp.

To create ramp attributes, we call the convenient classes contained in MRampAttribute:

MStatus RampAttributeDeformer::initialize()
{
    // Create the curve ramp attribute
    aCurveRamp = MRampAttribute::createCurveRamp( "curveRamp", "cur" );
    addAttribute( aCurveRamp );
    attributeAffects( aCurveRamp, outputGeom );

    // Create the color ramp attribute
    aColorRamp = MRampAttribute::createColorRamp( "colorRamp", "cor" );
    addAttribute( aColorRamp );
    attributeAffects( aColorRamp, outputGeom );

    return MS::kSuccess;
}

To access the ramp attribute values inside a node or deformer:

MStatus RampAttributeDeformer::deform( MDataBlock& data,
                          MItGeometry& itGeo,
                          const MMatrix &localToWorldMatrix,
                          unsigned int geomIndex )
{
    MStatus status;

    // Get the ramp attributes
    MObject oThis = thisMObject();
    MRampAttribute curveAttribute( oThis, aCurveRamp, &status );
    CHECK_MSTATUS_AND_RETURN_IT( status );
    MRampAttribute colorAttribute( oThis, aColorRamp, &status );
    CHECK_MSTATUS_AND_RETURN_IT( status );

    float rampPosition = 0.25f, curveRampValue;
    MColor color;

    // Get the corresponding value on the curve ramp attribute
    curveAttribute.getValueAtPosition( rampPosition, curveRampValue, &status );
    CHECK_MSTATUS_AND_RETURN_IT( status );

    // Get the corresponding value on the color ramp attribute
    colorAttribute.getColorAtPosition( rampPosition, color, &status );
    CHECK_MSTATUS_AND_RETURN_IT( status );

    // Do your calculation with the values

    return MS::kSuccess;
}

You will also need to make sure the attribute is set correctly in your attribute editor template for the node:

global proc AErampDeformerTemplate( string $nodeName )
{
    editorTemplate -beginScrollLayout;

        editorTemplate -beginLayout "Ramp Deformer Attributes" -collapse 0;
            AEaddRampControl( $nodeName + ".curveRamp" );
            AEaddRampControl( $nodeName + ".colorRamp" );
        editorTemplate -endLayout;

    editorTemplate -addExtraControls;
    editorTemplate -endScrollLayout;
}

4 Responses to “MRampAttribute”

Subscribes to this topic Comment RSS or TrackBack URL

Hey, thanks for posting this. I’m doing research on wire deformers and remembered seeing someone else use a curve like this for shaping, which is exactly what I’d like to do. :)

kattkieru on April 1st, 2011 at 7:20 am

Excellent stuff, gave me a nice shortcut around doing too much thinking today. Thanks Chad.

I would add this little snippet of handy info :

When using api ramps with nodes, it’s a good plan to initialize the ramps with single entries to avoid errors when using the Attribute Editor.

Oddly enough this accomplished via the AEtemplate, as in the following code tweak to your AEtemplate ( the extra code runs once & not after a user has added their own ramp entries [seemingly]):

global proc AErampDeformerTemplate( string $nodeName )
{
editorTemplate -beginScrollLayout;

editorTemplate -beginLayout “Ramp Deformer Attributes” -collapse 0;
AEaddRampControl( $nodeName + “.curveRamp” );
AEaddRampControl( $nodeName + “.colorRamp” );
editorTemplate -endLayout;

editorTemplate -addExtraControls;
editorTemplate -endScrollLayout;

// — With Ramps, we have to add some default values
// to avoid an error when displaying them in the Attribute Editor :

setAttr ($nodeName + “.curveRamp[0].curveRamp_FloatValue”) 1.0;
setAttr ($nodeName + “.curveRamp[0].curveRamp_Position”) 0.0;
setAttr ($nodeName + “.curveRamp[0].curveRamp_Interp”) 1;

setAttr ($nodeName + “.colorRamp[0].colorRamp_Color”) -type double3 1 1 1;
setAttr ($nodeName + “.colorRamp[0].colorRamp_Position”) 0.0;
setAttr ($nodeName + “.colorRamp[0].colorRamp_Interp”) 1;
}

NTaylor on April 9th, 2011 at 11:40 am

Nice example, thanks!

NTaylor, you should also check if some value already presents in the ramp (smth like `getAttr -silent -size ($nodeName+”.”+”curveRamp”` == 0) or it will override stored value.

Andrey on July 8th, 2011 at 9:11 pm

Andrey, You’re entirely correct about my suggestion leading to the overriding of values. Bad NTaylor. Bad.

Now that I finally have a chance to test this all out properly, I’ve run into the problem you’ve outlined. My values are indeed being overridden.

I’ve tried tinkering with your code, but I always get a size of 1 when running the code you suggested : My ramps *always* have an entry [0] just after createNode has been executed. This is also regardless of the value stored at index [0], so checking against a presumed default value is tricky [impossible].

Which means that I cant seem to find a MEL/AE solution for safely setting default ramp values :

I can wrap the createNode command within a MEL function that sets those default values, but it’d be nicer to be able to simply default initial ramp values so that they’re ‘correct’ if a user makes the node via createNode as well.

With that in mind I’ve tinkered a fair bit and found an ideal solution (via the API) : set initial ramp values from within the ::postConstructor() and set NO values in the AETemplate at all. Ideal.

===Post Constructor===

void YourNode::postConstructor()
{
MStatus status;

postConstructor_initialise_ramp_curve( YourNode::thisMObject(), aYourRamp, 0, 0.0f, 1.0f, 1 );

}

===postConstructor_initialise_ramp_curve===

MStatus postConstructor_initialise_ramp_curve( MObject& parentNode, MObject& rampObj, int index, float position, float value, int interpolation)
{
MStatus status;

MPlug rampPlug( parentNode, rampObj );

MPlug elementPlug = rampPlug.elementByLogicalIndex( index, &status );

MPlug positionPlug = elementPlug.child(0, &status);
status = positionPlug.setFloat(position);

MPlug valuePlug = elementPlug.child(1);
status = valuePlug.setFloat(value);

MPlug interpPlug = elementPlug.child(2);
interpPlug.setInt(interpolation);

return MS::kSuccess;
}

===AE TEMPLATE===
global proc AErampDeformerTemplate( string $nodeName )
{
editorTemplate -beginScrollLayout;
editorTemplate -beginLayout “Ramp Deformer Attributes” -collapse 0;
AEaddRampControl( $nodeName + “.curveRamp” );
AEaddRampControl( $nodeName + “.colorRamp” );
editorTemplate -endLayout;
editorTemplate -addExtraControls;
editorTemplate -endScrollLayout;
}

____________________________________
(Apologies for the formatting)

The above use of the PostConstructor works great. All Ramps can be built with default values and no errors are reported in maya with the original, simpler, AETemplate.

You can call the postConstructor_initialise_ramp_curve() more than once to set more than 1 default entry/position/value for a ramp

This can be easily extended to handle color ramps as well :

MStatus postConstructor_initialise_color_curve( MObject& parentNode, MObject& rampObj, int index, float position, MColor value, int interpolation )

{
MStatus status;

MPlug rampPlug( parentNode, rampObj );

MPlug elementPlug = rampPlug.elementByLogicalIndex( index, &status );

MPlug positionPlug = elementPlug.child(0, &status);
status = positionPlug.setFloat(position);

MPlug valuePlug = elementPlug.child(1);
status = valuePlug.child(0).setFloat(value.r);
status = valuePlug.child(1).setFloat(value.g);
status = valuePlug.child(2).setFloat(value.b);

MPlug interpPlug = elementPlug.child(2);
interpPlug.setInt(interpolation);

return MS::kSuccess;
}

________________________________________
I’ve left out a tonne of MStatus error checkling/trapping in the above code to reduce it a bit.

NTaylor on January 7th, 2012 at 4:54 am

Leave A Reply

Allowed tag : <blockquote>, <p>, <code>, <em>, <small>, <ul>, <li>, <ol>, <a href=>..

 Username

 Email Address

 Website

Sticky: Always double check your comment before posting Please Note: Comment Moderation Maybe Active So There Is No Need To Resubmit Your Comments
Home » Resources » Maya API Programming » MRampAttribute

© 2011 Chad Vernon