+1 vote

Hello I put here this new doubt:

I am creating a tool that dynamically adds several nodes to the SceneTree at once.

Is there any way to tell the SceneTree to select to edit one of those nodes that I have added as a child?

enter image description here


EDIT:

I found the solution myself:

The EditorInterface class has a method called "edit_node" that does just what I was looking for (The tool script has to have access to that class to use that method).


Godot version 3.4.2
in Engine by (196 points)
edited by

1 Answer

0 votes

this is possible thru plugins
in EditorPlugin you can get EditorInterface
like so - plugin.get_editor_interface().get_selection()
now you can use methods from here:
https://docs.godotengine.org/en/stable/classes/class_editorselection.html
for example if you want to select root node of the scene:

_editor_interface = plugin.get_editor_interface()
_edited_scene_root = _editor_interface.get_edited_scene_root()
_editor_interface.get_selection().add_node(_edited_scene_root)

just pass to add_node method nodes that you want to be selected
here is my own list of functions to work with nodes in editor (Godot 3.5)

# way to get editor scene selected nodes
# works only with tool scripts
static func get_selected_nodes(plugin:EditorPlugin)->Array:
    return plugin.get_editor_interface().get_selection().get_selected_nodes()

# for single node
static func get_selected_node(plugin:EditorPlugin)->Node:
    var selected = get_selected_nodes(plugin)

    if not selected.empty():
        # Always pick first node in selection
        return selected[0]

    return null

# selects desired node in editor
static func select_editor_node(plugin:EditorPlugin, node:Node):
    plugin.get_editor_interface().get_selection().add_node(node)

# getting edited scene
static func get_edited_scene_root(plugin:EditorPlugin):
    return plugin.get_editor_interface().get_edited_scene_root()
by (26 points)
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 Frequently asked questions and 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 [email protected] with your username.