Version: 2.83

I want to change the colour of a material during animation - via python script.

I can create a cube wherever I want, called myobject and attached to it an appropriate material:

        material.use_nodes = True
        principled_bsdf = material.node_tree.nodes['Principled BSDF']
        principled_bsdf.inputs[0].default_value = colour
        principled_bsdf.inputs[5].default_value = 0
        myobject.active_material = material

I can then move the object for each i and use myobject.keyframe_insert(data_path = "location", frame(i)) to insert frames appropriately. What is the correct data_path channel to insert keyframes for different colours, so I can change principled_bsdf.inputs[0] for whichever keyframes I want.

This question asks the same question, but doesn't have any answers: Change the color via python script

Note that using data_path = "color" doesn't give an error, but also doesn't work.


1 Answer 1


Keyframe the default value of the socket

Animating by keyframe the base colour of the bsdf node

To the python console.

From material "Material" get the bsdf node

>>> mat = D.materials['Material']
>>> bsdf = mat.node_tree.nodes['Principled BSDF']

What color is it now?

>>> bsdf.inputs['Base Color'].default_value
bpy.data.materials['Material'].node_tree.nodes["Principled BSDF"].inputs[0].default_value

>>> bsdf.inputs['Base Color'].default_value[:]
(0.800000011920929, 0.800000011920929, 0.800000011920929, 1.0)

Make it red and insert a keyframe at frame 30

>>> bsdf.inputs['Base Color'].default_value = (1, 0, 0, 1)
>>> bsdf.inputs['Base Color'].keyframe_insert("default_value", frame=30)

Same for blue at frame 50

>>> bsdf.inputs['Base Color'].default_value = (0,  0, 1, 1)
>>> bsdf.inputs['Base Color'].keyframe_insert("default_value", frame=50)

True above is indicating the success of adding the keyframe. To keyframe a single channel (r, g, b, a) -> index 0, 1, 2, 3. To keyframe just the green channel

input.keyframe_insert("default_value", index=1)

One material.

See this answer, https://blender.stackexchange.com/a/185314/15543

Could instead consider using one material and animate the object color.

ob.keyframe_insert("color", frame=33)
  • $\begingroup$ Thank you! This does the trick! $\endgroup$
    – Horizon
    Jul 11, 2020 at 1:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .