Blender V4.5
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
28{
29 PyTypeObject *py_class = static_cast<PyTypeObject *>(ot->rna_ext.data);
30 RNA_struct_blender_type_set(ot->rna_ext.srna, ot);
31
32 /* Only call this so pyrna_deferred_register_class gives a useful error
33 * WM_operatortype_append_ptr will call RNA_def_struct_identifier later.
34 *
35 * Note the 'no_struct_map' function is used since the actual struct name
36 * is already used by the operator.
37 */
39
40 if (pyrna_deferred_register_class(ot->srna, py_class) != 0) {
41 PyErr_Print(); /* failed to register operator props */
42 PyErr_Clear();
43 }
44
45 /* set the default property: ot->prop */
46 {
47 /* NOTE(@ideasman42): Picky developers will notice that `bl_property`
48 * won't work with inheritance get direct from the dict to avoid
49 * raising a load of attribute errors (yes this isn't ideal). */
50 PyObject *py_class_dict = py_class->tp_dict;
51 PyObject *bl_property = PyDict_GetItem(py_class_dict, bpy_intern_str_bl_property);
52 if (bl_property) {
53 const char *prop_id = PyUnicode_AsUTF8(bl_property);
54 if (prop_id != nullptr) {
55 PropertyRNA *prop;
56
57 PointerRNA ptr = RNA_pointer_create_discrete(nullptr, ot->srna, nullptr);
58 prop = RNA_struct_find_property(&ptr, prop_id);
59 if (prop) {
60 ot->prop = prop;
61 }
62 else {
63 PyErr_Format(
64 PyExc_ValueError, "%.200s.bl_property '%.200s' not found", ot->idname, prop_id);
65
66 /* this could be done cleaner, for now its OK */
67 PyErr_Print();
68 PyErr_Clear();
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 PyErr_Clear();
80 }
81 }
82 }
83 /* end 'ot->prop' assignment */
84}
85
87{
88 /* take care not to overwrite anything set in
89 * WM_operatortype_append_ptr before opfunc() is called */
90 StructRNA *srna = ot->srna;
91 *ot = *((wmOperatorType *)userdata);
92 ot->srna = srna; /* restore */
93
94 /* Use i18n context from rna_ext.srna if possible (py operators). */
95 if (ot->rna_ext.srna) {
97 }
98
100}
101
103{
104 wmOperatorType *data = (wmOperatorType *)userdata;
105
106 /* only copy a couple of things, the rest is set by the macro registration */
107 ot->name = data->name;
108 ot->idname = data->idname;
109 ot->description = data->description;
110 ot->flag |= data->flag; /* append flags to the one set by registration */
111 ot->pyop_poll = data->pyop_poll;
112 ot->ui = data->ui;
113 ot->rna_ext = data->rna_ext;
114
115 /* Use i18n context from rna_ext.srna if possible (py operators). */
116 if (ot->rna_ext.srna) {
118 }
119
121}
122
123PyObject *PYOP_wrap_macro_define(PyObject * /*self*/, PyObject *args)
124{
126 wmOperatorTypeMacro *otmacro;
127 PyObject *macro;
128 StructRNA *srna;
129
130 const char *idname_py;
131 char idname[OP_MAX_TYPENAME];
132
133 if (!PyArg_ParseTuple(args, "Os:_bpy.ops.macro_define", &macro, &idname_py)) {
134 return nullptr;
135 }
136
137 /* Support both `foo.bar` & `FOO_OT_bar`. */
138 WM_operator_bl_idname(idname, idname_py);
139 if (!WM_operator_bl_idname_is_valid(idname)) {
140 PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid operator id name", idname);
141 return nullptr;
142 }
143
144 /* identifiers */
145 srna = pyrna_struct_as_srna(macro, false, "Macro Define:");
146 if (srna == nullptr) {
147 return nullptr;
148 }
149
150 const char *macro_idname = RNA_struct_identifier(srna);
151 ot = WM_operatortype_find(macro_idname, true);
152
153 if (!ot) {
154 PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid macro", macro_idname);
155 return nullptr;
156 }
157
158 otmacro = WM_operatortype_macro_define(ot, idname);
159
160 PointerRNA ptr_otmacro = RNA_pointer_create_discrete(nullptr, &RNA_OperatorMacro, otmacro);
161 return pyrna_struct_CreatePyObject(&ptr_otmacro);
162}
#define OP_MAX_TYPENAME
BMesh const char void * data
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:8849
int pyrna_deferred_register_class(StructRNA *srna, PyTypeObject *py_class)
Definition bpy_rna.cc:9173
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:8386
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_discrete(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)
PointerRNA * ptr
Definition wm_files.cc:4227
wmOperatorType * ot
Definition wm_files.cc:4226
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)