Maya API: MRampAttribute from string?

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

# Just a dummy test
my_ramp = pm.createNode("ramp")  # 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.

Found this on stackoverflow:

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.

So in the end I did this:
I create a MFnVolumeLight ( 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.