How to display my node in the scene inspector when it's created from a tool script?

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


I’m currently trying to make a tool script that will dynamically spawn nodes and add them to the inspector. The issue is that whatever I do, the node never appears in the scene inspector.

I also posted a comment on this github issue: nodes added as children in tool scripts are not visible in the editor · Issue #40385 · godotengine/godot · GitHub

Here is the code I’m using:

extends Node2D

@export var points : Array[Node2D]

func _process(delta):
	for index in range(points.size()):

		# If the user wants to add a new point, we automatically create it for convenience
		if points[index] == null:
			print("Spawning node")
			var node =
			points[index] = node
			# The following never worked.
			# node.set_owner(get_tree().get_root())
			# node.owner = get_tree().get_root()
			# node.owner = get_owner()
			# node.owner = self
			# node.owner = get_tree().get_edited_scene_root()


You can see the different solutions I tried. None of them worked. I sometime got the error

scene/main/node.cpp:1645 - Condition "!owner_valid" is true.

And sometimes no error but no node in the inspector.

The documention on owner didn’t help at all, I’m still a beginner and most of the information flew above my head.

As soon as I asked the question, I got an answer on the github issue.

The proper one seems to be get_tree().get_edited_scene_root() but the important thing is that add_child has to be called BEFORE the node owner is set.

Telokis | 2023-07-03 15:03

If you found a solution you can post it as answer and validate it :slight_smile:

Zylann | 2023-07-06 11:25