Ui button context is only available once the hard-coded right-click menu has been invoked (see here). Afaik there’s no way to invoke it through python, but I haven’t really experimented with this.
One way I know of is to copy the data path under the cursor to the clipboard and evaluate it. This, however, doesn’t work for all properties, especially not properties that aren’t exposed to python (i.e vertex location).
Stuff like object location, scale, etc should work, though.
This example tries to invert the property under the cursor. Usage: Run in text editor. Hover over a numeric prop and search for Invert.
import bpy
class UI_OT_invert(bpy.types.Operator):
bl_idname = "ui.invert"
bl_label = "Invert"
def execute(self, context):
wm = context.window_manager
__class__.buffer = wm.clipboard
try:
bpy.ops.ui.copy_data_path_button(full_path=True)
eval_path = eval(wm.clipboard)
exec(f"{wm.clipboard} = -eval_path")
except (RuntimeError, TypeError) as err:
print(err)
return {'CANCELLED'}
finally:
# restore clipboard content
wm.clipboard = __class__.buffer
return {'FINISHED'}
if __name__ == "__main__":
bpy.utils.register_class(UI_OT_invert)