remove python2.x support
[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 /* 'self' stores the operator string */
44 static PyObject *pyop_call( PyObject * self, PyObject * args)
45 {
46         wmOperatorType *ot;
47         int error_val = 0;
48         PointerRNA ptr;
49         
50         char *opname;
51         PyObject *kw= NULL;
52
53         // 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...
54         bContext *C = BPy_GetContext();
55         
56
57         switch(PyTuple_Size(args)) {
58         case 2:
59                 kw = PyTuple_GET_ITEM(args, 1);
60
61                 if(!PyDict_Check(kw)) {
62                         PyErr_SetString( PyExc_AttributeError, "bpy.__ops__.call: expected second arg to be a dict");
63                         return NULL;
64                 }
65                 /* pass through */
66         case 1:
67                 opname = _PyUnicode_AsString(PyTuple_GET_ITEM(args, 0));
68
69                 if(opname==NULL) {
70                         PyErr_SetString( PyExc_AttributeError, "bpy.__ops__.call: expected the first arg to be a string");
71                         return NULL;
72                 }
73                 break;
74         default:
75                 PyErr_SetString( PyExc_AttributeError, "bpy.__ops__.call: expected a string and optional dict");
76                 return NULL;
77         }
78
79
80         ot= WM_operatortype_find(opname, TRUE);
81
82         if (ot == NULL) {
83                 PyErr_Format( PyExc_SystemError, "bpy.__ops__.call: operator \"%s\"could not be found", opname);
84                 return NULL;
85         }
86         
87         if(ot->poll && (ot->poll(C) == FALSE)) {
88                 PyErr_SetString( PyExc_SystemError, "bpy.__ops__.call: operator poll() function failed, context is incorrect");
89                 return NULL;
90         }
91         
92         /* WM_operator_properties_create(&ptr, opname); */
93         /* Save another lookup */
94         RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
95         
96         if(kw && PyDict_Size(kw))
97                 error_val= pyrna_pydict_to_props(&ptr, kw, 0, "Converting py args to operator properties: ");
98
99         
100         if (error_val==0) {
101                 ReportList reports;
102
103                 BKE_reports_init(&reports, RPT_STORE);
104
105                 WM_operator_call_py(C, ot, &ptr, &reports);
106
107                 if(BPy_reports_to_error(&reports))
108                         error_val = -1;
109
110                 BKE_reports_clear(&reports);
111         }
112
113         WM_operator_properties_free(&ptr);
114
115 #if 0
116         /* if there is some way to know an operator takes args we should use this */
117         {
118                 /* no props */
119                 if (kw != NULL) {
120                         PyErr_Format(PyExc_AttributeError, "Operator \"%s\" does not take any args", opname);
121                         return NULL;
122                 }
123
124                 WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
125         }
126 #endif
127
128         if (error_val==-1) {
129                 return NULL;
130         }
131
132         Py_RETURN_NONE;
133 }
134
135 static PyObject *pyop_dir(PyObject *self)
136 {
137         PyObject *list = PyList_New(0), *name;
138         wmOperatorType *ot;
139         
140         for(ot= WM_operatortype_first(); ot; ot= ot->next) {
141                 name = PyUnicode_FromString(ot->idname);
142                 PyList_Append(list, name);
143                 Py_DECREF(name);
144         }
145         
146         return list;
147 }
148
149 static PyObject *pyop_getrna(PyObject *self, PyObject *value)
150 {
151         wmOperatorType *ot;
152         PointerRNA ptr;
153         char *opname= _PyUnicode_AsString(value);
154         BPy_StructRNA *pyrna= NULL;
155         
156         if(opname==NULL) {
157                 PyErr_SetString(PyExc_TypeError, "bpy.__ops__.get_rna() expects a string argument");
158                 return NULL;
159         }
160         ot= WM_operatortype_find(opname, TRUE);
161         if(ot==NULL) {
162                 PyErr_Format(PyExc_KeyError, "bpy.__ops__.get_rna(\"%s\") not found", opname);
163                 return NULL;
164         }
165         
166         /* type */
167         //RNA_pointer_create(NULL, &RNA_Struct, ot->srna, &ptr);
168
169         /* XXX - should call WM_operator_properties_free */
170         WM_operator_properties_create(&ptr, ot->idname);
171         pyrna= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
172         pyrna->freeptr= TRUE;
173         return (PyObject *)pyrna;
174 }
175
176 PyObject *BPY_operator_module( void )
177 {
178         static PyMethodDef pyop_call_meth =             {"call", (PyCFunction) pyop_call, METH_VARARGS, NULL};
179         static PyMethodDef pyop_dir_meth =              {"dir", (PyCFunction) pyop_dir, METH_NOARGS, NULL};
180         static PyMethodDef pyop_getrna_meth =   {"get_rna", (PyCFunction) pyop_getrna, METH_O, NULL};
181         static PyMethodDef pyop_add_meth =              {"add", (PyCFunction) PYOP_wrap_add, METH_O, NULL};
182         static PyMethodDef pyop_remove_meth =   {"remove", (PyCFunction) PYOP_wrap_remove, METH_O, NULL};
183
184         PyObject *submodule = PyModule_New("bpy.__ops__");
185         PyDict_SetItemString(PySys_GetObject("modules"), "bpy.__ops__", submodule);
186
187         PyModule_AddObject( submodule, "call",  PyCFunction_New(&pyop_call_meth,        NULL) );
188         PyModule_AddObject( submodule, "dir",           PyCFunction_New(&pyop_dir_meth,         NULL) );
189         PyModule_AddObject( submodule, "get_rna",       PyCFunction_New(&pyop_getrna_meth,      NULL) );
190         PyModule_AddObject( submodule, "add",           PyCFunction_New(&pyop_add_meth,         NULL) );
191         PyModule_AddObject( submodule, "remove",        PyCFunction_New(&pyop_remove_meth,      NULL) );
192
193         return submodule;
194 }