godot-docs icon indicating copy to clipboard operation
godot-docs copied to clipboard

Improved docs for EditorContextMenuPlugin with full working example

Open kristianmandrup opened this issue 7 months ago • 3 comments

Your Godot version:

4.4.1

Issue description:

EditorContextMenuPlugin docs were insufficient to make a working plugin

URL to the documentation page (if already existing):

https://docs.godotengine.org/en/4.4/classes/class_editorcontextmenuplugin.html#class-editorcontextmenuplugin

I struggled for hours before I finally managed to create a working EditorContextMenuPlugin for the scene tree and file system. I propose to add the following examples to the EditorContextMenuPlugin page:

Note that this example might still not be perfect, so please fix as needed such as the typing which is still not entirely clear to me. Also would be nice with an example that uses an icon and perhaps adds a shortcut as well, which wasn't clear how to achieve.

How do I create or get the SHORTCUT here?

func _init():
	add_menu_shortcut(SHORTCUT, handle)

func _popup_menu(paths):
	add_context_menu_item_from_shortcut("File Custom options", SHORTCUT, ICON)

Working sample code:

scene_context_menu.gd

class_name SceneContextMenu
extends EditorContextMenuPlugin

func _init():
	print("Initialized custom SceneContextMenu")

func _popup_menu(paths: PackedStringArray) -> void:
	add_context_menu_item("Import nodes", on_import_selected)
	add_context_menu_item("Export node", on_export_selected)

func on_import_selected(paths: PackedStringArray):
	print("import assets for", node)

func on_export_selected(paths: PackedStringArray):
	print("export assets for", node)

file_system_context_menu.gd

class_name FileSystemContextMenu
extends EditorContextMenuPlugin

func _init():
	print("FileSystemContextMenu: _init() - Initializing context menu.")

func _popup_menu(paths: PackedStringArray) -> void:
	add_context_menu_item("Import assets here", on_import_selected)
	add_context_menu_item("Export assets", on_export_selected)
	print("FileSystemContextMenu: _popup_menu() - Popup menu extended with import and export assets from filetree.")	

func on_import_selected(paths: PackedStringArray):
	print("file context item handle:", paths)
	if not paths.size() == 0:
		print("Can only import to a single location")
		return
	var path = paths[0]
	print("import assets to filetree at", path)

func on_export_selected(paths: PackedStringArray):
	print("export filetree assets as paths:", paths)

plugin.gd

@tool
extends EditorPlugin

var file_context_menu_instance: EditorContextMenuPlugin # Instance for FileSystem context menu
var scene_context_menu_instance: EditorContextMenuPlugin # Instance for SceneTree context menu

func _enter_tree():
    print("Plugin Enabled")
    const FileSysCtxMenu = preload("res://addons/my-plugin/file_context_menu.gd")
    const SceneCtxMenu = preload("res://addons/my-plugin/scene_context_menu.gd")

    print("Adding context menu plugins")
    scene_context_menu_instance = SceneCtxMenu.new()
    file_context_menu_instance = FileSysCtxMenu.new()

    # add custom ctx menu extension to scene tree context menu
    add_context_menu_plugin(EditorContextMenuPlugin.CONTEXT_SLOT_SCENE_TREE, scene_context_menu_instance)   

    # add custom ctx menu extension to file system tree context menu
    add_context_menu_plugin(EditorContextMenuPlugin.CONTEXT_SLOT_FILESYSTEM, file_context_menu_instance)

func _exit_tree():
    print("Plugin Disabled")
    # Clean up plugin instances
    if is_instance_valid(file_context_menu_instance):
        remove_context_menu_plugin(file_context_menu_instance)
    if is_instance_valid(scene_context_menu_instance):
        remove_context_menu_plugin(scene_context_menu_instance)

kristianmandrup avatar Jun 06 '25 16:06 kristianmandrup

Thank you for making this issue, helped unblock me in my addon development 🙏

arran-nz avatar Sep 03 '25 12:09 arran-nz

How do I create or get the SHORTCUT here?

I think you're meant to set a value in the EditorSettings singleton using EditorInterface.get_editor_settings(). I don't know off the top of my head if you can create a shortcut that can be configured using the Editor Settings' Shortcuts tab though.

This is what the editor does, but it's not exposed to scripting: https://github.com/godotengine/godot/blob/6c9aa4c7d3b9b91cd50714c40eeb234874df7075/editor/settings/editor_settings.cpp#L1843-L1893

Calinou avatar Sep 03 '25 21:09 Calinou

In Godot 4.5, I am convinced that the Shortcut option does not work. The regular item option does work. If anyone can show a working example that uses a shortcut that would be great!!

Only real workaround is to toggle input when ScriptEditor has focus and do the action that way

AndySamoil avatar Oct 02 '25 03:10 AndySamoil