Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reparenting nodes with Godot Editor Scripts

Tags:

godot

I'm trying to place each node in a scene in it's own parent node. I made a script that inherits from EditorScript and it creates the parents, but the nodes are disappearing from the editor tree view after calling reparent(). The meshes themselves are still visible in the editor view however. How do I reparent nodes in an EditorScript tool?

@tool
extends EditorScript

func _run():
    var meshes = get_scene().get_children();
    for node in meshes:
        # make node
        var parent: Node3D = Node3D.new()
        parent.name = node.get_name()+"_root"
        # add to scene
        get_scene().add_child(parent)
        parent.owner = get_scene()
        #reparent mesh to new node
        node.reparent(parent);
        node.owner=parent
        #get_scene().print_tree_pretty() # <-- shows the nodes, but they're not visible in the editor tree
        print(meshes.size())
        #return

For clarification, I've got multiple meshes on the root level, and I want to place each mesh in it's own node and add a convex collision to it, to convert the scene to a Mesh Library

like image 884
MrSomeone Avatar asked Mar 26 '26 15:03

MrSomeone


1 Answers

The owner is how Godot know to which "scene" a Node belongs.

More precisely a Node is considered a "scene" if it is to be persisted on its own file (and so it has the path of an associated PackedScene in its scene_file_path in Godot 4, or filename in Godot 3).

If the owner does not match with the currently edited scene, then it must either be a temporary node (that will not be persisted, and will not be shown in the editor), or belong to a sub-scene (which will be persisted separately). And to be clear, a Node child of the currently edited scene is considered a "scene"... by the means I said above... The children of the sub-scene are shown in the editor according to the "Children Editable Flag" bundled in the PackedScene.

You are setting the owner to a Node that is not considered a "scene":

node.owner=parent

Make the scene root the owner of the Nodes instead:

node.owner = get_scene()
like image 129
Theraot Avatar answered Mar 30 '26 03:03

Theraot