It's possibile to retrive the color stop index from Color Ramp to a python script plugin?

Hello,

i’m trying to retrive the active color stop data from the color ramp to a python script plugin.

For now in the official documentation of Blender the color ramp seems to not have the color stop data for be retrieve:

There is any other un-official way to retrieve it ? Maybe with an external plugin ?

Thanks
T

It’s in the node:

>>> n = bpy.context.object.data.materials[0].node_tree.nodes.active

>>> n.color_ramp.elements[1].color[:]
(1.0, 0.0, 0.0, 1.0)

The active one does not seem to be exposed in the API though:

Use Tab for auto-complete in Python Console to figure stuff like this out.

It’s in the tooltip as well:

Make sure to enable Python Tooltips in the preferences, not sure if they are on by default:

The Outliner in Data API mode is also useful:

Thanks…So basically it’s internal in the C code of Blender , right?

Seems to be. There aren’t any more places it could be in the Python API and it’s not there. So no add-on or external plugin is possible.

So basically that’s not the case - you can definitely retrieve position and color of the color ramp elements or stops, just not the active one. …not sure if you did actually mean just the active one there. Maybe my explanations don’t make sense sicne you already knew that maybe. :laughing:

Ok thanks! So my other question is :
Can i change the active color ramp with a single click on a custom color presets?
Thanks

I am not sure I understand the question. Yeah. Of course. What are we talking about here?

import bpy

ramp_node = bpy.context.object.data.materials[0].node_tree.nodes.active
color_ramp = ramp_node.color_ramp

while len(color_ramp.elements)>1:
    color_ramp.elements.remove(color_ramp.elements[0]) 
    
elements = [[0, (0,0,0,1)], [.333333, (1,0,0,1)], [.66666, (0,1,0,1)],[1, (0,0,1,1)]]

color_ramp.elements[0].position = elements[0][0]
color_ramp.elements[0].color = elements[0][1]
for e in elements[1:]:
    new = color_ramp.elements.new(e[0])
    new.color = e[1]

I’ll explain better with image :slight_smile:


I have a custom colour palette created with a python plugin …
What i would like to do is press a color on the colour palette and set immediately the color in the active stop index.
I know that i can do it … but for example if a change the active stop index then from what i understand from your reply is that i can’t ask the actual stop index from the color ramp object (right?) to update the active stop index in my color palette plugin part.

We would need to look at the code. :laughing: Not the image.

Oh, wait, you are talking about the active one again, so, no, that one is impossible as we have discovered. If those are standard Blender input fields in the whatever add-on you have, you could just drag them:

drag

Copy paste works too. Ctrl + C while hovering over a color input in the UI will copy it and if you hit Ctrl + V while hovering over another will paste it.

Or you could have presets specifically for the whole color ramp, then you can make a button that just replaces the whole color ramp with that preset, but not the active stop, because the active stop is not exposed to Python API.

In any case, you could write a Python add-on that has some palette of colors as standard color fields in the UI so you can make it that they are dragable, it depends on how the palette is coded.

You could also code a custom panel that mimics color ramp inputs and then construct the color ramp there… But it would just be colors and positions, maybe it would be possible to show preview somehow, but I think this bit is not something you can use in UI:
image
It’s part of a template probably, but I haven’t checked to know for sure.