Safe way to access Ramp attribute (MRampAttribute) from compute()

Safe way to access Ramp attribute (MRampAttribute) from compute()

morinnicolas8944
Advocate Advocate
1,153 Views
4 Replies
Message 1 of 5

Safe way to access Ramp attribute (MRampAttribute) from compute()

morinnicolas8944
Advocate
Advocate

Hello all.

 

I am basically making a ramp shader and trying to find the proper way to get values from a ramp attribute in the compute() function for a node.

I know it can be done with

 

MStatus MyNode::compute(const MPlug& plug, MDataBlock& data) {
MObject oThis = thisMObject(); MRampAttribute rampAttribute(oThis, aRamp); // aRamp is our ramp MObject.

int position = computeThePosition();

MColor color;
rampAttribute.getColorAtPosition(position, color);
...
}

but is it safe to do this in compute? The Maya documentation mentions in a few places that, in compute, we should only use attributes in the MDataBlock to get input data, but I can't find a way to get a ramp attribute from the MDataBlock. I couldn't find any official code samples using MRampAttribute in compute() either.

 

Thanks!

0 Likes
Accepted solutions (1)
1,154 Views
4 Replies
Replies (4)
Message 2 of 5

cheng_xi_li
Autodesk Support
Autodesk Support
Accepted solution

Hi morinnicolas8944,

 

It's not safe to access the attribute directly during the compute. It may cause problem when using EM or during a playback.

 

It is a compound array with three children of each item.

 

They are named with your ramp attribute's name with a suffix,

 

p/_Position: kFloat

cv/_Color: Color

i/_Interp: enum(0: none, 1: linear, 2:smooth, 3:spline)

 

You can get the data of each position with input handle and calculate the color at position yourself.

 

Yours,

Li

0 Likes
Message 3 of 5

morinnicolas8944
Advocate
Advocate

Thanks Li


@cheng_xi_li wrote:

You can get the data of each position with input handle and calculate the color at position yourself.


That's what I was afraid of.

I was hoping for some way to use the facilities of MRampAttribute to get the color. It kind of defeats the purpose of a specialized attribute if we can't use its sample functions ...

 

I tried looking for some way to get Maya to compute the ramp values for me without using MRampAttribute but couldn't find any. If you know of one, I'd be very happy to hear bout it. Or maybe I should start a new post with that as a question?

0 Likes
Message 4 of 5

cheng_xi_li
Autodesk Support
Autodesk Support

Hi morinnicolas8944,

 

I am afraid there is no Maya API for you right now.

 

But I think you could use SeExpr from Walt Disney as a reference. They have created a control to mimic the behavior of Maya color ramp. It's open source, you can find it here.

 

Yours,

Li

Message 5 of 5

morinnicolas8944
Advocate
Advocate

Thanks again! I'll have a look at it.

0 Likes