||Topic was automatically imported from the old Question2Answer platform.
||Published before Godot 3 was released.
I’ve created an editor dock following this tutorial http://docs.godotengine.org/en/latest/tutorials/_plugins.html.
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:
var dock = null
dock = preload("res://addons/dock/Dock.tscn").instance()
add_control_to_dock( DOCK_SLOT_RIGHT_UL, dock )
dock = null
The actual dock (Dock.tscn) has the following script attached:
var ep = null
get_node("BUTTON").connect("pressed", self, "_test_stuff")
ep = EditorPlugin.new()
var selection = ep.get_selection().get_selected_nodes()
print( str(selection.get_name()) )
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
To get the root of the currently edited scene, use
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
If you don’t have the node (for
node.get_tree().get_edited_scene_root()), there are two indirect ways, too:
- Use the editor selection (requires at least one selected node) - doesn’t require an actual editor plugin.
func get_edited_root() -> Node:
var plugin := EditorPlugin.new()
var eds := plugin.get_selection()
var selected := eds.get_selected_nodes()
- Create a real (but invisible) editor plugin and use its
scene_changed( scene_root : Node ) -signal and a singleton. In the
custom_node.gd simply write:
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…?)