Blender V4.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
14#include <Python.h>
15
16#include "BLI_utildefines.h"
17
18#include "WM_api.hh"
19#include "WM_types.hh"
20
21#include "RNA_access.hh"
22#include "RNA_define.hh"
23#include "RNA_prototypes.hh"
24
25#include "bpy_intern_string.hh"
26#include "bpy_operator_wrap.hh" /* own include */
27#include "bpy_rna.hh"
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 PyErr_Clear();
45 }
46
47 /* set the default property: ot->prop */
48 {
49 /* Picky developers will notice that 'bl_property' won't work with inheritance
50 * get direct from the dict to avoid raising a load of attribute errors (yes this isn't ideal)
51 * - campbell. */
52 PyObject *py_class_dict = py_class->tp_dict;
53 PyObject *bl_property = PyDict_GetItem(py_class_dict, bpy_intern_str_bl_property);
54 if (bl_property) {
55 const char *prop_id = PyUnicode_AsUTF8(bl_property);
56 if (prop_id != nullptr) {
57 PropertyRNA *prop;
58
59 PointerRNA ptr = RNA_pointer_create(nullptr, ot->srna, nullptr);
60 prop = RNA_struct_find_property(&ptr, prop_id);
61 if (prop) {
62 ot->prop = prop;
63 }
64 else {
65 PyErr_Format(
66 PyExc_ValueError, "%.200s.bl_property '%.200s' not found", ot->idname, prop_id);
67
68 /* this could be done cleaner, for now its OK */
69 PyErr_Print();
70 PyErr_Clear();
71 }
72 }
73 else {
74 PyErr_Format(PyExc_ValueError,
75 "%.200s.bl_property should be a string, not %.200s",
76 ot->idname,
77 Py_TYPE(bl_property)->tp_name);
78
79 /* this could be done cleaner, for now its OK */
80 PyErr_Print();
81 PyErr_Clear();
82 }
83 }
84 }
85 /* end 'ot->prop' assignment */
86}
87
89{
90 /* take care not to overwrite anything set in
91 * WM_operatortype_append_ptr before opfunc() is called */
92 StructRNA *srna = ot->srna;
93 *ot = *((wmOperatorType *)userdata);
94 ot->srna = srna; /* restore */
95
96 /* Use i18n context from rna_ext.srna if possible (py operators). */
97 if (ot->rna_ext.srna) {
99 }
100
102}
103
105{
106 wmOperatorType *data = (wmOperatorType *)userdata;
107
108 /* only copy a couple of things, the rest is set by the macro registration */
109 ot->name = data->name;
110 ot->idname = data->idname;
111 ot->description = data->description;
112 ot->flag |= data->flag; /* append flags to the one set by registration */
113 ot->pyop_poll = data->pyop_poll;
114 ot->ui = data->ui;
115 ot->rna_ext = data->rna_ext;
116
117 /* Use i18n context from rna_ext.srna if possible (py operators). */
118 if (ot->rna_ext.srna) {
120 }
121
123}
124
125PyObject *PYOP_wrap_macro_define(PyObject * /*self*/, PyObject *args)
126{
128 wmOperatorTypeMacro *otmacro;
129 PyObject *macro;
130 StructRNA *srna;
131
132 const char *idname_py;
133 char idname[OP_MAX_TYPENAME];
134
135 if (!PyArg_ParseTuple(args, "Os:_bpy.ops.macro_define", &macro, &idname_py)) {
136 return nullptr;
137 }
138
139 /* Support both `foo.bar` & `FOO_OT_bar`. */
140 WM_operator_bl_idname(idname, idname_py);
141 if (!WM_operator_bl_idname_is_valid(idname)) {
142 PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid operator id name", idname);
143 return nullptr;
144 }
145
146 /* identifiers */
147 srna = pyrna_struct_as_srna((PyObject *)macro, false, "Macro Define:");
148 if (srna == nullptr) {
149 return nullptr;
150 }
151
152 const char *macro_idname = RNA_struct_identifier(srna);
153 ot = WM_operatortype_find(macro_idname, true);
154
155 if (!ot) {
156 PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid macro", macro_idname);
157 return nullptr;
158 }
159
160 otmacro = WM_operatortype_macro_define(ot, idname);
161
162 PointerRNA ptr_otmacro = RNA_pointer_create(nullptr, &RNA_OperatorMacro, otmacro);
163 return pyrna_struct_CreatePyObject(&ptr_otmacro);
164}
#define OP_MAX_TYPENAME
PyObject * bpy_intern_str_bl_property
void BPY_RNA_operator_macro_wrapper(wmOperatorType *ot, void *userdata)
void BPY_RNA_operator_wrapper(wmOperatorType *ot, void *userdata)
static void operator_properties_init(wmOperatorType *ot)
PyObject * PYOP_wrap_macro_define(PyObject *, PyObject *args)
StructRNA * pyrna_struct_as_srna(PyObject *self, const bool parent, const char *error_prefix)
Definition bpy_rna.cc:8124
int pyrna_deferred_register_class(StructRNA *srna, PyTypeObject *py_class)
Definition bpy_rna.cc:8442
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:7694
void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
const char * RNA_struct_identifier(const StructRNA *type)
PointerRNA RNA_pointer_create(ID *id, StructRNA *type, void *data)
const char * RNA_struct_translation_context(const StructRNA *type)
void RNA_def_struct_identifier_no_struct_map(StructRNA *srna, const char *identifier)
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
StructRNA * srna
Definition RNA_types.hh:780
const char * name
Definition WM_types.hh:990
const char * idname
Definition WM_types.hh:992
bool(* pyop_poll)(bContext *C, wmOperatorType *ot) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1101
ExtensionRNA rna_ext
Definition WM_types.hh:1104
const char * description
Definition WM_types.hh:996
void(* ui)(bContext *C, wmOperator *op)
Definition WM_types.hh:1053
PropertyRNA * prop
Definition WM_types.hh:1092
StructRNA * srna
Definition WM_types.hh:1080
PointerRNA * ptr
Definition wm_files.cc:4126
wmOperatorType * ot
Definition wm_files.cc:4125
wmOperatorTypeMacro * WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
size_t WM_operator_bl_idname(char *dst, const char *src)
bool WM_operator_bl_idname_is_valid(const char *idname)