1
$\begingroup$

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.

$\endgroup$

1 Answer 1

1
$\begingroup$

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)
$\endgroup$
4
  • $\begingroup$ You don't have to use get() the material name is a dictionary key so you can use mat = bpy.data.materials["Abc"] $\endgroup$
    – sambler
    Oct 3, 2016 at 6:21
  • $\begingroup$ oh. I didn't know that. Are they the same though? $\endgroup$
    – Bradman175
    Oct 3, 2016 at 6:33
  • 1
    $\begingroup$ 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. $\endgroup$
    – batFINGER
    Oct 3, 2016 at 6:47
  • $\begingroup$ ah! then im fine here. $\endgroup$
    – Bradman175
    Oct 3, 2016 at 7:24

You must log in to answer this question.

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