Maya API: MRampAttribute from node (string name)


#1

Say you pass in the name of a ramp node (from the GUI) to your command/plug-in.
How do you create an MRampAttribute from this? I want to read the ramp node in OpenMaya.

This breaks and gives me a kFailure. Unexpected Internal Failure. -error:

import maya.OpenMaya as om
import pymel.core as pm

my_ramp = pm.createNode("ramp")  # Just a dummy test. Node created with MEL, Maya.cmds or PyMEL.

node_selection_list = om.MSelectionList()
node_selection_list.add("ramp1")  # Name of dummy ramp node
node_obj = om.MObject()
node_selection_list.getDependNode(0, node_obj)
ramp_attribute = om.MObject()
om.MRampAttribute(node_obj, ramp_attribute)  # FAILS - kFailure. Unexpected Internal Failure.

Also, this I found on Stack Overflow:
https://stackoverflow.com/questions/44551024/what-is-the-proper-way-to-work-with-ramp-nodes-using-the-maya-2016-sp6-python-2

So it seems like you have to get the dependency node of the ramp, then use findPlug(“colorEntryList”, False) - to get the colorEntryList… which happens to NOT be a MRampAttribute.

If I understand it correctly then you have to dive into that compound attribute and get the indices, values of the positions and the colors - and then create a new MRampAttribute from scratch and use setColorAtIndex() and setPositionAtIndex()? (because how else are you going to get access to cool functions of MRampAttribute???)

This sounds very messy to me, especially considering that you don’t have the interpolation types in colorEntryList. They seem to be inside the plug.interpolation (which I haven’t even been able to iterate through).

Is there an easier way of doing this? The documentation has very poor examples of this unfortunately.

TL:DR
I’m passing in the name of a Ramp node into my plug-in.
From that I want to get an MRampAttribute so that I can read color, curve and interpolation values. All the cool things of a ramp. How to do this?


#2

So in the end I did this:
I create a MFnVolumeLight (https://cpp.hotexamples.com/de/examples/-/MIntArray/clear/cpp-mintarray-clear-method-examples.html) and I use that MRampAttribute as my container. It’s the only working example I’ve found in the Maya API where the MRampAttribute doesn’t give you a ton of problems. This is step 1.

Step 2 is that I then read the data of my ramp node using the technique on Stack Overflow. I append this data to the existing MRampAttribute and then I delete entry(0) and entry(1) in it. After I’m done with the ramp I delete the MFnVolumeLight object.