2
$\begingroup$

As per Blender's documentation about extending the right click context menu, I can find what UI button was right clicked with a custom operator I added to the menu:

blender custom right click menu

However, "button_prop" just outputs the entire Rotation vector array, like this:

console output

But the context menu knows I clicked the "X" Rotation field/button:

enter image description here

I don't see a way to access that data. If I click X Rotation, I want to access just that property with this operation, not the entire array

I can find the object this field points to, from the same Blender docs example:

value = getattr(context, "button_pointer", None)

But that just returns an Object, and not the specific property I clicked.

Here's my complete script:

import bpy
context = bpy.context
    
# Right Click menu

class WM_OT_test_right_click_menu(bpy.types.Operator):
    bl_idname = "wm.test_right_click_menu"
    bl_label = "Test Right Click"

    def execute(self, context):
        
        #get name of right clicked custom prop
        pointer = getattr(context, "button_pointer", None) 
        prop = getattr(context, "button_prop", None)   
        
        print()
        print()
        print(prop)

        return {'FINISHED'}
     
def draw_button_menu(self, context):
    layout = self.layout
    layout.separator()
    layout.operator("wm.test_right_click_menu")
    

classes = [
    WM_OT_test_right_click_menu,
]        
        
      
def register():
    for c in classes:
        bpy.utils.register_class(c)
    
    bpy.types.UI_MT_button_context_menu.append(draw_button_menu)


def unregister():
    for c in classes:
        bpy.utils.unregister_class(c)
    
    bpy.types.UI_MT_button_context_menu.remove(draw_button_menu)
    
    
if __name__ == "__main__":
    register()

Update (1-17-2024): In rooting through Blender's source code, I can see that, for example, the Edit Driver operator in the right click menu has the same setup as my "getattr" function:

UI_context_active_but_prop_get(C, &ptr, &prop, &index);

It has the context, pointer, and property. But also has the index, which I'm assuming is the index of the array when you click a button that's an array (location, rotation, scale, etc).

So I'm still not sure how to get the array index, but I can see evidence for this functionality in the source code.

$\endgroup$
2
  • 1
    $\begingroup$ Please don't post code as screenshot. Post the code instead. $\endgroup$
    – taiyo
    Commented Jan 16 at 8:46
  • 1
    $\begingroup$ I've pasted my script at the bottom of my post now. $\endgroup$
    – JakeONeal
    Commented Jan 16 at 17:10

1 Answer 1

1
$\begingroup$

With Blender 4.0, they've added context.property that does exactly what I was asking for here. So, the problem is solved!

Here's some sample code. This adds an operator (menu item) to the right click menu called Test Operator. When clicked, Test Operator outputs the property you clicked, (e.g. 'scale') and the array index, to the system console. In my test case, output looked like this: (bpy.data.objects['Cube.002'], 'scale', 0)

import bpy
context = bpy.context


class OBJECT_OT_test_operator(bpy.types.Operator):
    bl_idname = "agt.test_operator"
    bl_label = "Test Operator"
    bl_options = {'REGISTER', 'UNDO'}
    
    def execute(self, context):
              
        print(context.property)

        return {'FINISHED'}

# Menu entries to add
  
def draw_button_menu(self, context):
    layout = self.layout
    layout.separator()
    layout.operator("agt.test_operator")

    
classes = [
    OBJECT_OT_test_operator
]   
    
def register():
    for c in classes:
        bpy.utils.register_class(c) 
    
    bpy.types.UI_MT_button_context_menu.prepend(draw_button_menu)   

def unregister():
    for c in classes:
        bpy.utils.unregister_class(c)
        
    bpy.types.UI_MT_button_context_menu.prepend(draw_button_menu)

if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

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