1
$\begingroup$

I am trying to create an animation with python.

For this I want to keyframe the material slot. So I tried:

matFocus = bpy.data.materials.new(name="matFocus") 
activeObject.material_slots[0].material=matFocus
activeObject.keyframe_insert(data_path="material_slots[0].material")

If I execute this, I get:

TypeError: bpy_struct.keyframe_insert() property "material_slots[0].material" not animatable

Background is: I have a scenery with many objects that all share the same material. My animation should highlight (via color) a sequence of objects one after another. So my approach was to assign a "focus-material" to each of them one by one and to keyframe this.

Is there a solution to this?

$\endgroup$
0
$\begingroup$

If you cannot keyframe the property from UI, you cannot from python either.

The solution is to have a material assigned to all the object behaving in such a way that its color is not constant but dynamic based on something else. It can be:

  • vertex colors
  • place in UV space
  • object index pass
  • ... (with drivers almost anything, but they don't update always nicely)

Let's use the object index pass, because it is just a single property you can animate, it is unique to each object and you have access to it from the material nodes:

enter image description here

Now if you change it for single object the material on that object will behave differently:

activeObject.pass_index = 1
activeObject.keyframe_insert("pass_index")

For Blender Internal the node setup is very similar (switch the material into node material):

enter image description here

There is also another solution available - you can set unique color for every object that is multiplied with the material (to get exactly the color have white material):

enter image description here

You set the color in python this way:

activeObject.color = (1.0, 0.0, 0.0, 1.0)   #RGBA red
$\endgroup$
  • $\begingroup$ Hooray, this works, thank you! As far as I can see this works with Cycles Render. Do you also have a hint how to achieve this with Blender Render? $\endgroup$ – Chris Feb 23 at 22:25
  • $\begingroup$ Great! Thank you! This also works! It would have taken me years to find out... $\endgroup$ – Chris Feb 26 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.