How to add a new stop to the color ramp in python?
2 Answers
$\begingroup$
$\endgroup$
4
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
Test_fire_color_ramp.color_ramp.elements.remove(Test_fire_color_ramp.color_ramp.elements[0])
# Adding new color stop at location 0.100
Test_fire_color_ramp.color_ramp.elements.new(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.new(0.200)
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)
-
6$\begingroup$ According to pep8 variable names should be
lower_case
... $\endgroup$ Commented Aug 3, 2020 at 9:07 -
$\begingroup$ @brockmann I agree with you $\endgroup$ Commented Aug 3, 2020 at 9:13
-
1$\begingroup$ camelCaseRocks! Underscores are really hard to type. $\endgroup$ Commented Aug 3, 2020 at 14:53
-
2$\begingroup$ Probably but pep8 is not about taste @RonJensen $\endgroup$ Commented Aug 3, 2020 at 18:22
$\begingroup$
$\endgroup$
import bpy
from bpy import data as D
# Get a reference to the colorRamp node
cr=D.materials['Material'].node_tree.nodes['ColorRamp'].color_ramp
# enumarate the elements to the console
for e in cr.elements:
print(e.position)
# set the left most color to green
cr.elements[0].color=(0,1,0,1)
# add a new ramp in the middle, position = 0.5
cr.elements.new(position = 0.5)
# set the middle color to red
cr.elements[1].color=(1,0,0,1)
# set the right color to blue
cr.elements[2].color=(0,0,1,1)