get scene tree from editor plugin

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By twinpixel
:warning: Old Version Published before Godot 3 was released.


I’ve created an editor dock following this tutorial

So far it contains only a button.

Now, I would like to access the scene_tree by pressing the button, for example, get the name of the current scene and print it to console, check if the scene contains a certain node, …

How can I do that?

The dock is initialized through:

extends EditorPlugin

var dock = null

func _enter_tree():
	dock = preload("res://addons/dock/Dock.tscn").instance()
	add_control_to_dock( DOCK_SLOT_RIGHT_UL, dock )

func _exit_tree():
		dock = null

The actual dock (Dock.tscn) has the following script attached:

extends Control

var ep = null

func _enter_tree():
	get_node("BUTTON").connect("pressed", self, "_test_stuff")
	ep =

func _test_stuff():

	var selection = ep.get_selection().get_selected_nodes()[0]
	print( str(selection.get_name()) )
:bust_in_silhouette: Reply From: eska

You can access the editor’s SceneTree as usual by calling get_tree() from a Node. This requires that the node is already inserted into the tree (i.e. inbetween _enter_tree and _exit_tree)
To get the root of the currently edited scene, use get_tree().get_edited_scene_root().

Naturally, this will return null outside the editor (in game) and the function is not available at all in templates/exported games.

Thanks. That worked.

twinpixel | 2016-08-14 13:44

Your proposed method also adds a bunch of timers and other nodes that are not visible in the scene tree

On a side node- how would you go about getting the node icons from the scenetree? Let’s say that for my addon I want to recreate the scene tree interface in godot’s editor - with the icons - but use another functionality on my version of it

How does one retrieve a node’s icon that is used by godot editor?

blurymind | 2018-04-06 12:19

If you want to avoid runtime-generated nodes, test if a node’s owner is the edited scene. If node.owner == get_tree().edited_scene_root is false, then node is either the scene root or is generated at runtime.

To use node icons, retrieve them from the EditorIcons type in the editor theme. Open a new question if you need a code sample, this thread is unrelated and 1.5 years old

eska | 2018-04-06 13:42

:bust_in_silhouette: Reply From: takaturre

If you don’t have the node (for node.get_tree().get_edited_scene_root()), there are two indirect ways, too:

  1. Use the editor selection (requires at least one selected node) - doesn’t require an actual editor plugin.
    func get_edited_root() -> Node:
        var plugin :=
        var eds := plugin.get_selection()
        var selected := eds.get_selected_nodes()
        if selected.size():
            return selected[0].get_tree().get_edited_scene_root()
        return null
  1. Create a real (but invisible) editor plugin and use its scene_changed( scene_root : Node ) -signal and a singleton. In the simply write:
    extends EditorPlugin

    func _enter_tree():

Then in _tool singleton:

    var editor_plugin : EditorPlugin setget set_editor_plugin
    func set_editor_plugin(new_plugin : EditorPlugin):
        editor_plugin = new_plugin
        editor_plugin.connect("scene_changed", self, "set_edited_root")

    var edited_root: Node setget set_edited_root
    func set_edited_root(new_root : Node):
        edited_root = new_root

(You can also make an EditorScript and use its get_scene() method. Though, if I understood correctly, you cannot retrieve this info from another script…?)