PyDoc: bpy.types.Operator docs - add note on execution context

Clarify the default behavior when invoke and execute is called.

Ref: !135854
This commit is contained in:
Andrej730
2025-03-26 16:51:40 +11:00
committed by Campbell Barton
parent 224b8bb116
commit ad5494d7ac
4 changed files with 19 additions and 2 deletions

View File

@@ -14,6 +14,10 @@ Common return values are ``{'FINISHED'}`` and ``{'CANCELLED'}``, the latter
meaning that the operator execution was aborted without making any changes or
saving an undo history entry.
If operator was cancelled but there wasn't any reports from it with ``{'ERROR'}`` type,
it will just return ``{'CANCELLED'}`` without raising any exceptions.
If it had error reports, then it will raise a ``RuntimeError`` including all report messages.
Calling an operator in the wrong context will raise a ``RuntimeError``,
there is a poll() method to avoid this problem.

View File

@@ -14,10 +14,12 @@ convention for menus.
.. note::
Menus have their :class:`Layout.operator_context` initialized as
'EXEC_REGION_WIN' rather than 'INVOKE_DEFAULT' (see :ref:`Execution Context <operator-execution_context>`).
Menus have their :class:`UILayout.operator_context` initialized as
'EXEC_REGION_WIN' rather than 'INVOKE_REGION_WIN' (see :ref:`Execution Context <operator-execution_context>`).
If the operator context needs to initialize inputs from the
:class:`Operator.invoke` function, then this needs to be explicitly set.
When a menu is added to UI elements such as a panel or header,
the operator execution context will be inherited from them.
"""
import bpy

View File

@@ -9,6 +9,13 @@ execute().
Some operators don't have an execute() function, removing the ability to be
repeated from a script or macro.
When an operator is called via :mod:`bpy.ops`, the execution context depends
on the argument provided to :mod:`bpy.ops`. By default, it uses execute().
When an operator is activated from a button or menu item, it follows
the setting in :class:`UILayout.operator_context`. In most cases, invoke() is used.
Running an operator via a key shortcut always uses invoke(),
and this behavior cannot be changed.
This example shows how to define an operator which gets mouse input to
execute a function and that this operator can be invoked or executed from
the python api.

View File

@@ -1706,6 +1706,10 @@ static void rna_def_ui_layout(BlenderRNA *brna)
RNA_def_property_enum_items(prop, rna_enum_operator_context_items);
RNA_def_property_enum_funcs(
prop, "rna_UILayout_op_context_get", "rna_UILayout_op_context_set", nullptr);
RNA_def_property_ui_text(prop,
"Operator Context",
"Typically set to 'INVOKE_REGION_WIN', except some cases "
"in :class:`bpy.types.Menu` when it's set to 'EXEC_REGION_WIN'.");
prop = RNA_def_property(srna, "enabled", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_funcs(prop, "rna_UILayout_enabled_get", "rna_UILayout_enabled_set");