Select a node via gdscript in SceneTree of the Editor

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

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?


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).


:bust_in_silhouette: Reply From: aftamat4ik

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:

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()