# How to create and change the color of a Node based material with a script

I want to do an animation of a blender object which changes the color over time, in dependence of the temperature. Therefore zeffi already wrote a script which transforms different colormaps into blender. (Set color of Blender object based on external data (e. g. temperature) using a colormap)

Now I would like to create a node based material with a script, set the colormap to jet and when the temperature changes, I would like to change also the color.

Can someone post a little piece of script, how I can achieve this?

Thanks

• do you have some kind of text file with the temperature over time? – zeffii Oct 29 '15 at 17:08
• related: blender.stackexchange.com/q/23436/47 , blender.stackexchange.com/a/5445/47 Scripting a node material is pretty well documented. and blender.stackexchange.com/questions/23882/… – zeffii Oct 29 '15 at 17:15
• This question is really 3 or 4 questions rolled into one, we encourage you to split problems up into separate concepts so they can be asked and answered separately. The scripting of materials, and material animation are definitely easy to find using the this site's search feature. The only part of this question that isn't readily found relates to the add-on you mention (which is mine so I feel some responsibility for answering questions about it: see below) . – zeffii Oct 30 '15 at 8:07

I can answer one of your questions but you will need to download and install the add-on again if you haven't yet. The add-on now lets you access functionality via the script shown below.

• This assumes a recent version of the octave_gradients add-on is available locally.

code for using the add-on inside a script:

import bpy

# add material, or reuse existing one
mymat = bpy.data.materials.get('lazy_cube')
if not mymat:
mymat = bpy.data.materials.new('lazy_cube')
mymat.use_nodes = True

nodes = mymat.node_tree.nodes

if not 'ColorRamp' in nodes:
ColorRamp = nodes['ColorRamp']

# reuse the Octave Gradient maker code, when calling from this context,
# it will add a function called 'external_octave' to the driver_namespace
# this function doesn't care what context you are in, all it wants
# is a reference to the ColorRamp node, and a number between 0..13
external_octave = bpy.app.driver_namespace['external_octave']

# force ramp theme
external_octave(ColorRamp, 12)  # jet=12

# hook the ColorRamp up to the DiffuseBSDF node.
DiffuseBSDF = nodes.get("Diffuse BSDF")

• Thanks for your quick answer! When I try running your script, I get an Error: AttributeError: 'NoneType' object has no attribute 'inputs' Error: Python script fail, look in the console for now... It refers to this line: mymat.node_tree.links.new(ColorRamp.outputs[0], DiffuseBSDF.inputs[0]) Do you know why I get this failure message? – qube13 Oct 30 '15 at 16:31