32#include "RNA_prototypes.hh"
65#define BPY_RELEASE_GIL
117 const char *C_exec =
"EXEC_DEFAULT";
119 bool is_exec =
false;
120 bool is_undo_set =
false;
122 Py_ssize_t args_len = PyTuple_GET_SIZE(args);
123 for (Py_ssize_t i = 0; i < args_len; i++) {
124 PyObject *arg = PyTuple_GET_ITEM(args, i);
126 if (!is_exec && PyUnicode_Check(arg)) {
128 PyErr_SetString(PyExc_ValueError,
"string arg must come before the boolean");
131 C_exec = PyUnicode_AsUTF8(arg);
134 else if ((r_is_undo !=
nullptr) && (!is_undo_set && (PyBool_Check(arg) || PyLong_Check(arg))))
136 C_undo = PyObject_IsTrue(arg);
140 PyErr_SetString(PyExc_ValueError,
"1-2 args execution context is supported");
145 *r_context_str = C_exec;
168 PyErr_SetString(PyExc_RuntimeError,
"Context is None, cannot poll any operators");
175 PyErr_Format(PyExc_AttributeError,
176 "Polling operator \"bpy.ops.%s\" error, "
177 "could not be found",
183 int context_int = int(context);
187 PyErr_Format(PyExc_TypeError,
188 "Calling operator \"bpy.ops.%s.poll\" error, "
189 "expected a string enum in (%s)",
192 MEM_delete(enum_str);
202 return Py_NewRef(
ret);
208 ".. method:: poll(context='EXEC_DEFAULT', /)\n"
210 "Test if the operator can be executed in the current context.\n"
212 ":param context: Execution context (optional)\n"
213 ":type context: str\n"
214 ":return: True if the operator can be executed\n"
218 const char *context_str;
234 const char *context_str,
249 PyErr_SetString(PyExc_RuntimeError,
"Context is None, cannot poll any operators");
256 PyErr_Format(PyExc_AttributeError,
257 "Calling operator \"bpy.ops.%s\" error, "
258 "could not be found",
264 PyErr_Format(PyExc_RuntimeError,
265 "Calling operator \"bpy.ops.%s\" error, "
266 "cannot modify blend data in this state (drawing/rendering)",
272 int context_int = int(context);
276 PyErr_Format(PyExc_TypeError,
277 "Calling operator \"bpy.ops.%s\" error, "
278 "expected a string enum in (%s)",
281 MEM_delete(enum_str);
289 bool msg_free =
false;
291 PyErr_Format(PyExc_RuntimeError,
292 "Operator bpy.ops.%.200s.poll() %.200s",
294 msg ? msg :
"failed, context is incorrect");
305 if (kw && PyDict_Size(kw)) {
307 &ptr, kw,
false,
"Converting py args to operator properties:");
310 if (error_val == 0) {
313 reports = MEM_new<ReportList>(
"wmOperatorReportList");
318#ifdef BPY_RELEASE_GIL
324 PyThreadState *ts = PyEval_SaveThread();
329#ifdef BPY_RELEASE_GIL
331 PyEval_RestoreThread(ts);
338 if (!reports->
list.is_empty()) {
365 PyErr_Format(PyExc_AttributeError,
"Operator \"%s\" does not take any args", opname);
374 if (error_val == -1) {
390 if (
C ==
nullptr) [[unlikely]] {
391 PyErr_SetString(PyExc_RuntimeError,
"Context is None, cannot call an operator");
398 PyErr_SetString(PyExc_TypeError,
"keywords must be strings");
407 const char *context_str;
422 self->
idname, kwargs, context_str, is_undo);
454 PyErr_SetString(PyExc_RuntimeError,
455 "Context is None, cannot get the string representation of this object.");
462 PyErr_Format(PyExc_AttributeError,
463 "_bpy.ops.as_string: operator \"%.200s\" "
464 "could not be found",
473 if (kw && PyDict_Size(kw)) {
475 &ptr, kw,
false,
"Converting py args to operator properties:");
478 std::string op_string;
479 if (error_val == 0) {
485 if (error_val == -1) {
507 PyErr_Format(PyExc_KeyError,
"get_rna_type(\"%s\") not found", opname);
513 return reinterpret_cast<PyObject *
>(pyrna);
518 bpy_op_fn_get_rna_type_doc,
519 ".. method:: get_rna_type()\n"
521 "Get the RNA type definition for this operator.\n"
523 ":return: RNA type object for introspection\n"
524 ":rtype: :class:`bpy.types.Struct`\n");
532 const char *opname = PyUnicode_AsUTF8(value);
533 if (opname ==
nullptr) {
534 PyErr_Format(PyExc_TypeError,
"get_rna_type() expects a string argument");
550 PyErr_Format(PyExc_KeyError,
"get_bl_options(\"%s\") not found", opname);
558 bpy_op_fn_get_bl_options_doc,
559 "Set of option flags for this operator (e.g. 'REGISTER', 'UNDO').\n"
561 ":type: set[str]\n");
575 Py_TYPE(self)->tp_free(
reinterpret_cast<PyObject *
>(self));
587 const char *dot_pos = strchr(idname_py,
'.');
589 return PyUnicode_FromFormat(
590 "<function bpy.ops.%s at %p>", idname_py,
static_cast<void *
>(self));
593 size_t op_mod_str_len = dot_pos - idname_py;
598 if (op_mod_str_len >=
sizeof(op_mod_str)) {
600 op_mod_str_len =
sizeof(op_mod_str) - 1;
602 memcpy(op_mod_str, idname_py, op_mod_str_len);
603 op_mod_str[op_mod_str_len] =
'\0';
604 STRNCPY(op_fn_str, dot_pos + 1);
606 return PyUnicode_FromFormat(
607 "<function bpy.ops.%s.%s at %p>", op_mod_str, op_fn_str,
static_cast<void *
>(self));
622 return PyUnicode_FromFormat(
"<bpy.ops.%s function>", idname_py);
636 bpy_op_fn_idname_doc,
637 ".. method:: idname()\n"
639 ":return: Return the Blender-format operator idname (e.g., 'OBJECT_OT_select_all').\n"
643 return PyUnicode_FromString(self->
idname);
648 bpy_op_fn_idname_py_doc,
649 ".. method:: idname_py()\n"
651 ":return: Return the Python-format operator idname (e.g., 'object.select_all').\n"
657 return PyUnicode_FromString(idname_py);
671 return PyUnicode_FromFormat(
"bpy.ops.%s(...)", idname_py);
683 PyObject *description = PyObject_GetAttrString(rna_type,
"description");
692 PyObject *combined = PyUnicode_FromFormat(
"%U\n%U", sig_result, description);
693 Py_DECREF(sig_result);
694 Py_DECREF(description);
699 return PyUnicode_FromFormat(
"bpy.ops.%s(...)", idname_py);
707 {
"poll",
reinterpret_cast<PyCFunction
>(
bpy_op_fn_poll), METH_VARARGS, bpy_op_fn_poll_doc},
711 bpy_op_fn_get_rna_type_doc},
712 {
"idname",
reinterpret_cast<PyCFunction
>(
bpy_op_fn_idname), METH_NOARGS, bpy_op_fn_idname_doc},
716 bpy_op_fn_idname_py_doc},
717 {
nullptr,
nullptr, 0,
nullptr}};
734 bpy_op_fn_get_bl_options_doc,
737 {
"__doc__",
reinterpret_cast<getter
>(
bpy_op_fn_get_doc),
nullptr,
nullptr,
nullptr},
738 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}};
749 "BPyOpFunction(context='EXEC_DEFAULT', undo=False, **kwargs)\n"
751 " Execute the operator with the given parameters.\n"
753 " :param context: Execution context (optional)\n"
754 " :type context: str\n"
755 " :param undo: Force undo behavior (optional)\n"
756 " :type undo: bool\n"
757 " :param kwargs: Operator properties\n"
758 " :return: Set of completion status flags\n"
759 " :rtype: set[str]\n");
761 PyVarObject_HEAD_INIT(
nullptr, 0)
777 PyObject_GenericGetAttr,
828 const char *op_mod_str, *op_fn_str;
830 if (!PyArg_ParseTuple(args,
841 size_t bl_len = strlen(op_mod_str) + 4 + strlen(op_fn_str) + 1;
843 PyErr_Format(PyExc_ValueError,
"Operator name too long: %s.%s", op_mod_str, op_fn_str);
856 STRNCPY(op_mod_str_upper, op_mod_str);
859 const size_t idname_len =
SNPRINTF(op_fn->
idname,
"%s_OT_%s", op_mod_str_upper, op_fn_str);
863 return reinterpret_cast<PyObject *
>(op_fn);
#define SNPRINTF(dst, format,...)
Read Blender memory allocation and freeing.
#define UNUSED_VARS_NDEBUG(...)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static PyObject * bpy_op_fn_get_doc(BPyOpFunction *self, void *)
static const char * opname_as_py(const char *opname, char opname_buf[OP_MAX_TYPENAME])
char * STRNCPY(char(&dst)[N], const char *src)
void BLI_str_toupper_ascii(char *str, size_t len) ATTR_NONNULL(1)
static PyObject * bpy_op_fn_get_bl_options_impl(const char *opname)
void BPY_modules_update()
void WM_operator_properties_sanitize(PointerRNA *ptr, const bool no_context)
void BKE_reports_free(ReportList *reports)
void BKE_report_print_level_set(ReportList *reports, eReportType level)
void WM_operator_properties_free(PointerRNA *ptr)
void BKE_reports_init(ReportList *reports, int flag)
void BPy_reports_write_stdout(const ReportList *reports, const char *header)
int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, const bool all_args, const char *error_prefix)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
static PyObject * bpy_op_fn_as_string(BPyOpFunction *self)
static PyGetSetDef bpy_op_fn_getsetters[]
void BKE_reports_clear(ReportList *reports)
void DEG_make_active(Depsgraph *depsgraph)
static PyObject * bpy_op_fn_repr(BPyOpFunction *self)
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
ViewLayer * CTX_data_view_layer(const bContext *C)
static PyObject * bpy_op_fn_get_rna_type_impl(const char *opname)
bool RNA_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *r_value)
const EnumPropertyItem rna_enum_operator_return_items[]
wmOperatorStatus WM_operator_call_py(bContext *C, wmOperatorType *ot, wm::OpCallContext context, PointerRNA *properties, ReportList *reports, const bool is_undo)
std::string WM_operator_pystring_ex(bContext *C, wmOperator *op, const bool all_args, const bool macro_args, wmOperatorType *ot, PointerRNA *opptr)
bool PyC_Dict_CheckKeysAreStrings(PyObject *dict)
struct bContext * BPY_context_get()
const EnumPropertyItem rna_enum_operator_context_items[]
wmWindowManager * CTX_wm_manager(const bContext *C)
static PyObject * bpy_op_fn_as_string_impl(const char *opname, PyObject *kw, bool all_args, bool macro_args)
static void bpy_op_fn_dealloc(BPyOpFunction *self)
PyObject * pyop_create_function(PyObject *, PyObject *args)
bool BKE_main_view_layers_synced_ensure(const Main *bmain)
void BKE_scene_graph_update_tagged(Depsgraph *depsgraph, Main *bmain)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
int BPyOpFunction_InitTypes()
const char * CTX_wm_operator_poll_msg_get(bContext *C, bool *r_free)
bool WM_operator_poll_context(bContext *C, wmOperatorType *ot, wm::OpCallContext context)
wmOperatorStatus WM_operator_name_call(bContext *C, const char *opstring, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
static PyObject * bpy_op_fn_idname_py(BPyOpFunction *self, PyObject *)
static PyObject * bpy_op_fn_call(BPyOpFunction *self, PyObject *args, PyObject *kwargs)
static void bpy_op_view_layer_update(bContext *C)
static PyTypeObject BPyOpFunctionType
static PyObject * bpy_op_fn_idname(BPyOpFunction *self, PyObject *)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
PyObject * pyrna_enum_bitfield_as_set(const EnumPropertyItem *items, int value)
static PyObject * bpy_op_fn_get_bl_options(BPyOpFunction *self, void *)
PyObject * pyop_getrna_type(PyObject *, PyObject *value)
Main * CTX_data_main(const bContext *C)
static PyObject * bpy_op_fn_poll(BPyOpFunction *self, PyObject *args)
const EnumPropertyItem rna_enum_operator_type_flag_items[]
static bool bpy_op_parse_args(PyObject *args, const char **r_context_str, bool *r_is_undo)
PyObject * PyC_UnicodeFromStdStr(const std::string &str)
static PyObject * bpy_op_fn_get_rna_type(BPyOpFunction *self, PyObject *)
Scene * CTX_data_scene(const bContext *C)
Depsgraph * BKE_scene_ensure_depsgraph(Main *bmain, Scene *scene, ViewLayer *view_layer)
short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear)
static PyObject * bpy_op_fn_poll_impl(const char *opname, const char *context_str)
@ RPT_PRINT_HANDLED_BY_OWNER
char * pyrna_enum_repr(const EnumPropertyItem *item)
static PyObject * bpy_op_fn_str(BPyOpFunction *self)
static PyMethodDef bpy_op_fn_methods[]
static std::optional< wmOperatorStatus > bpy_op_fn_call_impl(const char *opname, PyObject *kw, const char *context_str, bool is_undo)
bool DEG_is_evaluating(const Depsgraph *depsgraph)
size_t WM_operator_py_idname(char *dst, const char *src)
void CTX_wm_operator_poll_msg_clear(bContext *C)
static PyObject * bpy_op_fn_get_doc_impl(BPyOpFunction *self)
header-only compatibility defines.
PyObject_HEAD char idname[OP_MAX_TYPENAME]
ListBaseT< Scene > scenes
ListBaseT< ViewLayer > view_layers