Instantiate scenes via a @tool script?

Godot Version

4.3

Question

Hi there, I’m trying to create a tool that can generate and load scenes from disk based on a few input parameters.

This is the code I’m using:

			print_debug("Creating chunk %s" % chunk_name)
			var chunk = Node3D.new()
			chunk.set_name(chunk_name)
			chunk.position = chunk_position
			
			print_debug("Saving chunk %s" % chunk_path)
			var scene = PackedScene.new()
			scene.pack(chunk)
			var flags = 0 # ResourceSaver.SaverFlags.FLAG_CHANGE_PATH | ResourceSaver.SaverFlags.FLAG_REPLACE_SUBRESOURCE_PATHS
			var error = ResourceSaver.save(scene, chunk_path, flags)
			
			if error != OK:
				printerr("Failure: %s" % error)
			else:
				print_debug("Success!")
			
			var instance = scene.instantiate()
			add_child(instance)
			chunks.append(instance)
			instance.owner = self

The chunk shows up in the editor, but it doesn’t show the same “Open in Editor” icon. That makes me think something is missing in my initializing code and the relation scene ↔ Node is lost.

Example:

There are a few parameters to instantiate in the documentation, that might hold the solution, but I find them super cryptic:

GEN_EDIT_STATE_MAIN
If passed to instantiate, provides local scene resources to the local scene.

In the context of calling instantiate,

Who provides local scene resources?
What is local scene resource?
What is the local scene? the instantiated scene? That doesn’t make much sense to me (yet),

I would love some hints on how to interpret that doc and advice on how to correctly instantiate a child scene programmatically :slight_smile:

In case that helps someone: I found that if you load from disk instead of instantiating the scene directly, the Node will be linked to its scene correctly:

			# ...
			var error = ResourceSaver.save(scene, chunk_path, flags)
			
			if error != OK:
				printerr("Failure: %s" % error)
			else:
				print_debug("Success!")

			scene = ResourceLoader.load(chunk_path)
			var instance = scene.instantiate()
			add_child(instance)
			chunks.append(instance)
			instance.owner = self

That’s good enough for me for now