MRampAttributes without creating an AETemplate file
Monday, May 9, 2011 at 10:04PM I wanted to see if there was a way to initialize MRampAttributes without the need for the AETemplateFile.
I ended up using this solution.
First I define my AETemplate inside my plugin by calling MGlobal::executeCommand(); I pass it a couple of commands to intialize my ramp.
This function gets run inside myNode::initialize() function, after I've defined my ramps.
MStatus myNode::initializeAETemplate()
{
MString attributeTemplate;
attributeTemplate += "global proc AEmyNodeTemplate( string $nodeName )"
attributeTemplate += "{";
attributeTemplate += "editorTemplate -beginScrollLayout;";
attributeTemplate += "editorTemplate -beginLayout \"ramps\" -collapse 0;";
attributeTemplate += "AEaddRampControl \"myRamp\";";
attributeTemplate += "editorTemplate -endLayout;";
attributeTemplate += "AEdependNodeTemplate $nodeName;";
attributeTemplate += "editorTemplate -addExtraControls;";
attributeTemplate += "editorTemplate -endScrollLayout;";
attributeTemplate += "}";
MGlobal::executeCommand(attributeTemplate);
return MS::kSuccess;
}
Nicely written my string looks like this:
global proc AEmyNodeTemplate( string $nodeName );
{
editorTemplate -beginScrollLayout;
editorTemplate -beginLayout "ramps" -collapse 0;
AEaddRampControl "myRamp";
editorTemplate -endLayout;
AEdependNodeTemplate $nodeName;
editorTemplate -addExtraControls;
editorTemplate -endScrollLayout;
}
The most important line is this one.
AEaddRampControl "myRamp";
In mel this makes the AEtemplate recognize my custom ramp attribute. You can add this line for as many ramps as you have on your node. Without this defined in an AETemplate, Maya does not draw your ramp attribute properly in the Attribute Editor.
Also make sure the the procedure's name matches the node name. If my node name was registered as nodeName then my procedure name would be: AEnodeNameTemplate
This procedure runs every time you load the node in maya's attribute editor. So we can't initialize the ramp's values here.
We can do this in our node's postConstructor. Here I define a color ramp's default values.
This sort of method can also be applied to curve ramps
void myNode::postConstructor()
{
MColor defaultColor(0.5,0.5,0.5);
MPlug myRampPlug(myNode::thisMObject(), myRamp);
MColor color1(1.0, 0.0, 0.0);
MColor color2(1.0, 1.0, 0.0);
addColorRampSlot(myRampPlug, 0, 0.0, defaultColor, 2);
addColorRampSlot(myRampPlug, 1, 0.5, color1, 2);
addColorRampSlot(myRampPlug, 2, 1.0, color2, 2);
}
And finally, here is my function addColorRampSlot:
void addColorRampSlot(MPlug rampAttributePlug, int index, float position, MColor color, int interpType)
{
MPlug entryPlug = rampAttributePlug.elementByLogicalIndex(index);
MPlug posPlug = entryPlug.child(0);
posPlug.setDouble(position);
MPlug valuePlug = entryPlug.child(1);
valuePlug.child(0).setFloat(color.r);
valuePlug.child(1).setFloat(color.g);
valuePlug.child(2).setFloat(color.b);
MPlug interpPlug = entryPlug.child(2);
interpPlug.setInt(interpType);
}
By going through the MPlug documentation you can figure out exactly what all this means.
AETemplate,
MRampAttribute,
c++,
maya api,
postConstructor