Keymap sets a toggle.
The operator has two properties, mode
and toggle
>>> bpy.ops.object.mode_set(
mode_set()
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
Sets the object interaction mode
Mesh object active
The TAB Shortcut The toggle
property is set to True
.
which correlates to
bpy.ops.object.mode_set(mode='EDIT', toggle=True)
GPencil object active
Why is it blank when GP is active, the keymap uses 'EDIT' as the setting, which is not available as an enum item. (See below) Appears to be some dodgy fix in place behind the scenes, replacing 'EDIT' with a startswith 'EDIT' option.
The shortcut can be emulated by emulating this.
import bpy
ob = bpy.context.object
bpy.ops.object.mode_set(
mode = 'EDIT_GPENCIL' if ob.type == 'GPENCIL' else 'EDIT',
toggle=True,
)
use this in your scripts to emulate tab.
Dynamic Enum
On the "Non Matching". An enum property can be dynamic, ie items added / removed based on context. If a grease pencil object is active the options are different for if a mesh object is active, etc.
The documentation shows all possible items, of which only a subset will be valid, based on context.
EDIT
On further investigation, the shortcut is using
bpy.ops.gpencil.editmode_toggle()
this can be confirmed by pressing TAB with GP object active and in python console
>>> C.active_operator
<bpy_struct, Operator("GPENCIL_OT_editmode_toggle") at 0x7f3221be1988>
>>> C.window_manager.operator_properties_last('gpencil.editmode_toggle').back
False
when the context object is a gpencil, so without any knowledge of context, could use a try catch clause
import bpy
try:
bpy.ops.object.mode_set(
mode = 'EDIT',
toggle=True,
)
except TypeError:
bpy.ops.gpencil.editmode_toggle()
which will still throw a runtime error if the context object is None for example.
bpy.ops.gpencil.editmode_toggle()
However, if you go into POSE MODE and press TAB there, you will get both editmode toggle and posemode toggle. The same happens when you press TAB again, Blender remembers that you were in POSE MODE. $\endgroup$