soc-2008-mxcurioni: merged changes to revision 23516
[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., 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
52         /* note that context is an int, python does the conversion in this case */
53         int context= WM_OP_EXEC_DEFAULT;
54
55         // 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...
56         bContext *C = BPy_GetContext();
57         
58         if (!PyArg_ParseTuple(args, "s|O!i:bpy.__ops__.call", &opname, &PyDict_Type, &kw, &context))
59                 return NULL;
60
61         ot= WM_operatortype_find(opname, TRUE);
62
63         if (ot == NULL) {
64                 PyErr_Format( PyExc_SystemError, "bpy.__ops__.call: operator \"%s\"could not be found", opname);
65                 return NULL;
66         }
67         
68         if(WM_operator_poll((bContext*)C, ot) == FALSE) {
69                 PyErr_SetString( PyExc_SystemError, "bpy.__ops__.call: operator poll() function failed, context is incorrect");
70                 return NULL;
71         }
72
73         /* WM_operator_properties_create(&ptr, opname); */
74         /* Save another lookup */
75         RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
76         
77         if(kw && PyDict_Size(kw))
78                 error_val= pyrna_pydict_to_props(&ptr, kw, 0, "Converting py args to operator properties: ");
79
80         
81         if (error_val==0) {
82                 ReportList *reports;
83
84                 reports= MEM_mallocN(sizeof(ReportList), "wmOperatorReportList");
85                 BKE_reports_init(reports, RPT_STORE);
86
87                 WM_operator_call_py(C, ot, context, &ptr, reports);
88
89                 if(BPy_reports_to_error(reports))
90                         error_val = -1;
91
92                 /* operator output is nice to have in the terminal/console too */
93                 if(reports->list.first) {
94                         char *report_str= BKE_reports_string(reports, 0); /* all reports */
95
96                         if(report_str) {
97                                 PySys_WriteStdout(report_str);
98                                 MEM_freeN(report_str);
99                         }
100                 }
101
102                 BKE_reports_clear(reports);
103                 if ((reports->flag & RPT_FREE) == 0)
104                 {
105                         MEM_freeN(reports);
106                 }
107         }
108
109         WM_operator_properties_free(&ptr);
110
111 #if 0
112         /* if there is some way to know an operator takes args we should use this */
113         {
114                 /* no props */
115                 if (kw != NULL) {
116                         PyErr_Format(PyExc_AttributeError, "Operator \"%s\" does not take any args", opname);
117                         return NULL;
118                 }
119
120                 WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
121         }
122 #endif
123
124         if (error_val==-1) {
125                 return NULL;
126         }
127
128         Py_RETURN_NONE;
129 }
130
131 static PyObject *pyop_as_string( PyObject * self, PyObject * args)
132 {
133         wmOperatorType *ot;
134         PointerRNA ptr;
135
136         char            *opname;
137         PyObject        *kw= NULL; /* optional args */
138         int all_args = 1;
139         int error_val= 0;
140
141         char *buf;
142         PyObject *pybuf;
143
144         bContext *C = BPy_GetContext();
145
146         if (!PyArg_ParseTuple(args, "s|O!i:bpy.__ops__.as_string", &opname, &PyDict_Type, &kw, &all_args))
147                 return NULL;
148
149         ot= WM_operatortype_find(opname, TRUE);
150
151         if (ot == NULL) {
152                 PyErr_Format( PyExc_SystemError, "bpy.__ops__.as_string: operator \"%s\"could not be found", opname);
153                 return NULL;
154         }
155
156         /* WM_operator_properties_create(&ptr, opname); */
157         /* Save another lookup */
158         RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
159
160         if(kw && PyDict_Size(kw))
161                 error_val= pyrna_pydict_to_props(&ptr, kw, 0, "Converting py args to operator properties: ");
162
163         if (error_val==0)
164                 buf= WM_operator_pystring(C, ot, &ptr, all_args);
165
166         WM_operator_properties_free(&ptr);
167
168         if (error_val==-1) {
169                 return NULL;
170         }
171
172         if(buf) {
173                 pybuf= PyUnicode_FromString(buf);
174                 MEM_freeN(buf);
175         }
176         else {
177                 pybuf= PyUnicode_FromString("");
178         }
179
180         return pybuf;
181 }
182
183 static PyObject *pyop_dir(PyObject *self)
184 {
185         PyObject *list = PyList_New(0), *name;
186         wmOperatorType *ot;
187         
188         for(ot= WM_operatortype_first(); ot; ot= ot->next) {
189                 name = PyUnicode_FromString(ot->idname);
190                 PyList_Append(list, name);
191                 Py_DECREF(name);
192         }
193         
194         return list;
195 }
196
197 static PyObject *pyop_getrna(PyObject *self, PyObject *value)
198 {
199         wmOperatorType *ot;
200         PointerRNA ptr;
201         char *opname= _PyUnicode_AsString(value);
202         BPy_StructRNA *pyrna= NULL;
203         
204         if(opname==NULL) {
205                 PyErr_SetString(PyExc_TypeError, "bpy.__ops__.get_rna() expects a string argument");
206                 return NULL;
207         }
208         ot= WM_operatortype_find(opname, TRUE);
209         if(ot==NULL) {
210                 PyErr_Format(PyExc_KeyError, "bpy.__ops__.get_rna(\"%s\") not found", opname);
211                 return NULL;
212         }
213         
214         /* type */
215         //RNA_pointer_create(NULL, &RNA_Struct, ot->srna, &ptr);
216
217         /* XXX - should call WM_operator_properties_free */
218         WM_operator_properties_create(&ptr, ot->idname);
219         pyrna= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
220         pyrna->freeptr= TRUE;
221         return (PyObject *)pyrna;
222 }
223
224 PyObject *BPY_operator_module( void )
225 {
226         static PyMethodDef pyop_call_meth =             {"call", (PyCFunction) pyop_call, METH_VARARGS, NULL};
227         static PyMethodDef pyop_as_string_meth ={"as_string", (PyCFunction) pyop_as_string, METH_VARARGS, NULL};
228         static PyMethodDef pyop_dir_meth =              {"dir", (PyCFunction) pyop_dir, METH_NOARGS, NULL};
229         static PyMethodDef pyop_getrna_meth =   {"get_rna", (PyCFunction) pyop_getrna, METH_O, NULL};
230         static PyMethodDef pyop_add_meth =              {"add", (PyCFunction) PYOP_wrap_add, METH_O, NULL};
231         static PyMethodDef pyop_remove_meth =   {"remove", (PyCFunction) PYOP_wrap_remove, METH_O, NULL};
232
233         PyObject *submodule = PyModule_New("bpy.__ops__");
234         PyDict_SetItemString(PySys_GetObject("modules"), "bpy.__ops__", submodule);
235
236         PyModule_AddObject( submodule, "call",  PyCFunction_New(&pyop_call_meth,        NULL) );
237         PyModule_AddObject( submodule, "as_string",PyCFunction_New(&pyop_as_string_meth,NULL) );
238         PyModule_AddObject( submodule, "dir",           PyCFunction_New(&pyop_dir_meth,         NULL) );
239         PyModule_AddObject( submodule, "get_rna",       PyCFunction_New(&pyop_getrna_meth,      NULL) );
240         PyModule_AddObject( submodule, "add",           PyCFunction_New(&pyop_add_meth,         NULL) );
241         PyModule_AddObject( submodule, "remove",        PyCFunction_New(&pyop_remove_meth,      NULL) );
242
243         return submodule;
244 }