Programmatically animating color doesn't work

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bashan

I would like to programmatically animate the color of a simple MeshInstance cube and it doesn’t seems to work. I am also adding scale animation and it does work OK.

Here is my code:

var animation: Animation = animation_player.get_animation("myanim")

for i in animation.get_track_count():

var index = animation.add_track(Animation.TYPE_VALUE)
animation.track_set_path(index, "MeshInstance:scale")
animation.track_insert_key(index, 0, Vector3(1, 1, 1))
animation.track_insert_key(index, 0.5, Vector3(0.5, 0.5, 0.5))
animation.track_insert_key(index, 1, Vector3(1, 1, 1))

index = animation.add_track(Animation.TYPE_VALUE)
animation.track_set_path(index, "MeshInstance:mesh:material:albedo_color")
animation.track_insert_key(index, 0, Color(0, 1, 0))
animation.track_insert_key(index, 1, Color(1, 0, 0))

if (!animation_player.is_playing()):"myanim")

Anything I am missing?

Have you considered using a Tween node? It’s better suited for code-driven animations. Alternatively, some code in _process could do the same job.

Zylann | 2019-11-29 19:08

Thanks. I tried with Tween and got a strange behavior: my cube is simply colored in green (no animation), which is the original color of the mesh (please note I color the node in a different color when the screen loads). Then I also tried again with animation player and found my apparent problem: the track path should be: MeshInstance:material/albedo_color. Anyway, I get the same effect as Tween: the node is just colored in green. Note sure what exactly is wrong. Maybe it is a bug in Godot?

bashan | 2019-11-30 00:10

Do you have multiple instances of that mesh in your scene?

Zylann | 2019-11-30 00:23

Indeed. Many of them. When the screen loads, I override their original color by (it is a loop running on all nodes):

var material = node.mesh.surface_get_material(0).duplicate()
material.albedo_color = myCustomColor
node.set_surface_material(0, material)

bashan | 2019-11-30 12:19