post_import(scene) add extra scene for all child

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

Hi, I want to add a previously made scene(Spatial.scn) to every child in new scene when I import it using post_import() script, but it only add ONE Spatial.scn .

errors:
 scene/main/node.cpp:1685 - Condition ' !owner_valid ' is true.
 Can't add child 'Spatial' to 'Scene Root', already has a parent 'Scene Root'.
 Can't add child 'Spatial' to 'Scene Root', already has a parent 'Scene Root'.
 Can't add child 'Spatial' to 'Scene Root', already has a parent 'Scene Root'.

Script

 tool # needed so it runs in editor
    extends EditorScenePostImport
    
    var qq = preload("res://Res_Enviro/Spatial.scn").instance()
    
    func post_import(scene):
        # do your stuff here
    
        for child in scene.get_children():
    
            var n_instance = Node.new()
            n_instance = qq
            n_instance.set_owner(scene)
            scene.add_child(n_instance)
    
    return scene # remember to return the imported scene
:bust_in_silhouette: Reply From: zbyszko

i found solution

var n_instance = qq.duplicate()
 scene.add_child(n_instance)
 n_instance.set_owner(scene)
1 Like

This is perfect! The piece it was missing. Thank you very much