How to open a new empty or inherited scene tab via code in the editor?

Godot Version

4.2.1

Question

Hi, is there a way to open a tab for a new or inherited scene via code, like Scene → New or Scene → New Inherited Scene in the editor menu do? The only functionality I see coming close is EditorInterface.open_scene_from_path(), but that only works with an existing scene, obviously.

It’s a bit convoluted but this should work:

@tool
extends EditorScript


func _run() -> void:
	# Get the editor's base control
	var base_control = EditorInterface.get_base_control()
	# Get the editor title bar (the one with Scene Project Debug ...)
	var editor_title_bar = base_control.find_child('*EditorTitleBar*', true, false)
	# Get the first PopupMenu (Scene)
	var scene_popup = editor_title_bar.find_children("*", "PopupMenu", true, false)[0] as PopupMenu
	# Get the id of the first item (New Scene)
	var id = scene_popup.get_item_id(0)
	# Emit the "id_pressed" signal
	scene_popup.id_pressed.emit(id)
1 Like

It took me a bit to realize this just calls the related menu entries, nice :sweat_smile: Works well enough for what I want, many thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.