1
$\begingroup$

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

enter image description here

$\endgroup$
2
$\begingroup$

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)
| improve this answer | |
$\endgroup$
  • 4
    $\begingroup$ According to pep8 variable names should be lower_case... $\endgroup$ – brockmann Aug 3 at 9:07
  • $\begingroup$ @brockmann I agree with you $\endgroup$ – Rakesh choudhary Aug 3 at 9:13
  • 1
    $\begingroup$ camelCaseRocks! Underscores are really hard to type. $\endgroup$ – Ron Jensen Aug 3 at 14:53
  • 1
    $\begingroup$ Probably but pep8 is not about taste @RonJensen $\endgroup$ – brockmann Aug 3 at 18:22
4
$\begingroup$
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)
| improve this answer | |
$\endgroup$

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.