How to use EditorScript to create Nodes

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

I want to add some extra nodes to an object and automate it via the following editor script.
Getting the root node of the current scene works and I can change its name but adding child nodes does not. Any help would be welcome.

tool
extends EditorScript 


func _run():
get_scene().set_name("test")			# works
get_scene().add_child(Node.new())	    # does nothing

This Node.new() seems suspicious to me. Did you try it in the script from an existing scene?

Gokudomatic2 | 2016-04-15 09:40

:bust_in_silhouette: Reply From: ulty

working example, the .set_owner() needs to be set for the new node

extends EditorScript 
tool

func _run():

var new_node = Node.new() 

get_scene().add_child(new_node)

new_node.set_owner(get_scene())         
# even if you add it to a sub node set owner only to get_scene

Is set_owner still needed? It hasn’t thrown any errors when I leave it out.

jarlowrey | 2017-05-10 03:35