0

I'm trying to create my own plugin and I need to figure out which AnimationPlayer node is currently shown in the bottom Animation panel

enter image description here

and please do not suggest I use get_selection() because even if I select multiple (or none ) AnimationPlayer node(s), only one is show on the Animation Panel

so far my approach has been to try finding the node to the Animation panel using get_editor_interface() and then maybe viewing it's properties?

Edit

Based on the answer given by user @Theraot I tried this:

tool
extends EditorPlugin

func handles(object):
    print('handles=>',object);
    return true;
    
func edit(object):
    print('edit=>',object);
    

func _enter_tree():
    pass;

but using this I only get the selected nodes within the tree but even if I haven't selected any AnimationPlayer Node it still shows up in the animation panel

enter image description here

cak3_lover
  • 1,440
  • 5
  • 26

1 Answers1

1

On your EditorPlugin implement handles to return true on any AnimationPlayer given to it, and Godot should call edit with the AnimationPlayer that is to be edited currently.

Example code:

tool
extends EditorPlugin


var edited_animation_player:AnimationPlayer


func handles(object:Object) -> bool:
    return object is AnimationPlayer
    

func edit(object:Object) -> void:
    edited_animation_player = object
    print(edited_animation_player)

I was hoping the information of which AnimationPlayer is currently being edited in the Animation panel was perhaps stored in some node for the bottom panel?

Even if get the correct node from the interface:

    var control:= Control.new()
    var animation_player_editor:Control
    add_control_to_bottom_panel(control, "puff")
    for sibling in control.get_parent().get_children():
        if (sibling as Control).get_class() == "AnimationPlayerEditor":
            animation_player_editor = sibling
    remove_control_from_bottom_panel(control)
    if animation_player_editor != null:
        print(animation_player_editor)

Which AnimationPlayer it is editing is not exposed to scripting. We can find a get_player method in the source code and a get_state method also in the source code, but we could only access them from C++, as they are not bound for scripting (source code).

In theory we should be able to get the EditorPlugin and call get_state on it, however it is not working for me:

    var animation_player_editor_plugin:EditorPlugin
    for sibling in get_parent().get_children():
        if (sibling as Node).get_class() == "AnimationPlayerEditorPlugin":
            animation_player_editor_plugin = sibling
    if animation_player_editor_plugin != null:
        print(animation_player_editor_plugin.call("get_state"))

Despite get_state being exposed on EditorPlugin, it appears to not be possible to call it.

By the way, we can see in the source code for AnimationPlayerEditorPlugin that it follows the handles and edit logic I used above.

Theraot
  • 31,890
  • 5
  • 57
  • 86
  • I've edited the question based on your answer, could you take a look? – cak3_lover Feb 02 '22 at 10:12
  • @cakelover The `AnimationPlayer` should be the last one you got on `edit`. However you didn't return `true` from `handles`. That is, you are telling Godot you are not interested in that object, so Godot will not call your `edit` for it. – Theraot Feb 02 '22 at 11:18
  • I added `return true` but the same object is being passed to `edit()` I really don't understand what we're trying to achieve here, how is this any different than using `get_editor_interface().get_selection().get_selected_nodes()`? – cak3_lover Feb 02 '22 at 13:08
  • 1
    @cakelover It is not the same thing. As you are aware `get_selected_nodes` can return none, one or multiple. However only one is the last edited node. Did you, by any chance return `true` to all objects this time around? (edit: Ah, yes, I just saw the updated code in the question) You should only return `true` if you get an `AnimationPlayer`, return `false` to any other object so you don't get them on `edit`. – Theraot Feb 02 '22 at 13:13
  • well, the `handles()` function only runs when a node is selected, in that case how do I get which `AnimationPlayer` is currently in the Animation Panel when none are selected? – cak3_lover Feb 02 '22 at 13:17
  • 1
    @cakelover The handles() function only runs when a node is selected. And which `AnimationPlayer` is edited on the animation panel changes when you select an `AnimationPlayer`. This makes sense to me. The one you want is the most recent one. Ah, the most recent one edited, not selected, that will deal with the case where multiple are selected. – Theraot Feb 02 '22 at 13:24
  • I mean technically your answer is correct but I was hoping the information of which `AnimationPlayer` is currently being edited in the Animation panel was perhaps stored in some node for the bottom panel? does such a thing exist? – cak3_lover Feb 02 '22 at 16:23
  • 1
    @cakelover See updated answer. – Theraot Feb 02 '22 at 17:46