I have an animation player and I want to invoke a function anytime a keyframe gets added
like this:
extends AnimationPlayer
...
func key_added(track_indx,key_indx):
print("key added: ",key_indx," to track:",track_indx)
is something like this possible? is there any inbuilt function that I'm missing?
I did not have the time to fully experiment with it, but this should give you a direction you could look into.
first of you will need to alter the animation class to give you the needed event. To be able to use it in the editor keep in mind, that you need the tool flag:
extends Animation
tool
class_name ToolAnimation
signal key_added(track_idx, key_indx)
func track_insert_key ( track_idx : int, time : float, key, transition : float = 1) -> void:
.track_insert_key(track_idx, time, key, transition)
#need to find the key index of the key we added
var key_id = track_find_key ( track_idx, time, true )
emit_signal("key_added", track_idx, key_id)
All I do here is to overwrite the track_insert_key to search for the key Id after adding it and then emit a signal.
Now we need to tell our animation_player to add our newly created animations instead of normal animation classes so we change the script of the animation_player and override the add_animation function:
extends AnimationPlayer
tool
func add_animation(name : String, animation: Animation):
var tool_animation = ToolAnimation.new()
tool_animation.connect("key_added", self, "key_added")
.add_animation(name, tool_animation)
pass
func key_added(track_indx,key_indx):
print("key added: ",key_indx," to track:",track_indx)
Now everytime a key is added you should get into the key_added method.
This will only work for newly created animations, because existing ones will not have the toolanimation extensions. To add the feature to existing animations, you would need to deep copy them in your ready functions, for example.
Edit: As @cakelover pointed out in the comments: To alter existing animations, iterate over them and use their set_script() function.
Second thing I noticed, when trying it out was, that my key_added method was not called if a track is newly created and the first key is added simultaniously (basically pressing the key symbol on a property I did not track before). So thats something you should look into, if you also need the first key.