Page MenuHome

Implementation of custom python entries in all right click menus
ClosedPublic

Authored by Thomas Beck (plasmasolutions) on Apr 9 2017, 2:33 PM.

Details

Summary

Hi Guys,

as one of my clients needs the possibility to have custom menu entries in the general right click menu (all over Blender: in the node editor, properties, toolbars,..) I talked with Campbell about expanding our hard coded menu a bit. This is the outcome. As I only need those two, I support currently a button_prop and a button_pointer.

I tested the changes with a custom script where I added a custom entry and executed an operator on click - it seems to work exactly how it's intended to. The script:

As I'm not too experienced in rna stuff I would really appreciate any review.
Thanks very much Campbell for his open ears & help on this issue!

Diff Detail

Repository
rB Blender

Event Timeline

@Bastien Montagne (mont29) @Sybren A. Stüvel (sybren) Would be great if you could give me input on that too as I would like to commit that before our 2.79 release. As it's our production release and will definitely stay fresh a bit longer, I would really like to give add-on creates this functionality now. Thanks!

This is a suggestion I made for a way for Python to extend the RMB-buttons menu.

Adding these kinds of hidden menus feels a bit weak, but apart from moving the entire menu to Python, I'm not sure how to solve this in a nicer way.


Note, with all properties exposed it may in fact be possible to move this menu to Python.

source/blender/editors/interface/interface_handlers.c
6741

WM_MT_context is a bit meaningless, possibly WM_MT_button_context ?

6778

Could add support for index (although maybe tricky to do via uiLayoutSetContextPointer). And should support operator + options too.

Thomas Beck (plasmasolutions) edited the summary of this revision. (Show Details)

Operator support, renaming of WM_MT_context to WM_MT_button_context

Thomas Beck (plasmasolutions) marked an inline comment as done.Apr 9 2017, 3:23 PM
Thomas Beck (plasmasolutions) added inline comments.
source/blender/editors/interface/interface_handlers.c
6778

Operator support is now in it...

In your example code you write:

# This class has to be exactly named like that to insert an entry in the right click menu
class WM_MT_button_context(Menu):
    bl_label = "Add Viddyoze Tag"

So how does that work when multiple add-ons want to add such a custom menu item?

So how does that work when multiple add-ons want to add such a custom menu item?

Cleared that up with Sybren already in IRC, still want to repeat if for the notes: Currently not at all! The last add-on will get the spot. We could of course enter more hidden menu entries with different names and check in python if the first one is already taken... but this patch aims at making it possible at all to have a custom entry in the right click menu. I will work on transforming the right click menu from c to python in our 2.8 branch at mid/end of the year (hopefully with Campbell's help again,) but we need a non-intrusive solution for 2.79 now (as this will be the last stable one for a longer period of time and we got the third client now that begs for it).

LGTM.

Single-addon access to the feature is slightly annoying (though not totally true I think, addon could check for already existing menu class, and add entries to it instead of redefining I think?), but this solution is simple and should help already compared to current situation.

This revision is now accepted and ready to land.Apr 23 2017, 5:18 PM

LGTM, although if we start having many of these they should really be documented in our API somewhere.

While I don't think there is an ideal place for this - we could list menus that allow extension here: https://docs.blender.org/api/blender_python_api_current/bpy.types.Menu.html

source/blender/editors/interface/interface_handlers.c
6778

Would add comment here noting that index could be supported,

Thomas Beck (plasmasolutions) edited the summary of this revision. (Show Details)

Documentation for the feature and a last comment on indices like Campbell proposed... going to commit that now.

Thomas Beck (plasmasolutions) marked 3 inline comments as done.Apr 28 2017, 11:22 PM
This revision was automatically updated to reflect the committed changes.

Hi, I like this feature. It would be nice to get this done. Here are my ideas:

  • Add a new class similar to WM_MT_button_context, but this one will be available by default, so multiple add-ons can implement right-click context menu just by calling WM_MT_button_context_thatnewname.append(renderfunction)
  • There is one todo: "index could be supported as well". I'm horrible at writing C, it's not something I do every day, not even every year. I'm sending a patch which works for me. I can see it is absolutely against all rules, at least the line #include "UI_interface.h", but I am not able to find the nice and correct solution. And there is at least one more unresolved problem - in all contexts there is button_index, even when it makes no sense. Please look someone at it.
  • Update bpy.types.Menu.4.py and add there more complete example of using this, something like:
pointer = getattr(context, "button_pointer", None)
prop = getattr(context, "button_prop", None)
index = getattr(context, "button_index", None)
...
if prop.is_array: 
    print(getattr(pointer, prop.identifier)[index])
else:
    print(getattr(pointer, prop.identifier))

This revision is now accepted and ready to land.Nov 5 2019, 8:26 PM

@Robert Juricek (rooobertek) please do not re-open closed revisions! If you want to propose a patch, create a new revision.