How to add a new stop to the color ramp in python?

enter image description here


I think you are creating a color ramp in a node group as shown in the screenshot so i am giving you an example of creating inside of a node group. I hope you know how to add a color_ramp but still I am giving a complete example for creating a color ramp to add a new stop.

# Creating Color ramp
Test_fire_color_ramp = group.nodes.new(type="ShaderNodeValToRGB")

# assigning location
Test_fire_color_ramp.location = (0,0)

# Removing the First Element this is not necessary but done to show how to remove color stops

# Adding new color stop at location 0.100

# Setting the color for the stop that we recently created
Test_fire_color_ramp.color_ramp.elements[0].color = (0,0,0,1)

#creating the second stop the same way
Test_fire_color_ramp.color_ramp.elements[1].color = (0.500,0.400,0.016,1)

# Assigning position and color to already present stop
Test_fire_color_ramp.color_ramp.elements[2].position = (0.995)
Test_fire_color_ramp.color_ramp.elements[2].color = (0,0,0,1)
  • 4
    $\begingroup$ According to pep8 variable names should be lower_case... $\endgroup$
    – brockmann
    Aug 3 '20 at 9:07
  • $\begingroup$ @brockmann I agree with you $\endgroup$ Aug 3 '20 at 9:13
  • 1
    $\begingroup$ camelCaseRocks! Underscores are really hard to type. $\endgroup$
    – Ron Jensen
    Aug 3 '20 at 14:53
  • 2
    $\begingroup$ Probably but pep8 is not about taste @RonJensen $\endgroup$
    – brockmann
    Aug 3 '20 at 18:22
import bpy
from bpy import data as D

# Get a reference to the colorRamp node

# enumarate the elements to the console
for e in cr.elements:

# set the left most color to green

# add a new ramp in the middle, position = 0.5
cr.elements.new(position = 0.5)

# set the middle color to red

# set the right color to blue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.