Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# assumes the values of the first indices add up to 100.
import bpy
import random
mats = bpy.data.materials
nodes = mats['Material'].node_tree.nodes
elements = nodes['ColorRamp'].color_ramp.elements
RAN = random.random
get_random_color = lambda: (RAN(), RAN(), RAN(), 1.0)
procents_and_colors = [
[20, get_random_color()], # or fill these with prechosen colours
[30, get_random_color()],
[50, get_random_color()],
]
# this sections adds or removes elements if you update your
# procents_and_colors list with more / fewer elements
diff = len(elements) - len(procents_and_colors)
if diff > 0:
for i in range(abs(diff)):
elements.remove(elements[-1])
elif diff < 0:
for i in range(abs(diff)):
elements.new(position=0.0)
position = 0
for idx, section in enumerate(procents_and_colors):
elements[idx].color = section[1]
elements[idx].position = position
position += (section[0] / 100.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.