Skip to content

EditorContextMenuPlugin

Inherits: RefCounted < Object

Plugin for adding custom context menus in the editor.

EditorContextMenuPlugin allows for the addition of custom options in the editor’s context menu. Register them with EditorPlugin.add_context_menu_plugin().

See ContextMenuSlot for the list of available areas where context menu plugins are supported.

Returns Method
void _get_menu_options(data: Dictionary[StringName, Variant]) virtual
void add_context_menu_item(name: String, callback: Callable, icon: Texture2D = null)
void add_context_menu_item_from_shortcut(name: String, shortcut: Shortcut, icon: Texture2D = null)
void add_context_submenu_item(name: String, menu: PopupMenu, icon: Texture2D = null)
void add_menu_shortcut(shortcut: Shortcut, callback: Callable)

ContextMenuSlot

enum ContextMenuSlot

CONTEXT_SLOT_SCENE_TREE = 0

Context menu of Scene dock. The data Dictionary contains:

&"selected_nodes": Array[Entity] - The list of currently selected nodes (same as EditorSelection.get_top_selected_entities()).

CONTEXT_SLOT_FILESYSTEM = 1

Context menu of FileSystem dock. The data Dictionary contains:

&"selected_files": PackedStringArray - The list of selected files.

CONTEXT_SLOT_SCRIPT_EDITOR = 2

Context menu of Script editor’s script tabs and Shader editor’s shader tabs. The data Dictionary contains:

&"selected_resource": Resource - The currently selected resource. Can be a Script, a Shader, a text file, or null in case of help pages.

&"path": String - The path of the selected file. If a help page is selected, the path will be the name of the opened class, prefixed with help:.

CONTEXT_SLOT_FILESYSTEM_CREATE = 3

The “Create…” submenu of FileSystem dock’s context menu, or the “New” section of the main context menu when empty space is clicked. The data Dictionary contains:

&"base_directory": String - Path to the base directory where a file should be created.

&"needs_prefix": bool - Whether the option needs a “New” prefix (for consistency with native options).

CONTEXT_SLOT_SCRIPT_EDITOR_CODE = 4

Context menu of Script editor’s code editor. The data Dictionary contains:

&"code_edit": CodeEdit - The code editor instance.

&"file_path": String - Path to the currently edited file.

CONTEXT_SLOT_SCENE_TABS = 5

Context menu of scene tabs. The data Dictionary contains:

&"selected_scene": String - Scene path of the selected/clicked scene tab. Empty if empty space was clicked.

CONTEXT_SLOT_2D_EDITOR = 6

Context menu of the 2D editor’s viewport. The data Dictionary contains:

&"hovered_nodes": Array[CanvasItem] - The CanvasItem nodes under the cursor.

&"mouse_position": Vector2 - The local mouse position in the 2D scene.

CONTEXT_SLOT_INSPECTOR_PROPERTY = 7

Context menu of an editor property in the inspector. The data Dictionary contains:

&"property": EditorProperty - The clicked EditorProperty object.

&"object": Object - The object edited by the inspector.

&"property_name": StringName - The name of the object’s property that was clicked.

_get_menu_options

void _get_menu_options(data: Dictionary[StringName, Variant]) virtual

Called when creating a context menu, custom options can be added by using the add_context_menu_item() or add_context_menu_item_from_shortcut() functions. data contains the context data, which differs depending on the context menu slot. See the descriptions of ContextMenuSlot for more information.

add_context_menu_item

void add_context_menu_item(name: String, callback: Callable, icon: Texture2D = null)

Add custom option to the context menu of the plugin’s specified slot. callback should be a method that takes a single argument of type Dictionary. It will be called when the option is activated and the data received in the callback is the same as in _get_menu_options().

If you want to assign shortcut to the menu item, use add_context_menu_item_from_shortcut() instead.

add_context_menu_item_from_shortcut

void add_context_menu_item_from_shortcut(name: String, shortcut: Shortcut, icon: Texture2D = null)

Add custom option to the context menu of the plugin’s specified slot. The option will have the shortcut assigned and reuse its callback. The shortcut has to be registered beforehand with add_menu_shortcut().

add_context_submenu_item

void add_context_submenu_item(name: String, menu: PopupMenu, icon: Texture2D = null)

Add a submenu to the context menu of the plugin’s specified slot. The submenu is not automatically handled, you need to connect to its signals yourself. Also the submenu is freed on every popup, so provide a new PopupMenu every time.

add_menu_shortcut

void add_menu_shortcut(shortcut: Shortcut, callback: Callable)

Registers a shortcut associated with the plugin’s context menu. This method should be called once (e.g. in the plugin’s Object._init()). callback will be called when user presses the specified shortcut while the menu’s context is in effect (e.g. FileSystem dock is focused). Callback should take single data Dictionary, same as in add_context_menu_item().