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