0 votes

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")
animation.set_length(1)

for i in animation.get_track_count():
    animation.remove_track(i)

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()):
    animation_player.play("myanim")

Anything I am missing?

in Engine by (50 points)
retagged by

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.

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?

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

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)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.