# Blender Render: Python material

How do I use python to set keyframes for the diffuse colour of materials in blender render. This is what I got, but it doesn't work.

mat = bpy.data.materials("Abc")
mat.diffuse_color = (1.0,0.0,0.0)
mat.keyframe_insert("default_value", frame=scene.frame_current)


I have an existing material that I want to set keyframes for, but I don't know how to do it. I tried searching it up and only saw python scripts for cycles materials.

I found out what I did wrong. The documentation of materials on the blender api page did not mention "get", but the bpy_struct said you can use "get" in the function, so:

mat = bpy.data.materials("Abc")


Should be:

mat = bpy.data.materials.get("Abc")


Also I have to define what scene of scene.frame_current is. That means at the beginning, I need to import bpy.

import bpy
scene = bpy.context.scene


Also default_value is nonsense to be placed there. Being the diffuse color that's had its keyframe changed, it should be:

mat.keyframe_insert(data_path="diffuse_color", frame=scene.frame_current)


In conclusion, it should look like this:

import bpy
scene = bpy.context.scene

mat = bpy.data.materials.get("Abc")
mat.diffuse_color = (1.0,0.0,0.0)
mat.keyframe_insert(data_path="diffuse_color", frame=scene.frame_current)

• You don't have to use get() the material name is a dictionary key so you can use mat = bpy.data.materials["Abc"] – sambler Oct 3 '16 at 6:21
• oh. I didn't know that. Are they the same though? – Bradman175 Oct 3 '16 at 6:33
• The beauty of get is collection.get("doesnt exist") returns None whereas collection["doesn't exist"] will return an error. IMO  if mat is not None: is more elegant than a try except clause, or if "doesnt exist" in collection, hence I use get(...) as a matter of practice. – batFINGER Oct 3 '16 at 6:47
• ah! then im fine here. – Bradman175 Oct 3 '16 at 7:24