Blender  V2.93
bpy_operator_wrap.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
26 #include <Python.h>
27 
28 #include "BLI_utildefines.h"
29 
30 #include "WM_api.h"
31 #include "WM_types.h"
32 
33 #include "RNA_access.h"
34 #include "RNA_define.h"
35 
36 #include "bpy_intern_string.h"
37 #include "bpy_operator_wrap.h" /* own include */
38 #include "bpy_rna.h"
39 
41 {
42  PyTypeObject *py_class = ot->rna_ext.data;
44 
45  /* Only call this so pyrna_deferred_register_class gives a useful error
46  * WM_operatortype_append_ptr will call RNA_def_struct_identifier later.
47  *
48  * Note the 'no_struct_map' function is used since the actual struct name
49  * is already used by the operator.
50  */
52 
53  if (pyrna_deferred_register_class(ot->srna, py_class) != 0) {
54  PyErr_Print(); /* failed to register operator props */
55  PyErr_Clear();
56  }
57 
58  /* set the default property: ot->prop */
59  {
60  /* Picky developers will notice that 'bl_property' won't work with inheritance
61  * get direct from the dict to avoid raising a load of attribute errors (yes this isn't ideal)
62  * - campbell. */
63  PyObject *py_class_dict = py_class->tp_dict;
64  PyObject *bl_property = PyDict_GetItem(py_class_dict, bpy_intern_str_bl_property);
65  const char *prop_id;
66  bool prop_raise_error;
67 
68  if (bl_property) {
69  if (PyUnicode_Check(bl_property)) {
70  /* since the property is explicitly given, raise an error if its not found */
71  prop_id = PyUnicode_AsUTF8(bl_property);
72  prop_raise_error = true;
73  }
74  else {
75  PyErr_Format(PyExc_ValueError,
76  "%.200s.bl_property should be a string, not %.200s",
77  ot->idname,
78  Py_TYPE(bl_property)->tp_name);
79 
80  /* this could be done cleaner, for now its OK */
81  PyErr_Print();
82  PyErr_Clear();
83 
84  prop_id = NULL;
85  prop_raise_error = false;
86  }
87  }
88  else {
89  /* fallback to hard-coded string (pre 2.66, could be deprecated) */
90  prop_id = "type";
91  prop_raise_error = false;
92  }
93 
94  if (prop_id) {
96  PropertyRNA *prop;
97 
99  prop = RNA_struct_find_property(&ptr, prop_id);
100  if (prop) {
101  ot->prop = prop;
102  }
103  else {
104  if (prop_raise_error) {
105  PyErr_Format(
106  PyExc_ValueError, "%.200s.bl_property '%.200s' not found", ot->idname, prop_id);
107 
108  /* this could be done cleaner, for now its OK */
109  PyErr_Print();
110  PyErr_Clear();
111  }
112  }
113  }
114  }
115  /* end 'ot->prop' assignment */
116 }
117 
123 {
124  /* take care not to overwrite anything set in
125  * WM_operatortype_append_ptr before opfunc() is called */
126  StructRNA *srna = ot->srna;
127  *ot = *((wmOperatorType *)userdata);
128  ot->srna = srna; /* restore */
129 
130  /* Use i18n context from rna_ext.srna if possible (py operators). */
131  if (ot->rna_ext.srna) {
133  }
134 
136 }
137 
143 {
144  wmOperatorType *data = (wmOperatorType *)userdata;
145 
146  /* only copy a couple of things, the rest is set by the macro registration */
147  ot->name = data->name;
148  ot->idname = data->idname;
149  ot->description = data->description;
150  ot->flag |= data->flag; /* append flags to the one set by registration */
151  ot->pyop_poll = data->pyop_poll;
152  ot->ui = data->ui;
153  ot->rna_ext = data->rna_ext;
154 
155  /* Use i18n context from rna_ext.srna if possible (py operators). */
156  if (ot->rna_ext.srna) {
158  }
159 
161 }
162 
163 PyObject *PYOP_wrap_macro_define(PyObject *UNUSED(self), PyObject *args)
164 {
166  wmOperatorTypeMacro *otmacro;
167  PyObject *macro;
168  PointerRNA ptr_otmacro;
169  StructRNA *srna;
170 
171  const char *opname;
172  const char *macroname;
173 
174  if (!PyArg_ParseTuple(args, "Os:_bpy.ops.macro_define", &macro, &opname)) {
175  return NULL;
176  }
177 
178  if (WM_operatortype_find(opname, true) == NULL) {
179  PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid operator id", opname);
180  return NULL;
181  }
182 
183  /* identifiers */
184  srna = pyrna_struct_as_srna((PyObject *)macro, false, "Macro Define:");
185  if (srna == NULL) {
186  return NULL;
187  }
188 
189  macroname = RNA_struct_identifier(srna);
190  ot = WM_operatortype_find(macroname, true);
191 
192  if (!ot) {
193  PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid macro", macroname);
194  return NULL;
195  }
196 
197  otmacro = WM_operatortype_macro_define(ot, opname);
198 
199  RNA_pointer_create(NULL, &RNA_OperatorMacro, otmacro, &ptr_otmacro);
200 
201  return pyrna_struct_CreatePyObject(&ptr_otmacro);
202 }
#define UNUSED(x)
StructRNA RNA_OperatorMacro
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:7469
StructRNA * pyrna_struct_as_srna(PyObject *self, const bool parent, const char *error_prefix)
Definition: bpy_rna.c:7849
int pyrna_deferred_register_class(StructRNA *srna, PyTypeObject *py_class)
Definition: bpy_rna.c:8122
const char * RNA_struct_identifier(const StructRNA *type)
Definition: rna_access.c:723
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:146
void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
Definition: rna_access.c:1044
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:866
const char * RNA_struct_translation_context(const StructRNA *type)
Definition: rna_access.c:756
void RNA_def_struct_identifier_no_struct_map(StructRNA *srna, const char *identifier)
Definition: rna_define.c:1249
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
Definition: rna_define.c:1272
StructRNA * srna
Definition: RNA_types.h:681
void * data
Definition: RNA_types.h:680
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
struct StructRNA * srna
Definition: WM_types.h:802
ExtensionRNA rna_ext
Definition: WM_types.h:826
const char * description
Definition: WM_types.h:726
void(* ui)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:787
bool(* pyop_poll)(struct bContext *, struct wmOperatorType *ot) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:823
PropertyRNA * prop
Definition: WM_types.h:814
PointerRNA * ptr
Definition: wm_files.c:3157
wmOperatorType * ot
Definition: wm_files.c:3156
wmOperatorTypeMacro * WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)