Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active August 29, 2015 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/a7d5c3b775024eb2d76b to your computer and use it in GitHub Desktop.
Save zeffii/a7d5c3b775024eb2d76b to your computer and use it in GitHub Desktop.
# 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