Search code examples
pythonmayamaya-api

What is the proper way to work with ramp nodes using the Maya 2016 SP6 Python 2.0 API?


I am new to programming using the Python 2.0 API (have used the Python Maya.cmds a fair bit). As I understand it, I should be able to manipulate a ramp node referenced via an MObject using the corresponding ramp node function set. Unfortunately, I have not been able to find any examples on how to properly do this, or even any real clear information on what the function set is called. In the API documentation, there is an "MRampAttribute" class, and the description says that it is in fact the "Functionset for creating and working with ramp attributes." Unfortunately, I haven't had any luck getting it to work...

I did find another possibly relevant piece of information here, which is a short list of the compatible function sets for a ramp node, and included the following: kBase, kNamedObject, kDependencyNode, kTexture2d, and kRamp. Unfortunately, I have not really been able to find documentation on these function sets, aside from them appearing in a list of constants under the MFn class.

Ideally, it would be great to have a very simple example that shows the following:

  1. Properly store a ramp node (ramp texture node?) that already exists in the scene in an MObject
  2. Create an instance of the proper ramp node function set and have it set up to act on the ramp node reference
  3. Use the function set to do something simple, like changing or adding a color entry

I guess one last thing to note is that I am trying to work with color ramps (gradient ramps), as opposed to the curve(?) ramp, since it seems they are closely related.

Thank you!

Update: It's looking more and more like the issue may be that the Python 2.0 API is very incomplete, particularly since this straight Maya 2016 SP6 with no extensions. Was looking into some other issues and it appears basic things like MItDag were added in extension 2, and are not available in the base version...

TL;DR - I cannot seem to find the function set that is supposed to operate on ramp nodes in Maya 2016 SP6 using the Python 2.0 API. A simple working example code snippet would be greatly appreciated as a place to get started.


Solution

  • The Ramp node isn't particularly special except that the .colorEntryList property is a compound array attribute; it doesn't get a special function set. And, confusingly, .colorEntryList is not an MRampAttribute, it's just a regular indexed compound attribute.

     test = cmds.createNode('ramp')
    node = om.MGlobal.getSelectionListByName(test).getDependNode(0)
    depfn = om.MFnDependencyNode(node)
    compound_plug = depfn.findPlug("colorEntryList", False)
    for idx in range(compound_plug.numElements()):
        index_plug = compound_plug.elementByPhysicalIndex(idx)
        pos_handle = index_plug.child(0).asMDataHandle() 
        color_handle = index_plug.child(1).asMDataHandle()    
    
        print idx, pos_handle.asFloat(), ":",  color_handle.asFloat3()
    

    You'll notice that the elements are not in the order you'd expect, youll need to check the position values in pos_handle to find the one you want. The other attributes on the node are just generic properties.

    If you're not doing this inside an MPxCommand, you probably just want to do this with maya.cmds, it's less of a hassle and the speed difference is not likely to matter if this is a tool operation.