f2e2dd77e6dc19ef70e7b0e3a48603aecee0f88c
[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                 BKE_reports_clear(reports);
93                 if ((reports->flag & RPT_FREE) == 0)
94                 {
95                         MEM_freeN(reports);
96                 }
97         }
98
99         WM_operator_properties_free(&ptr);
100
101 #if 0
102         /* if there is some way to know an operator takes args we should use this */
103         {
104                 /* no props */
105                 if (kw != NULL) {
106                         PyErr_Format(PyExc_AttributeError, "Operator \"%s\" does not take any args", opname);
107                         return NULL;
108                 }
109
110                 WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
111         }
112 #endif
113
114         if (error_val==-1) {
115                 return NULL;
116         }
117
118         Py_RETURN_NONE;
119 }
120
121 static PyObject *pyop_as_string( PyObject * self, PyObject * args)
122 {
123         wmOperatorType *ot;
124         PointerRNA ptr;
125
126         char            *opname;
127         PyObject        *kw= NULL; /* optional args */
128         int all_args = 1;
129         int error_val= 0;
130
131         char *buf;
132         PyObject *pybuf;
133
134         bContext *C = BPy_GetContext();
135
136         if (!PyArg_ParseTuple(args, "s|O!i:bpy.__ops__.as_string", &opname, &PyDict_Type, &kw, &all_args))
137                 return NULL;
138
139         ot= WM_operatortype_find(opname, TRUE);
140
141         if (ot == NULL) {
142                 PyErr_Format( PyExc_SystemError, "bpy.__ops__.as_string: operator \"%s\"could not be found", opname);
143                 return NULL;
144         }
145
146         /* WM_operator_properties_create(&ptr, opname); */
147         /* Save another lookup */
148         RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
149
150         if(kw && PyDict_Size(kw))
151                 error_val= pyrna_pydict_to_props(&ptr, kw, 0, "Converting py args to operator properties: ");
152
153         if (error_val==0)
154                 buf= WM_operator_pystring(C, ot, &ptr, all_args);
155
156         WM_operator_properties_free(&ptr);
157
158         if (error_val==-1) {
159                 return NULL;
160         }
161
162         if(buf) {
163                 pybuf= PyUnicode_FromString(buf);
164                 MEM_freeN(buf);
165         }
166         else {
167                 pybuf= PyUnicode_FromString("");
168         }
169
170         return pybuf;
171 }
172
173 static PyObject *pyop_dir(PyObject *self)
174 {
175         PyObject *list = PyList_New(0), *name;
176         wmOperatorType *ot;
177         
178         for(ot= WM_operatortype_first(); ot; ot= ot->next) {
179                 name = PyUnicode_FromString(ot->idname);
180                 PyList_Append(list, name);
181                 Py_DECREF(name);
182         }
183         
184         return list;
185 }
186
187 static PyObject *pyop_getrna(PyObject *self, PyObject *value)
188 {
189         wmOperatorType *ot;
190         PointerRNA ptr;
191         char *opname= _PyUnicode_AsString(value);
192         BPy_StructRNA *pyrna= NULL;
193         
194         if(opname==NULL) {
195                 PyErr_SetString(PyExc_TypeError, "bpy.__ops__.get_rna() expects a string argument");
196                 return NULL;
197         }
198         ot= WM_operatortype_find(opname, TRUE);
199         if(ot==NULL) {
200                 PyErr_Format(PyExc_KeyError, "bpy.__ops__.get_rna(\"%s\") not found", opname);
201                 return NULL;
202         }
203         
204         /* type */
205         //RNA_pointer_create(NULL, &RNA_Struct, ot->srna, &ptr);
206
207         /* XXX - should call WM_operator_properties_free */
208         WM_operator_properties_create(&ptr, ot->idname);
209         pyrna= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
210         pyrna->freeptr= TRUE;
211         return (PyObject *)pyrna;
212 }
213
214 PyObject *BPY_operator_module( void )
215 {
216         static PyMethodDef pyop_call_meth =             {"call", (PyCFunction) pyop_call, METH_VARARGS, NULL};
217         static PyMethodDef pyop_as_string_meth ={"as_string", (PyCFunction) pyop_as_string, METH_VARARGS, NULL};
218         static PyMethodDef pyop_dir_meth =              {"dir", (PyCFunction) pyop_dir, METH_NOARGS, NULL};
219         static PyMethodDef pyop_getrna_meth =   {"get_rna", (PyCFunction) pyop_getrna, METH_O, NULL};
220         static PyMethodDef pyop_add_meth =              {"add", (PyCFunction) PYOP_wrap_add, METH_O, NULL};
221         static PyMethodDef pyop_remove_meth =   {"remove", (PyCFunction) PYOP_wrap_remove, METH_O, NULL};
222
223         PyObject *submodule = PyModule_New("bpy.__ops__");
224         PyDict_SetItemString(PySys_GetObject("modules"), "bpy.__ops__", submodule);
225
226         PyModule_AddObject( submodule, "call",  PyCFunction_New(&pyop_call_meth,        NULL) );
227         PyModule_AddObject( submodule, "as_string",PyCFunction_New(&pyop_as_string_meth,NULL) );
228         PyModule_AddObject( submodule, "dir",           PyCFunction_New(&pyop_dir_meth,         NULL) );
229         PyModule_AddObject( submodule, "get_rna",       PyCFunction_New(&pyop_getrna_meth,      NULL) );
230         PyModule_AddObject( submodule, "add",           PyCFunction_New(&pyop_add_meth,         NULL) );
231         PyModule_AddObject( submodule, "remove",        PyCFunction_New(&pyop_remove_meth,      NULL) );
232
233         return submodule;
234 }