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