29ec33767655d7fe0e92729fe170118cd0ef3f97
[blender-staging.git] / source / blender / python / intern / bpy_operator.c
1
2 /**
3  * $Id$
4  *
5  * ***** BEGIN GPL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * Contributor(s): Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 #include "bpy_operator.h"
27 #include "bpy_operator_wrap.h"
28 #include "bpy_rna.h" /* for setting arg props only - pyrna_py_to_prop() */
29 #include "bpy_compat.h"
30
31 //#include "blendef.h"
32 #include "BLI_dynstr.h"
33
34 #include "WM_api.h"
35 #include "WM_types.h"
36
37 #include "MEM_guardedalloc.h"
38 //#include "BKE_idprop.h"
39 #include "BKE_report.h"
40
41 extern ListBase global_ops; /* evil, temp use */
42
43
44
45 /* This function is only used by operators right now
46  * Its used for taking keyword args and filling in property values */
47 int PYOP_props_from_dict(PointerRNA *ptr, PyObject *kw)
48 {
49         int error_val = 0;
50         int totkw;
51         const char *arg_name= NULL;
52         PyObject *item;
53
54         PropertyRNA *prop, *iterprop;
55         CollectionPropertyIterator iter;
56
57         iterprop= RNA_struct_iterator_property(ptr);
58         RNA_property_collection_begin(ptr, iterprop, &iter);
59
60         totkw = kw ? PyDict_Size(kw):0;
61
62         for(; iter.valid; RNA_property_collection_next(&iter)) {
63                 prop= iter.ptr.data;
64
65                 arg_name= RNA_property_identifier(&iter.ptr, prop);
66
67                 if (strcmp(arg_name, "rna_type")==0) continue;
68
69                 if (kw==NULL) {
70                         PyErr_Format( PyExc_AttributeError, "no args, expected \"%s\"", arg_name ? arg_name : "<UNKNOWN>");
71                         error_val= -1;
72                         break;
73                 }
74
75                 item= PyDict_GetItemString(kw, arg_name);
76
77                 if (item == NULL) {
78                         PyErr_Format( PyExc_AttributeError, "argument \"%s\" missing", arg_name ? arg_name : "<UNKNOWN>");
79                         error_val = -1; /* pyrna_py_to_prop sets the error */
80                         break;
81                 }
82
83                 if (pyrna_py_to_prop(ptr, prop, item)) {
84                         error_val= -1;
85                         break;
86                 }
87
88                 totkw--;
89         }
90
91         RNA_property_collection_end(&iter);
92
93         if (error_val==0 && totkw > 0) { /* some keywords were given that were not used :/ */
94                 PyObject *key, *value;
95                 Py_ssize_t pos = 0;
96
97                 while (PyDict_Next(kw, &pos, &key, &value)) {
98                         arg_name= _PyUnicode_AsString(key);
99                         if (RNA_struct_find_property(ptr, arg_name) == NULL) break;
100                         arg_name= NULL;
101                 }
102
103                 PyErr_Format( PyExc_AttributeError, "argument \"%s\" unrecognized", arg_name ? arg_name : "<UNKNOWN>");
104                 error_val = -1;
105         }
106
107         return error_val;
108 }
109
110 static PyObject *pyop_base_dir(PyObject *self);
111 static PyObject *pyop_base_rna(PyObject *self, PyObject *pyname);
112 static struct PyMethodDef pyop_base_methods[] = {
113         {"__dir__", (PyCFunction)pyop_base_dir, METH_NOARGS, ""},
114         {"__rna__", (PyCFunction)pyop_base_rna, METH_O, ""},
115         {"add", (PyCFunction)PYOP_wrap_add, METH_O, ""},
116         {"remove", (PyCFunction)PYOP_wrap_remove, METH_O, ""},
117         {NULL, NULL, 0, NULL}
118 };
119
120 /* 'self' stores the operator string */
121 static PyObject *pyop_base_call( PyObject * self, PyObject * args,  PyObject * kw)
122 {
123         wmOperatorType *ot;
124         int error_val = 0;
125         PointerRNA ptr;
126         
127         // XXX Todo, work out a better solution for passing on context, could make a tuple from self and pack the name and Context into it...
128         bContext *C = (bContext *)PyCObject_AsVoidPtr(PyDict_GetItemString(PyEval_GetGlobals(), "__bpy_context__"));
129         
130         char *opname = _PyUnicode_AsString(self);
131         char *report_str= NULL;
132
133         if (PyTuple_Size(args)) {
134                 PyErr_SetString( PyExc_AttributeError, "All operator args must be keywords");
135                 return NULL;
136         }
137
138         ot= WM_operatortype_find(opname);
139         if (ot == NULL) {
140                 PyErr_Format( PyExc_SystemError, "Operator \"%s\"could not be found", opname);
141                 return NULL;
142         }
143         
144         if(ot->poll && (ot->poll(C) == 0)) {
145                 PyErr_SetString( PyExc_SystemError, "Operator poll() function failed, context is incorrect");
146                 return NULL;
147         }
148         
149         WM_operator_properties_create(&ptr, opname);
150         
151         error_val= PYOP_props_from_dict(&ptr, kw);
152         
153         if (error_val==0) {
154                 ReportList reports;
155
156                 BKE_reports_init(&reports, RPT_STORE);
157
158                 WM_operator_call_py(C, ot, &ptr, &reports);
159
160                 report_str= BKE_reports_string(&reports, RPT_ERROR);
161
162                 if (report_str) {
163                         PyErr_SetString(PyExc_SystemError, report_str);
164                         MEM_freeN(report_str);
165                         error_val = -1;
166                 }
167
168                 if (reports.list.first)
169                         BKE_reports_clear(&reports);
170         }
171
172         WM_operator_properties_free(&ptr);
173
174 #if 0
175         /* if there is some way to know an operator takes args we should use this */
176         {
177                 /* no props */
178                 if (kw != NULL) {
179                         PyErr_Format(PyExc_AttributeError, "Operator \"%s\" does not take any args", opname);
180                         return NULL;
181                 }
182
183                 WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
184         }
185 #endif
186
187         if (error_val==-1) {
188                 return NULL;
189         }
190
191         Py_RETURN_NONE;
192 }
193
194 static PyMethodDef pyop_base_call_meth[] = {
195         {"__op_call__", (PyCFunction)pyop_base_call, METH_VARARGS|METH_KEYWORDS, "generic operator calling function"}
196 };
197
198
199 //---------------getattr--------------------------------------------
200 static PyObject *pyop_base_getattro( BPy_OperatorBase * self, PyObject *pyname )
201 {
202         char *name = _PyUnicode_AsString(pyname);
203         PyObject *ret;
204         wmOperatorType *ot;
205         
206         if ((ot= WM_operatortype_find(name))) {
207                 ret = PyCFunction_New( pyop_base_call_meth, pyname); /* set the name string as self, PyCFunction_New incref's self */
208         }
209         else if ((ret = PyObject_GenericGetAttr((PyObject *)self, pyname))) {
210                 /* do nothing, this accounts for methoddef's add and remove */
211         }
212         else {
213                 PyErr_Format( PyExc_AttributeError, "Operator \"%s\" not found", name);
214                 ret= NULL;
215         }
216
217         return ret;
218 }
219
220 static PyObject *pyop_base_dir(PyObject *self)
221 {
222         PyObject *list = PyList_New(0), *name;
223         wmOperatorType *ot;
224         PyMethodDef *meth;
225         
226         for(ot= WM_operatortype_first(); ot; ot= ot->next) {
227                 name = PyUnicode_FromString(ot->idname);
228                 PyList_Append(list, name);
229                 Py_DECREF(name);
230         }
231
232         for(meth=pyop_base_methods; meth->ml_name; meth++) {
233                 name = PyUnicode_FromString(meth->ml_name);
234                 PyList_Append(list, name);
235                 Py_DECREF(name);
236         }
237         
238         return list;
239 }
240
241 static PyObject *pyop_base_rna(PyObject *self, PyObject *pyname)
242 {
243         char *name = _PyUnicode_AsString(pyname);
244         wmOperatorType *ot;
245         
246         if ((ot= WM_operatortype_find(name))) {
247                 BPy_StructRNA *pyrna;
248                 PointerRNA ptr;
249                 
250                 /* XXX POINTER - if this 'ot' is python generated, it could be free'd */
251                 RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
252                 
253                 pyrna= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr); /* were not really using &ptr, overwite next */
254                 //pyrna->freeptr= 1;
255                 return (PyObject *)pyrna;
256         }
257         else {
258                 PyErr_Format(PyExc_AttributeError, "Operator \"%s\" not found", name);
259                 return NULL;
260         }
261 }
262
263 PyTypeObject pyop_base_Type = {NULL};
264
265 PyObject *BPY_operator_module( bContext *C )
266 {
267         pyop_base_Type.tp_name = "OperatorBase";
268         pyop_base_Type.tp_basicsize = sizeof( BPy_OperatorBase );
269         pyop_base_Type.tp_getattro = ( getattrofunc )pyop_base_getattro;
270         pyop_base_Type.tp_flags = Py_TPFLAGS_DEFAULT;
271         pyop_base_Type.tp_methods = pyop_base_methods;
272         
273         if( PyType_Ready( &pyop_base_Type ) < 0 )
274                 return NULL;
275
276         //submodule = Py_InitModule3( "operator", M_rna_methods, "rna module" );
277         return (PyObject *)PyObject_NEW( BPy_OperatorBase, &pyop_base_Type );
278 }
279