Append operator to menu - at specific index

I can append my custom item to a menu. It either place it last or first (append - prepend).
bpy.types.VIEW3D_MT_add.append(menu_func)

It works and it does the job, but is not exactly a dream.

But say for example I want to append an item to a specific index. Say for example after the “Join” entry.
2021-01-17 08_54_35-Blender

I would do something like this in python.

a = [1,3,4]
a.insert(a.index(3), 2)

Not sure how to use the API though in an advanced way. There are “items()” or “keys()” or accessing the RNA properties as such.
bpy.types.VIEW3D_MT_object...

1 Like

Appending is all you need. According to the docs https://docs.blender.org/api/current/bpy.types.Menu.html section extending menus

You should create a function that that modifies menu. It makes sense as several add-ons can modify one menu. In this function you have access to self.layout and you can do anything you want with it.

I am on the go, can not provide details.

1 Like

I will go with append. Though I would keep this in mind for the future. At some point I will look into this right from the blender source. :slight_smile:

I’m sorry, but I’m not understanding how to insert a menu command at a specific index (per the original post) with self.layout from the doc link you sent. My other research shows people overriding the menus’ draw functions with a lot of code.

Does anyone know of a way to do this?

The TLDR version is that it can’t be done.

You aren’t appending menu items, you’re appending function pointers. So, while you are seeing 30 lines in a menu, it is likely a single function drawing them all. Short of rewriting the menu with your item where you want it to be, it isn’t possible.

yep, You can append and prepend easily, index is not available.
You can try doing things like: copy original source of class bpy.types.TOPBAR_MT_file, edit its draw function and re-register, or catch and filter draw function calls, but it is uphill battle.

For more details andbout this uphill battle see python - Add Custom Menu at Specific Location in the Header - Blender Stack Exchange

1 Like