Tool script: how to create a node and have it visible in the tree?

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

When I create a node from a tool script, it doesn’t appears in the scene tree, and is not saved. How can I make it visible and/or saved?

:bust_in_silhouette: Reply From: vnen

You have to set the new node’s owner as the scene root. When you save a scene the editor saves the root node itself and every node it owns.

my_node.set_owner(scene_root)

I tried this:

mesh_instance.set_owner(get_tree().get_root())

but it produces an error:

ERROR: Node::is_editable_instance: Condition ' !is_a_parent_of(p_node) ' is true. returned: false
At: scene\main\node.cpp:1441

Zylann | 2016-08-13 20:43

It should be the root of the scene, not the root Viewport. Something lilke:

mesh_instance.set_owner(get_tree().get_edited_scene_root())

vnen | 2016-08-13 20:55

Is this valid on runtime / exported game?
I guess it should be like this, I guess:

if get_tree().is_editor_hint():
	mesh_instance.set_owner(get_tree().get_edited_scene_root())

Zylann | 2016-08-13 21:38

The editor hint check is needed, I think. You can use get_tree().get_current_scene() in the game.

vnen | 2016-08-14 01:58

Actually I think in game there is no need to declare an owner because it’s just like instancing any node.

Zylann | 2016-08-14 02:17