Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

EditorContextMenuPlugin

Inherits: RefCounted < Object

Plugin for adding custom context menus in the editor.

Description

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.

Methods

void

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

void

_popup_menu(paths: PackedStringArray) 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)


Enumerations

enum ContextMenuSlot: 🔗

ContextMenuSlot CONTEXT_SLOT_SCENE_TREE = 0

Context menu of Scene dock. The data Dictionary contains:

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

ContextMenuSlot CONTEXT_SLOT_FILESYSTEM = 1

Context menu of FileSystem dock. The data Dictionary contains:

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

ContextMenuSlot 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:.

ContextMenuSlot 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).

func _get_menu_options(data):
    if data["needs_prefix"]:
        add_context_menu_item("New Image File...", create_image)
    else:
        add_context_menu_item("Image File...", create_image)

ContextMenuSlot 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.

ContextMenuSlot 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.

ContextMenuSlot 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.

ContextMenuSlot 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.


Method Descriptions

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.


void _popup_menu(paths: PackedStringArray) virtual 🔗

Deprecated: Use _get_menu_options() instead.

Note: Implementing this method makes the shortcut callbacks receive legacy data (for compatibility reasons).


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().

func _get_menu_options(data):
    add_context_menu_item("File Custom options", handle, ICON)

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


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().

func _init():
    add_menu_shortcut(SHORTCUT, handle)

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

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.

func _get_menu_options(data):
    var popup_menu = PopupMenu.new()
    popup_menu.add_item("Blue")
    popup_menu.add_item("White")
    popup_menu.id_pressed.connect(_on_color_submenu_option)

    add_context_submenu_item("Set Node Color", popup_menu)

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().

func _init():
    add_menu_shortcut(SHORTCUT, handle)

User-contributed notes

Please read the User-contributed notes policy before submitting a comment.