Blender V5.3
bpy_operator_wrap.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#include <Python.h>
15
16#include "WM_api.hh"
17#include "WM_types.hh"
18
19#include "RNA_access.hh"
20#include "RNA_define.hh"
21#include "RNA_prototypes.hh"
22
23#include "bpy_intern_string.hh"
24#include "bpy_operator_wrap.hh" /* own include */
25#include "bpy_rna.hh"
26
27namespace blender {
28
30{
31 PyTypeObject *py_class = static_cast<PyTypeObject *>(ot->rna_ext.data);
33
34 /* Only call this so pyrna_deferred_register_class gives a useful error
35 * WM_operatortype_append_ptr will call RNA_def_struct_identifier later.
36 *
37 * Note the 'no_struct_map' function is used since the actual struct name
38 * is already used by the operator.
39 */
41
42 if (pyrna_deferred_register_class(ot->srna, py_class) != 0) {
43 PyErr_Print(); /* failed to register operator props */
44 }
45
46 /* set the default property: ot->prop */
47 {
48 /* NOTE(@ideasman42): Picky developers will notice that `bl_property`
49 * won't work with inheritance get direct from the dict to avoid
50 * raising a load of attribute errors (yes this isn't ideal). */
51 PyObject *py_class_dict = py_class->tp_dict;
52 PyObject *bl_property = PyDict_GetItem(py_class_dict, bpy_intern_str_bl_property);
53 if (bl_property) {
54 const char *prop_id = PyUnicode_AsUTF8(bl_property);
55 if (prop_id != nullptr) {
56 PropertyRNA *prop;
57
58 PointerRNA ptr = RNA_pointer_create_discrete(nullptr, ot->srna, nullptr);
59 prop = RNA_struct_find_property(&ptr, prop_id);
60 if (prop) {
61 ot->prop = prop;
62 }
63 else {
64 PyErr_Format(
65 PyExc_ValueError, "%.200s.bl_property '%.200s' not found", ot->idname, prop_id);
66
67 /* this could be done cleaner, for now its OK */
68 PyErr_Print();
69 }
70 }
71 else {
72 PyErr_Format(PyExc_ValueError,
73 "%.200s.bl_property should be a string, not %.200s",
74 ot->idname,
75 Py_TYPE(bl_property)->tp_name);
76
77 /* this could be done cleaner, for now its OK */
78 PyErr_Print();
79 }
80 }
81 }
82 /* end 'ot->prop' assignment */
83}
84
86{
87 /* take care not to overwrite anything set in
88 * WM_operatortype_append_ptr before opfunc() is called */
89 StructRNA *srna = ot->srna;
90 *ot = std::move(*(static_cast<wmOperatorType *>(userdata)));
91 ot->srna = srna; /* restore */
92
93 /* Use i18n context from rna_ext.srna if possible (py operators). */
94 if (ot->rna_ext.srna) {
96 }
97
99}
100
102{
103 wmOperatorType *data = static_cast<wmOperatorType *>(userdata);
104
105 /* only copy a couple of things, the rest is set by the macro registration */
106 ot->name = data->name;
107 ot->idname = data->idname;
108 ot->description = data->description;
109 ot->flag |= data->flag; /* append flags to the one set by registration */
110 ot->pyop_poll = data->pyop_poll;
111 ot->ui = data->ui;
112 ot->rna_ext = data->rna_ext;
113
114 /* Use i18n context from rna_ext.srna if possible (py operators). */
115 if (ot->rna_ext.srna) {
117 }
118
120}
121
122PyObject *PYOP_wrap_macro_define(PyObject * /*self*/, PyObject *args)
123{
124 wmOperatorType *ot;
125 wmOperatorTypeMacro *otmacro;
126 PyObject *macro;
127 StructRNA *srna;
128
129 const char *idname_py;
130 char idname[OP_MAX_TYPENAME];
131
132 if (!PyArg_ParseTuple(args,
133 "O" /* `macro` */
134 "s" /* `idname` */
135 ":_bpy.ops.macro_define",
136 &macro,
137 &idname_py))
138 {
139 return nullptr;
140 }
141
142 /* Support both `foo.bar` & `FOO_OT_bar`. */
143 WM_operator_bl_idname(idname, idname_py);
144 if (!WM_operator_bl_idname_is_valid(idname)) {
145 PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid operator id name", idname);
146 return nullptr;
147 }
148
149 /* identifiers */
150 srna = pyrna_struct_as_srna(macro, false, "Macro Define:");
151 if (srna == nullptr) {
152 return nullptr;
153 }
154
155 const char *macro_idname = RNA_struct_identifier(srna);
156 ot = WM_operatortype_find(macro_idname, true);
157
158 if (!ot) {
159 PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid macro", macro_idname);
160 return nullptr;
161 }
162
163 otmacro = WM_operatortype_macro_define(ot, idname);
164
165 PointerRNA ptr_otmacro = RNA_pointer_create_discrete(nullptr, RNA_OperatorMacro, otmacro);
166 return pyrna_struct_CreatePyObject(&ptr_otmacro);
167}
168
169} // namespace blender
#define OP_MAX_TYPENAME
BMesh Iterators.
static void operator_properties_init(wmOperatorType *ot)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool WM_operator_bl_idname_is_valid(const char *idname)
wmOperatorTypeMacro * WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
size_t WM_operator_bl_idname(char *dst, const char *src)
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:8874
void RNA_def_struct_identifier_no_struct_map(StructRNA *srna, const char *identifier)
void BPY_RNA_operator_macro_wrapper(wmOperatorType *ot, void *userdata)
BMesh const char void * data
PyObject * PYOP_wrap_macro_define(PyObject *, PyObject *args)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
const char * RNA_struct_identifier(const StructRNA *type)
int pyrna_deferred_register_class(StructRNA *srna, PyTypeObject *py_class)
Definition bpy_rna.cc:9663
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
StructRNA * pyrna_struct_as_srna(PyObject *self, const bool parent, const char *error_prefix)
Definition bpy_rna.cc:9338
void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
void BPY_RNA_operator_wrapper(wmOperatorType *ot, void *userdata)
PyObject * bpy_intern_str_bl_property
const char * RNA_struct_translation_context(const StructRNA *type)
const char * description
Definition WM_types.hh:1115
bool(*) pyop_poll(bContext *C, wmOperatorType *ot) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1224
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176