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:
tool
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():
remove_control_from_docks(dock)
if(dock!=null):
#dock.get_parent().remove_child(dock);
dock.free()
dock = null
The actual dock (Dock.tscn) has the following script attached:
tool
extends Control
var ep = null
func _enter_tree():
get_node("BUTTON").connect("pressed", self, "_test_stuff")
ep = EditorPlugin.new()
func _test_stuff():
var selection = ep.get_selection().get_selected_nodes()[0]
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 _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
@eska
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()
if selected.size():
return selected[0].get_tree().get_edited_scene_root()
return null
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:
(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…?)