PyAPI: fixes for defining operator macros

- Support passing in operator ID's using Python dot syntax.
- Support define operators that haven't yet been registered
  matching Blender's own behavior.

Also add doc-string for bpy.types.Macro.define.
This commit is contained in:
Campbell Barton 2024-06-01 16:17:02 +10:00
parent a700fdc163
commit d002703484
3 changed files with 26 additions and 12 deletions

@ -966,9 +966,17 @@ class Macro(StructRNA):
__slots__ = ()
@classmethod
def define(cls, opname):
def define(cls, operator):
"""
Append an operator to a registered macro class.
:arg operator: Identifier of the operator. This does not have to be defined when this function is called.
:type operator: string
:return: The operator macro for property access.
:rtype: :class:`OperatorMacro`
"""
from _bpy import ops
return ops.macro_define(cls, opname)
return ops.macro_define(cls, operator)
class PropertyGroup(StructRNA, metaclass=RNAMetaPropGroup):

@ -129,15 +129,17 @@ PyObject *PYOP_wrap_macro_define(PyObject * /*self*/, PyObject *args)
PyObject *macro;
StructRNA *srna;
const char *opname;
const char *macroname;
const char *idname_py;
char idname[OP_MAX_TYPENAME];
if (!PyArg_ParseTuple(args, "Os:_bpy.ops.macro_define", &macro, &opname)) {
if (!PyArg_ParseTuple(args, "Os:_bpy.ops.macro_define", &macro, &idname_py)) {
return nullptr;
}
if (WM_operatortype_find(opname, true) == nullptr) {
PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid operator id", opname);
/* Support both `foo.bar` & `FOO_OT_bar`. */
WM_operator_bl_idname(idname, idname_py);
if (!WM_operator_bl_idname_is_valid(idname)) {
PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid operator id name", idname);
return nullptr;
}
@ -147,15 +149,15 @@ PyObject *PYOP_wrap_macro_define(PyObject * /*self*/, PyObject *args)
return nullptr;
}
macroname = RNA_struct_identifier(srna);
ot = WM_operatortype_find(macroname, true);
const char *macro_idname = RNA_struct_identifier(srna);
ot = WM_operatortype_find(macro_idname, true);
if (!ot) {
PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid macro", macroname);
PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid macro", macro_idname);
return nullptr;
}
otmacro = WM_operatortype_macro_define(ot, opname);
otmacro = WM_operatortype_macro_define(ot, idname);
PointerRNA ptr_otmacro = RNA_pointer_create(nullptr, &RNA_OperatorMacro, otmacro);
return pyrna_struct_CreatePyObject(&ptr_otmacro);

@ -14,7 +14,11 @@ struct wmOperatorType;
extern "C" {
#endif
/** These are used for operator methods, used by `bpy_operator.cc`. */
/**
* These are used for operator methods, used by `bpy_operator.cc`.
*
* Accessed via sub-classes of `bpy.types.Macro` using the `define` method.
*/
PyObject *PYOP_wrap_macro_define(PyObject *self, PyObject *args);
/* Exposed to RNA/WM API. */