Blender  V3.3
bpy_operator_wrap.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
12 #include <Python.h>
13 
14 #include "BLI_utildefines.h"
15 
16 #include "WM_api.h"
17 #include "WM_types.h"
18 
19 #include "RNA_access.h"
20 #include "RNA_define.h"
21 #include "RNA_prototypes.h"
22 
23 #include "bpy_intern_string.h"
24 #include "bpy_operator_wrap.h" /* own include */
25 #include "bpy_rna.h"
26 
28 {
29  PyTypeObject *py_class = ot->rna_ext.data;
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  /* Picky developers will notice that 'bl_property' won't work with inheritance
48  * get direct from the dict to avoid raising a load of attribute errors (yes this isn't ideal)
49  * - campbell. */
50  PyObject *py_class_dict = py_class->tp_dict;
51  PyObject *bl_property = PyDict_GetItem(py_class_dict, bpy_intern_str_bl_property);
52  const char *prop_id;
53  bool prop_raise_error;
54 
55  if (bl_property) {
56  if (PyUnicode_Check(bl_property)) {
57  /* since the property is explicitly given, raise an error if its not found */
58  prop_id = PyUnicode_AsUTF8(bl_property);
59  prop_raise_error = true;
60  }
61  else {
62  PyErr_Format(PyExc_ValueError,
63  "%.200s.bl_property should be a string, not %.200s",
64  ot->idname,
65  Py_TYPE(bl_property)->tp_name);
66 
67  /* this could be done cleaner, for now its OK */
68  PyErr_Print();
69  PyErr_Clear();
70 
71  prop_id = NULL;
72  prop_raise_error = false;
73  }
74  }
75  else {
76  /* fallback to hard-coded string (pre 2.66, could be deprecated) */
77  prop_id = "type";
78  prop_raise_error = false;
79  }
80 
81  if (prop_id) {
83  PropertyRNA *prop;
84 
86  prop = RNA_struct_find_property(&ptr, prop_id);
87  if (prop) {
88  ot->prop = prop;
89  }
90  else {
91  if (prop_raise_error) {
92  PyErr_Format(
93  PyExc_ValueError, "%.200s.bl_property '%.200s' not found", ot->idname, prop_id);
94 
95  /* this could be done cleaner, for now its OK */
96  PyErr_Print();
97  PyErr_Clear();
98  }
99  }
100  }
101  }
102  /* end 'ot->prop' assignment */
103 }
104 
106 {
107  /* take care not to overwrite anything set in
108  * WM_operatortype_append_ptr before opfunc() is called */
109  StructRNA *srna = ot->srna;
110  *ot = *((wmOperatorType *)userdata);
111  ot->srna = srna; /* restore */
112 
113  /* Use i18n context from rna_ext.srna if possible (py operators). */
114  if (ot->rna_ext.srna) {
116  }
117 
119 }
120 
122 {
123  wmOperatorType *data = (wmOperatorType *)userdata;
124 
125  /* only copy a couple of things, the rest is set by the macro registration */
126  ot->name = data->name;
127  ot->idname = data->idname;
128  ot->description = data->description;
129  ot->flag |= data->flag; /* append flags to the one set by registration */
130  ot->pyop_poll = data->pyop_poll;
131  ot->ui = data->ui;
132  ot->rna_ext = data->rna_ext;
133 
134  /* Use i18n context from rna_ext.srna if possible (py operators). */
135  if (ot->rna_ext.srna) {
137  }
138 
140 }
141 
142 PyObject *PYOP_wrap_macro_define(PyObject *UNUSED(self), PyObject *args)
143 {
145  wmOperatorTypeMacro *otmacro;
146  PyObject *macro;
147  PointerRNA ptr_otmacro;
148  StructRNA *srna;
149 
150  const char *opname;
151  const char *macroname;
152 
153  if (!PyArg_ParseTuple(args, "Os:_bpy.ops.macro_define", &macro, &opname)) {
154  return NULL;
155  }
156 
157  if (WM_operatortype_find(opname, true) == NULL) {
158  PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid operator id", opname);
159  return NULL;
160  }
161 
162  /* identifiers */
163  srna = pyrna_struct_as_srna((PyObject *)macro, false, "Macro Define:");
164  if (srna == NULL) {
165  return NULL;
166  }
167 
168  macroname = RNA_struct_identifier(srna);
169  ot = WM_operatortype_find(macroname, true);
170 
171  if (!ot) {
172  PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid macro", macroname);
173  return NULL;
174  }
175 
176  otmacro = WM_operatortype_macro_define(ot, opname);
177 
178  RNA_pointer_create(NULL, &RNA_OperatorMacro, otmacro, &ptr_otmacro);
179 
180  return pyrna_struct_CreatePyObject(&ptr_otmacro);
181 }
#define UNUSED(x)
PyObject * bpy_intern_str_bl_property
void BPY_RNA_operator_macro_wrapper(wmOperatorType *ot, void *userdata)
PyObject * PYOP_wrap_macro_define(PyObject *UNUSED(self), PyObject *args)
void BPY_RNA_operator_wrapper(wmOperatorType *ot, void *userdata)
static void operator_properties_init(wmOperatorType *ot)
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition: bpy_rna.c:7505
StructRNA * pyrna_struct_as_srna(PyObject *self, const bool parent, const char *error_prefix)
Definition: bpy_rna.c:7907
int pyrna_deferred_register_class(StructRNA *srna, PyTypeObject *py_class)
Definition: bpy_rna.c:8230
const char * RNA_struct_identifier(const StructRNA *type)
Definition: rna_access.c:586
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:136
void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
Definition: rna_access.c:902
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:717
const char * RNA_struct_translation_context(const StructRNA *type)
Definition: rna_access.c:619
void RNA_def_struct_identifier_no_struct_map(StructRNA *srna, const char *identifier)
Definition: rna_define.c:1227
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
Definition: rna_define.c:1250
StructRNA * srna
Definition: RNA_types.h:766
void * data
Definition: RNA_types.h:765
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
struct StructRNA * srna
Definition: WM_types.h:969
ExtensionRNA rna_ext
Definition: WM_types.h:993
const char * description
Definition: WM_types.h:893
void(* ui)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:954
bool(* pyop_poll)(struct bContext *, struct wmOperatorType *ot) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:990
PropertyRNA * prop
Definition: WM_types.h:981
PointerRNA * ptr
Definition: wm_files.c:3480
wmOperatorType * ot
Definition: wm_files.c:3479
wmOperatorTypeMacro * WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)