Py-Driver: add 'self' option
[blender.git] / source / blender / python / intern / bpy_operator_wrap.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_operator_wrap.c
24  *  \ingroup pythonintern
25  *
26  * This file is so python can define operators that C can call into.
27  * The generic callback functions for python operators are defines in
28  * 'rna_wm.c', some calling into functions here to do python specific
29  * functionality.
30  */
31
32 #include <Python.h>
33
34 #include "BLI_utildefines.h"
35
36 #include "WM_api.h"
37 #include "WM_types.h"
38
39 #include "RNA_access.h"
40 #include "RNA_define.h"
41
42 #include "bpy_rna.h"
43 #include "bpy_intern_string.h"
44 #include "bpy_operator_wrap.h"  /* own include */
45
46 static void operator_properties_init(wmOperatorType *ot)
47 {
48         PyTypeObject *py_class = ot->ext.data;
49         RNA_struct_blender_type_set(ot->ext.srna, ot);
50
51         /* only call this so pyrna_deferred_register_class gives a useful error
52          * WM_operatortype_append_ptr will call RNA_def_struct_identifier
53          * later */
54         RNA_def_struct_identifier(ot->srna, ot->idname);
55
56         if (pyrna_deferred_register_class(ot->srna, py_class) != 0) {
57                 PyErr_Print(); /* failed to register operator props */
58                 PyErr_Clear();
59         }
60
61         /* set the default property: ot->prop */
62         {
63                 /* picky developers will notice that 'bl_property' won't work with inheritance
64                  * get direct from the dict to avoid raising a load of attribute errors (yes this isnt ideal) - campbell */
65                 PyObject *py_class_dict = py_class->tp_dict;
66                 PyObject *bl_property = PyDict_GetItem(py_class_dict, bpy_intern_str_bl_property);
67                 const char *prop_id;
68                 bool prop_raise_error;
69
70                 if (bl_property) {
71                         if (PyUnicode_Check(bl_property)) {
72                                 /* since the property is explicitly given, raise an error if its not found */
73                                 prop_id = _PyUnicode_AsString(bl_property);
74                                 prop_raise_error = true;
75                         }
76                         else {
77                                 PyErr_Format(PyExc_ValueError,
78                                              "%.200s.bl_property should be a string, not %.200s",
79                                              ot->idname, Py_TYPE(bl_property)->tp_name);
80
81                                 /* this could be done cleaner, for now its OK */
82                                 PyErr_Print();
83                                 PyErr_Clear();
84
85                                 prop_id = NULL;
86                                 prop_raise_error = false;
87                         }
88                 }
89                 else {
90                         /* fallback to hard-coded string (pre 2.66, could be deprecated) */
91                         prop_id = "type";
92                         prop_raise_error = false;
93                 }
94
95                 if (prop_id) {
96                         PointerRNA ptr;
97                         PropertyRNA *prop;
98
99                         RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
100                         prop = RNA_struct_find_property(&ptr, prop_id);
101                         if (prop) {
102                                 ot->prop = prop;
103                         }
104                         else {
105                                 if (prop_raise_error) {
106                                         PyErr_Format(PyExc_ValueError,
107                                                      "%.200s.bl_property '%.200s' not found",
108                                                      ot->idname, prop_id);
109
110                                         /* this could be done cleaner, for now its OK */
111                                         PyErr_Print();
112                                         PyErr_Clear();
113                                 }
114                         }
115                 }
116         }
117         /* end 'ot->prop' assignment */
118
119 }
120
121 void operator_wrapper(wmOperatorType *ot, void *userdata)
122 {
123         /* take care not to overwrite anything set in
124          * WM_operatortype_append_ptr before opfunc() is called */
125         StructRNA *srna = ot->srna;
126         *ot = *((wmOperatorType *)userdata);
127         ot->srna = srna; /* restore */
128
129         /* Use i18n context from ext.srna if possible (py operators). */
130         if (ot->ext.srna) {
131                 RNA_def_struct_translation_context(ot->srna, RNA_struct_translation_context(ot->ext.srna));
132         }
133
134         operator_properties_init(ot);
135 }
136
137 void macro_wrapper(wmOperatorType *ot, void *userdata)
138 {
139         wmOperatorType *data = (wmOperatorType *)userdata;
140
141         /* only copy a couple of things, the rest is set by the macro registration */
142         ot->name = data->name;
143         ot->idname = data->idname;
144         ot->description = data->description;
145         ot->flag |= data->flag; /* append flags to the one set by registration */
146         ot->pyop_poll = data->pyop_poll;
147         ot->ui = data->ui;
148         ot->ext = data->ext;
149
150         /* Use i18n context from ext.srna if possible (py operators). */
151         if (ot->ext.srna) {
152                 RNA_def_struct_translation_context(ot->srna, RNA_struct_translation_context(ot->ext.srna));
153         }
154
155         operator_properties_init(ot);
156 }
157
158 PyObject *PYOP_wrap_macro_define(PyObject *UNUSED(self), PyObject *args)
159 {
160         wmOperatorType *ot;
161         wmOperatorTypeMacro *otmacro;
162         PyObject *macro;
163         PointerRNA ptr_otmacro;
164         StructRNA *srna;
165
166         const char *opname;
167         const char *macroname;
168
169         if (!PyArg_ParseTuple(args, "Os:_bpy.ops.macro_define", &macro, &opname))
170                 return NULL;
171
172         if (WM_operatortype_find(opname, true) == NULL) {
173                 PyErr_Format(PyExc_ValueError,
174                              "Macro Define: '%s' is not a valid operator id",
175                              opname);
176                 return NULL;
177         }
178
179         /* identifiers */
180         srna = pyrna_struct_as_srna((PyObject *)macro, false, "Macro Define:");
181         if (srna == NULL) {
182                 return NULL;
183         }
184
185         macroname = RNA_struct_identifier(srna);
186         ot = WM_operatortype_find(macroname, true);
187
188         if (!ot) {
189                 PyErr_Format(PyExc_ValueError,
190                              "Macro Define: '%s' is not a valid macro",
191                              macroname);
192                 return NULL;
193         }
194
195         otmacro = WM_operatortype_macro_define(ot, opname);
196
197         RNA_pointer_create(NULL, &RNA_OperatorMacro, otmacro, &ptr_otmacro);
198
199         return pyrna_struct_CreatePyObject(&ptr_otmacro);
200 }
201