So I wanted to make a simple context sensitive operator to check the edit mode and set the right kind of bevel.
You can see the python code in the tooltip here:
My code is the same but doesn’t do anything.
if context.tool_settings.mesh_select_mode[:] == (True, False, False):
print("Vert")
bpy.ops.mesh.bevel(affect='VERTICES')
if context.tool_settings.mesh_select_mode[:] == (False, True, False):
print("Edge")
bpy.ops.mesh.bevel(affect='EDGES')
Looking in the keymap there’s a bunch of modal keys under the bevel command key. So I’m guessing the menu is activating this stuff but that code is not shown?
If that is the case does anyone know if I’d be able to do the same?
the tooltip only shows the properties that are being set by the layout.operator() call. mesh.bevel has 19 properties, so you can imagine that the tooltip would be a bit ridiculous if they included all of them.
If you’re interested in seeing all of them, you can either check the docs or use autocomplete in the python console
The reason your code doesn’t do anything is because mesh.bevel is a modal operator, and it can’t do anything without a mouse event. You need to run the operator with the ‘INVOKE_DEFAULT’ flag. bpy.ops.mesh.bevel('INVOKE_DEFAULT', affect='VERTICES')
see the operator docs for more information on execution context
So I did check the docs on the bevel operator and was aware that there’s a lot properties. Also looking at the operator docs too I can’t see anything there which explains what you have which is that ‘invoke_default’ will have this affect.
I assume it’s just obvious if you have a certain level of knowledge (which I clearly lack)?
Yeah it might not be entirely obvious if you haven’t tried writing a modal operator before- but in Blender the only way to get any type of input event is to do so via a modal operator, specifically the ‘invoke’ method. A non-modal fire-and-forget operator typically just uses the ‘execute’ method, which does not have an event parameter. Some operators actually have both an invoke and an execute- because when you use the redo panel to adjust settings after you’ve run an op you’re effectively re-executing it.
By running an operator with the INVOKE_DEFAULT flag you are specifically telling Blender that the operator should use the invoke path, which in mesh.bevel’s case will include the input event and modal state.
If you wanted to bevel and already had a specific offset in mind and didn’t need the modal aspect of the operator, you could just pass in the parameters manually and the default execution context EXEC_DEFAULT would tell the operator to use the execute path, which would be perfect because it would happen instantly without any input needed from the user.