2
$\begingroup$

How do I add two items in the RMB context menu when right clicking in the outliner? I want them at the position of this red line:

enter image description here

There should be a separator after "Deselect" and before "Unlink" and in between I want to have the Batch rename (bpy.ops.wm.batch_rename()) operator and a flyout called "Rename utilities".

The Batch Rename operator is the same as in from Edit > Batch Rename

So the menu would look like this:


  • Copy
  • Paste

  • Delete
  • Delete Hierarchy

  • Select
  • Select Hierarchy
  • Deselect

  • Batch Rename
  • Rename Utilities >

  • Unlink
  • etc.

Rename Utilities would several operators that I am currently working on. How do I add those to the menu position (1) or position (2) or position (-1)? And how do I add break lines in between?

$\endgroup$
3
  • $\begingroup$ You can't. The available UI only allows you to add menu entries at the top or bottom. There are no interfaces for inserting or reordering. $\endgroup$ Commented Jun 5, 2022 at 15:06
  • $\begingroup$ Then how do I add these two items at the top of the menu? $\endgroup$
    – Hologram
    Commented Jun 5, 2022 at 15:16
  • 1
    $\begingroup$ use prepend instead of append $\endgroup$
    – Gorgious
    Commented Jun 5, 2022 at 20:24

1 Answer 1

4
$\begingroup$

The general way to add a new entry to a menu has two parts, the draw function for your menu entry, and a call to the menu type to add your draw function.

In this example TOPBAR_MT_ is a prefix indicating that the menu of interest is one of those on the top bar. The example uses the File menu and added an entry to the import submenu. For your second screenshot, you would change file_import to edit. You can find this out by enable dev extras in preferences and hovering over the Edit menu. If you want the entry at the top instead of the bottom, use prepend instead of append.

def menu_item_draw_func(self, context):
    # Replace the next line with your menu draw code
    # self.layout.operator()


def register():
    # replace TOPBAR_MT_file_import with the name of the menu you want
    # to add your menu item to.  append adds to the end, prepend to the
    # start
    # bpy.types.TOPBAR_MT_file_import.append(menu_item_draw_func)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .