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