support for registering operators using the same internal rna api as panels, menus...
[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 /* Note, this module is not to be used directly by the user.
27  * its accessed from blender with bpy.__ops__
28  * */
29
30 #include "bpy_operator.h"
31 #include "bpy_operator_wrap.h"
32 #include "bpy_rna.h" /* for setting arg props only - pyrna_py_to_prop() */
33 #include "bpy_util.h"
34
35 #include "WM_api.h"
36 #include "WM_types.h"
37
38 #include "MEM_guardedalloc.h"
39 #include "BKE_report.h"
40 #include "BKE_utildefines.h"
41
42
43 static PyObject *pyop_call( PyObject * self, PyObject * args)
44 {
45         wmOperatorType *ot;
46         int error_val = 0;
47         PointerRNA ptr;
48         
49         char            *opname;
50         PyObject        *kw= NULL; /* optional args */
51         PyObject        *context_dict= NULL; /* optional args */
52         PyObject        *context_dict_back;
53
54         /* note that context is an int, python does the conversion in this case */
55         int context= WM_OP_EXEC_DEFAULT;
56
57         // 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...
58         bContext *C = BPy_GetContext();
59         
60         if (!PyArg_ParseTuple(args, "sO|O!i:_bpy.ops.call", &opname, &context_dict, &PyDict_Type, &kw, &context))
61                 return NULL;
62
63         ot= WM_operatortype_exists(opname);
64
65         if (ot == NULL) {
66                 PyErr_Format( PyExc_SystemError, "_bpy.ops.call: operator \"%s\" could not be found", opname);
67                 return NULL;
68         }
69         
70         if(!PyDict_Check(context_dict))
71                 context_dict= NULL;
72
73         context_dict_back= CTX_py_dict_get(C);
74
75         CTX_py_dict_set(C, (void *)context_dict);
76         Py_XINCREF(context_dict); /* so we done loose it */
77
78         if(WM_operator_poll((bContext*)C, ot) == FALSE) {
79                 PyErr_Format( PyExc_SystemError, "_bpy.ops.call: operator %.200s.poll() function failed, context is incorrect", opname);
80                 error_val= -1;
81         }
82         else {
83                 /* WM_operator_properties_create(&ptr, opname); */
84                 /* Save another lookup */
85                 RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
86
87                 if(kw && PyDict_Size(kw))
88                         error_val= pyrna_pydict_to_props(&ptr, kw, 0, "Converting py args to operator properties: ");
89
90
91                 if (error_val==0) {
92                         ReportList *reports;
93
94                         reports= MEM_mallocN(sizeof(ReportList), "wmOperatorReportList");
95                         BKE_reports_init(reports, RPT_STORE);
96
97                         WM_operator_call_py(C, ot, context, &ptr, reports);
98
99                         if(BPy_reports_to_error(reports))
100                                 error_val = -1;
101
102                         /* operator output is nice to have in the terminal/console too */
103                         if(reports->list.first) {
104                                 char *report_str= BKE_reports_string(reports, 0); /* all reports */
105         
106                                 if(report_str) {
107                                         PySys_WriteStdout("%s\n", report_str);
108                                         MEM_freeN(report_str);
109                                 }
110                         }
111         
112                         BKE_reports_clear(reports);
113                         if ((reports->flag & RPT_FREE) == 0)
114                         {
115                                 MEM_freeN(reports);
116                         }
117                 }
118
119                 WM_operator_properties_free(&ptr);
120
121 #if 0
122                 /* if there is some way to know an operator takes args we should use this */
123                 {
124                         /* no props */
125                         if (kw != NULL) {
126                                 PyErr_Format(PyExc_AttributeError, "Operator \"%s\" does not take any args", opname);
127                                 return NULL;
128                         }
129
130                         WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
131                 }
132 #endif
133         }
134
135         /* restore with original context dict, probably NULL but need this for nested operator calls */
136         Py_XDECREF(context_dict);
137         CTX_py_dict_set(C, (void *)context_dict_back);
138
139         if (error_val==-1) {
140                 return NULL;
141         }
142
143         Py_RETURN_NONE;
144 }
145
146 static PyObject *pyop_as_string( PyObject * self, PyObject * args)
147 {
148         wmOperatorType *ot;
149         PointerRNA ptr;
150
151         char            *opname;
152         PyObject        *kw= NULL; /* optional args */
153         int all_args = 1;
154         int error_val= 0;
155
156         char *buf = NULL;
157         PyObject *pybuf;
158
159         bContext *C = BPy_GetContext();
160
161         if (!PyArg_ParseTuple(args, "s|O!i:_bpy.ops.as_string", &opname, &PyDict_Type, &kw, &all_args))
162                 return NULL;
163
164         ot= WM_operatortype_find(opname, TRUE);
165
166         if (ot == NULL) {
167                 PyErr_Format( PyExc_SystemError, "_bpy.ops.as_string: operator \"%s\"could not be found", opname);
168                 return NULL;
169         }
170
171         /* WM_operator_properties_create(&ptr, opname); */
172         /* Save another lookup */
173         RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
174
175         if(kw && PyDict_Size(kw))
176                 error_val= pyrna_pydict_to_props(&ptr, kw, 0, "Converting py args to operator properties: ");
177
178         if (error_val==0)
179                 buf= WM_operator_pystring(C, ot, &ptr, all_args);
180
181         WM_operator_properties_free(&ptr);
182
183         if (error_val==-1) {
184                 return NULL;
185         }
186
187         if(buf) {
188                 pybuf= PyUnicode_FromString(buf);
189                 MEM_freeN(buf);
190         }
191         else {
192                 pybuf= PyUnicode_FromString("");
193         }
194
195         return pybuf;
196 }
197
198 static PyObject *pyop_dir(PyObject *self)
199 {
200         PyObject *list = PyList_New(0), *name;
201         wmOperatorType *ot;
202         
203         for(ot= WM_operatortype_first(); ot; ot= ot->next) {
204                 name = PyUnicode_FromString(ot->idname);
205                 PyList_Append(list, name);
206                 Py_DECREF(name);
207         }
208         
209         return list;
210 }
211
212 static PyObject *pyop_getrna(PyObject *self, PyObject *value)
213 {
214         wmOperatorType *ot;
215         PointerRNA ptr;
216         char *opname= _PyUnicode_AsString(value);
217         BPy_StructRNA *pyrna= NULL;
218         
219         if(opname==NULL) {
220                 PyErr_SetString(PyExc_TypeError, "_bpy.ops.get_rna() expects a string argument");
221                 return NULL;
222         }
223         ot= WM_operatortype_find(opname, TRUE);
224         if(ot==NULL) {
225                 PyErr_Format(PyExc_KeyError, "_bpy.ops.get_rna(\"%s\") not found", opname);
226                 return NULL;
227         }
228         
229         /* type */
230         //RNA_pointer_create(NULL, &RNA_Struct, ot->srna, &ptr);
231
232         /* XXX - should call WM_operator_properties_free */
233         WM_operator_properties_create_ptr(&ptr, ot);
234
235         
236         pyrna= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
237         pyrna->freeptr= TRUE;
238         return (PyObject *)pyrna;
239 }
240
241 PyObject *BPY_operator_module( void )
242 {
243         static PyMethodDef pyop_call_meth =             {"call", (PyCFunction) pyop_call, METH_VARARGS, NULL};
244         static PyMethodDef pyop_as_string_meth ={"as_string", (PyCFunction) pyop_as_string, METH_VARARGS, NULL};
245         static PyMethodDef pyop_dir_meth =              {"dir", (PyCFunction) pyop_dir, METH_NOARGS, NULL};
246         static PyMethodDef pyop_getrna_meth =   {"get_rna", (PyCFunction) pyop_getrna, METH_O, NULL};
247         static PyMethodDef pyop_add_meth =              {"add", (PyCFunction) PYOP_wrap_add, METH_O, NULL};
248         static PyMethodDef pyop_add_macro_meth ={"add_macro", (PyCFunction) PYOP_wrap_add_macro, METH_O, NULL};
249         static PyMethodDef pyop_macro_def_meth ={"macro_define", (PyCFunction) PYOP_wrap_macro_define, METH_VARARGS, NULL};
250         static PyMethodDef pyop_remove_meth =   {"remove", (PyCFunction) PYOP_wrap_remove, METH_O, NULL};
251
252         PyObject *submodule = PyModule_New("_bpy.ops");
253         PyDict_SetItemString(PySys_GetObject("modules"), "_bpy.ops", submodule);
254
255         PyModule_AddObject( submodule, "call",  PyCFunction_New(&pyop_call_meth,        NULL) );
256         PyModule_AddObject( submodule, "as_string",PyCFunction_New(&pyop_as_string_meth,NULL) );
257         PyModule_AddObject( submodule, "dir",           PyCFunction_New(&pyop_dir_meth,         NULL) );
258         PyModule_AddObject( submodule, "get_rna",       PyCFunction_New(&pyop_getrna_meth,      NULL) );
259         PyModule_AddObject( submodule, "add",           PyCFunction_New(&pyop_add_meth,         NULL) );
260         PyModule_AddObject( submodule, "add_macro",     PyCFunction_New(&pyop_add_macro_meth,           NULL) );
261         PyModule_AddObject( submodule, "macro_define",PyCFunction_New(&pyop_macro_def_meth,             NULL) );
262         PyModule_AddObject( submodule, "remove",        PyCFunction_New(&pyop_remove_meth,      NULL) );
263
264         return submodule;
265 }