Files
test2/source/blender/python/intern/bpy_operator_function.hh
Nick Alberelli 877283a09a PyAPI: bpy.ops callable type in the C-API
Moving `__call__` logic into Python's C-API means error messages
caused by operators reference the code of the caller instead of the
call from `.scripts/modules/bpy/ops.py`.

When a full call stack is available this isn't so important,
however when logging callers it's not useful to log the call
from `ops.py`.

There minor performance advantages for moving from Python to C++
however this wasn't a significant consideration for the change.

Note that this is a fairly direct conversion from Python to C++,
there is room for refactoring to simplify the calling logic.

See #144746 for the motivation & #147448 for further improvements.

Ref !145344
2025-10-11 03:04:57 +00:00

58 lines
1.6 KiB
C++

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup pythonintern
*
* This file implements the #BPyOpFunction type,
* a Python C-API implementation of a callable Blender operator.
*/
#pragma once
#include <Python.h>
#include "DNA_windowmanager_types.h"
/**
* A callable operator.
*
* Exposed by `bpy.ops.{module}.{operator}()` to allow Blender operators to be called from Python.
*/
typedef struct {
PyObject_HEAD
/** Operator ID name (e.g., `OBJECT_OT_select_all`). */
char idname[OP_MAX_TYPENAME];
} BPyOpFunction;
extern PyTypeObject BPyOpFunctionType;
#define BPyOpFunction_Check(v) (PyObject_TypeCheck(v, &BPyOpFunctionType))
#define BPyOpFunction_CheckExact(v) (Py_TYPE(v) == &BPyOpFunctionType)
/* Forward declarations for external functions from `bpy_operator.cc`. */
PyObject *pyop_poll(PyObject *self, PyObject *args);
PyObject *pyop_call(PyObject *self, PyObject *args);
PyObject *pyop_as_string(PyObject *self, PyObject *args);
PyObject *pyop_getrna_type(PyObject *self, PyObject *value);
PyObject *pyop_get_bl_options(PyObject *self, PyObject *value);
/**
* Create a new BPyOpFunction object for the given operator module and function.
*
* \param self: Unused (required by Python C API).
* \param args: Python tuple containing module and function name strings.
* \return A new #BPyOpFunction object or NULL on error.
*/
PyObject *pyop_create_function(PyObject *self, PyObject *args);
/**
* Initialize the BPyOpFunction type.
* This must be called before using any BPyOpFunction functions.
*
* \return 0 on success, -1 on failure
*/
int BPyOpFunction_InitTypes();