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
17 /** \file
18 * \ingroup pythonintern
19 *
20 * This file is so python can define operators that C can call into.
21 * The generic callback functions for python operators are defines in
22 * 'rna_wm.c', some calling into functions here to do python specific
23 * functionality.
24 */
25
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
operator_properties_init(wmOperatorType * ot)40 static void operator_properties_init(wmOperatorType *ot)
41 {
42 PyTypeObject *py_class = ot->rna_ext.data;
43 RNA_struct_blender_type_set(ot->rna_ext.srna, ot);
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 */
51 RNA_def_struct_identifier_no_struct_map(ot->srna, ot->idname);
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_AsString(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) {
95 PointerRNA ptr;
96 PropertyRNA *prop;
97
98 RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
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
BPY_RNA_operator_wrapper(wmOperatorType * ot,void * userdata)118 void BPY_RNA_operator_wrapper(wmOperatorType *ot, void *userdata)
119 {
120 /* take care not to overwrite anything set in
121 * WM_operatortype_append_ptr before opfunc() is called */
122 StructRNA *srna = ot->srna;
123 *ot = *((wmOperatorType *)userdata);
124 ot->srna = srna; /* restore */
125
126 /* Use i18n context from rna_ext.srna if possible (py operators). */
127 if (ot->rna_ext.srna) {
128 RNA_def_struct_translation_context(ot->srna, RNA_struct_translation_context(ot->rna_ext.srna));
129 }
130
131 operator_properties_init(ot);
132 }
133
BPY_RNA_operator_macro_wrapper(wmOperatorType * ot,void * userdata)134 void BPY_RNA_operator_macro_wrapper(wmOperatorType *ot, void *userdata)
135 {
136 wmOperatorType *data = (wmOperatorType *)userdata;
137
138 /* only copy a couple of things, the rest is set by the macro registration */
139 ot->name = data->name;
140 ot->idname = data->idname;
141 ot->description = data->description;
142 ot->flag |= data->flag; /* append flags to the one set by registration */
143 ot->pyop_poll = data->pyop_poll;
144 ot->ui = data->ui;
145 ot->rna_ext = data->rna_ext;
146
147 /* Use i18n context from rna_ext.srna if possible (py operators). */
148 if (ot->rna_ext.srna) {
149 RNA_def_struct_translation_context(ot->srna, RNA_struct_translation_context(ot->rna_ext.srna));
150 }
151
152 operator_properties_init(ot);
153 }
154
PYOP_wrap_macro_define(PyObject * UNUSED (self),PyObject * args)155 PyObject *PYOP_wrap_macro_define(PyObject *UNUSED(self), PyObject *args)
156 {
157 wmOperatorType *ot;
158 wmOperatorTypeMacro *otmacro;
159 PyObject *macro;
160 PointerRNA ptr_otmacro;
161 StructRNA *srna;
162
163 const char *opname;
164 const char *macroname;
165
166 if (!PyArg_ParseTuple(args, "Os:_bpy.ops.macro_define", ¯o, &opname)) {
167 return NULL;
168 }
169
170 if (WM_operatortype_find(opname, true) == NULL) {
171 PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid operator id", opname);
172 return NULL;
173 }
174
175 /* identifiers */
176 srna = pyrna_struct_as_srna((PyObject *)macro, false, "Macro Define:");
177 if (srna == NULL) {
178 return NULL;
179 }
180
181 macroname = RNA_struct_identifier(srna);
182 ot = WM_operatortype_find(macroname, true);
183
184 if (!ot) {
185 PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid macro", macroname);
186 return NULL;
187 }
188
189 otmacro = WM_operatortype_macro_define(ot, opname);
190
191 RNA_pointer_create(NULL, &RNA_OperatorMacro, otmacro, &ptr_otmacro);
192
193 return pyrna_struct_CreatePyObject(&ptr_otmacro);
194 }
195