Poll Method¶
A menu’s Poll determines whether it is available in Blender’s current state. Enter a condition in that menu’s Advanced settings → Poll field.
return True: make the menu available.return False: make it unavailable. Depending on its presentation and invocation, an item may be hidden or shown disabled.
Keymap and Poll have different roles¶
Keymap determines where a hotkey is registered; Poll determines whether the menu can be used now. Think of a Keymap as the container holding a hotkey registration.
Setting |
Determines |
Example |
|---|---|---|
Keymap |
The editor or mode in which the hotkey is registered |
Register an Object Mode operation |
Poll |
Whether the current state meets the requirements |
Available when the active object is a mesh |
The current state that Poll reads is the context. C.mode refers to the mode, C.active_object to the active object, and C.area to the editor area.
For example, register a menu in the Object Mode keymap and give it a Poll that requires an active mesh. Within the same mode, a mesh and a light produce different results. False is a normal result meaning that the condition is not met, not a code error.
Input reaching the registered Keymap scope and Poll returning True are separate requirements. Narrowing the target with Poll does not broaden the Keymap scope. Poll also checks availability when the menu is invoked by something other than a hotkey.
Common conditions¶
Each example is one line to paste into a menu’s Poll field. Choose the condition you need.
Required state |
Poll code |
|---|---|
Always available |
|
Object Mode |
|
Mesh Edit Mode |
|
An active object exists |
|
The active object is a mesh |
|
3D Viewport |
|
Check: Invoke the same menu in states that satisfy and do not satisfy the condition. For Mesh Edit Mode, test a mesh in Edit Mode and Object Mode. If the invocation does not reach the menu at all, also check the Keymap settings.
The + to the right of the Poll field opens a helper menu for adding common conditions. Review the resulting condition and test it in both applicable and inapplicable states.
Combine conditions¶
Syntax |
Meaning |
|---|---|
|
Both conditions are met |
|
Either condition is met |
|
The condition is not met |
|
Values are equal / not equal |
|
A value belongs to the specified candidates |
Make a menu available in Object Mode or Mesh Edit Mode:
return C.mode in {'OBJECT', 'EDIT_MESH'}
Make a menu available when a mesh is active in Object Mode:
obj = C.active_object; return obj is not None and obj.type == 'MESH' and C.mode == 'OBJECT'
Checking obj is not None first avoids reading obj.type when no target exists. Similarly, check the existence and type of C.area or C.space_data before reading editor-specific values.
Poll versus branching¶
Purpose |
Where to configure it |
|---|---|
Limit the states in which a menu is available |
That menu’s Poll |
Invoke different menus or actions from one key according to context |
|
Choose an operation when an item is executed |
A condition in Command |
Change UI content according to state |
Custom drawing code |
Poll is evaluated repeatedly. Read state and return a result; do not add objects or change settings. Passing Poll does not establish the execution requirements of Blender operators called inside the menu. For poll() failed, check operator context and timing.