Is it possible to SaveBranchAsScene from code?

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

I’m building an editor tool, and i’m trying do the following:

  • take a Node and it children from the sceneTree
  • pack them into a PackedScene and save to file,
  • add them back to the main scene, maintaining the reference to the file

This is the same functionality as SaveBranchFromScene… is it possible to do this from code?

I can instance a scene and add it back into the tree, but it’s not a linked instance… like when you right click on a node and press “Make Local”.
I need it to be a linked instance, so that when I edit it the file elsewhere in the future, it’ll update here too.

:bust_in_silhouette: Reply From: rossunger

Wow. the answer is so simple. I can’t believe how long I spent looking for this :stuck_out_tongue:
You just set the instanced node’s “filename” property.

var oldScene = get_node("oldScene")
var packedScene = PackedScene.pack(oldScene)
ResourceSaver.save("res://scenefile.tscn", packedScene)
oldScene.filename = "res://scenefile.tscn"