removed ED_ prefix from script operator.
[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 #include "bpy_operator.h"
27 #include "bpy_rna.h" /* for setting arg props only - pyrna_py_to_prop() */
28 #include "bpy_compat.h"
29
30 //#include "blendef.h"
31 #include "BLI_dynstr.h"
32 #include "WM_api.h"
33 #include "WM_types.h"
34
35 #include "MEM_guardedalloc.h"
36 #include "BKE_idprop.h"
37
38 extern ListBase global_ops; /* evil, temp use */
39
40 /* floats bigger then this are displayed as inf in the docstrings */
41 #define MAXFLOAT_DOC 10000000
42
43 static int pyop_func_compare( BPy_OperatorFunc * a, BPy_OperatorFunc * b )
44 {
45         return (strcmp(a->name, b->name)==0) ? 0 : -1;
46 }
47
48 /*----------------------repr--------------------------------------------*/
49 static PyObject *pyop_base_repr( BPy_OperatorBase * self )
50 {
51         return PyUnicode_FromFormat( "[BPy_OperatorBase]");
52 }
53
54 static PyObject *pyop_func_repr( BPy_OperatorFunc * self )
55 {
56         return PyUnicode_FromFormat( "[BPy_OperatorFunc \"%s\"]", self->name);
57 }
58
59 //---------------getattr--------------------------------------------
60 static PyObject *pyop_base_getattro( BPy_OperatorBase * self, PyObject *pyname )
61 {
62         char *name = _PyUnicode_AsString(pyname);
63         PyObject *ret;
64         wmOperatorType *ot;
65
66         if( strcmp( name, "__members__" ) == 0 ) {
67                 PyObject *item;
68
69                 ret = PyList_New(0);
70
71                 for(ot= WM_operatortype_first(); ot; ot= ot->next) {
72                         item = PyUnicode_FromString( ot->idname );
73                         PyList_Append(ret, item);
74                         Py_DECREF(item);
75                 }
76         } else {
77                 ot = WM_operatortype_find(name);
78
79                 if (ot) {
80                         ret= pyop_func_CreatePyObject(self->C, name);
81                 }
82                 else {
83                         PyErr_Format( PyExc_AttributeError, "Operator \"%s\" not found", name);
84                         ret= NULL;
85                 }
86         }
87         
88         return ret;
89 }
90
91 static PyObject * pyop_func_call(BPy_OperatorFunc * self, PyObject *args, PyObject *kw)
92 {
93         IDProperty *properties = NULL;
94         wmOperatorType *ot;
95
96         int error_val = 0;
97         int totkw;
98         const char *arg_name= NULL;
99         PyObject *item;
100         
101         PointerRNA ptr;
102         PropertyRNA *prop, *iterprop;
103         CollectionPropertyIterator iter;
104
105         if (PyTuple_Size(args)) {
106                 PyErr_SetString( PyExc_AttributeError, "All operator args must be keywords");
107                 return NULL;
108         }
109
110         ot= WM_operatortype_find(self->name);
111         if (ot == NULL) {
112                 PyErr_SetString( PyExc_SystemError, "Operator could not be found");
113                 return NULL;
114         }
115         
116         RNA_pointer_create(NULL, NULL, ot->srna, &properties, &ptr);
117
118
119         iterprop= RNA_struct_iterator_property(&ptr);
120         RNA_property_collection_begin(&ptr, iterprop, &iter);
121
122         totkw = kw ? PyDict_Size(kw):0;
123
124         for(; iter.valid; RNA_property_collection_next(&iter)) {
125                 prop= iter.ptr.data;
126
127                 arg_name= RNA_property_identifier(&iter.ptr, prop);
128
129                 if (strcmp(arg_name, "rna_type")==0) continue;
130
131                 if (kw==NULL) {
132                         PyErr_Format( PyExc_AttributeError, "no args, expected \"%s\"", arg_name ? arg_name : "<UNKNOWN>");
133                         error_val= 1;
134                         break;
135                 }
136                 
137                 item= PyDict_GetItemString(kw, arg_name);
138
139                 if (item == NULL) {
140                         PyErr_Format( PyExc_AttributeError, "argument \"%s\" missing", arg_name ? arg_name : "<UNKNOWN>");
141                         error_val = 1; /* pyrna_py_to_prop sets the error */
142                         break;
143                 }
144
145                 if (pyrna_py_to_prop(&ptr, prop, item)) {
146                         error_val= 1;
147                         break;
148                 }
149
150                 totkw--;
151         }
152
153         RNA_property_collection_end(&iter);
154
155         if (error_val==0 && totkw > 0) { /* some keywords were given that were not used :/ */
156                 PyObject *key, *value;
157                 Py_ssize_t pos = 0;
158
159                 while (PyDict_Next(kw, &pos, &key, &value)) {
160                         arg_name= _PyUnicode_AsString(key);
161                         if (RNA_struct_find_property(&ptr, arg_name) == NULL) break;
162                         arg_name= NULL;
163                 }
164
165                 PyErr_Format( PyExc_AttributeError, "argument \"%s\" unrecognized", arg_name ? arg_name : "<UNKNOWN>");
166                 error_val = 1;
167         }
168
169         if (error_val==0) {
170                 WM_operator_name_call(self->C, self->name, WM_OP_EXEC_DEFAULT, properties);
171         }
172
173         if (properties) {
174                 IDP_FreeProperty(properties);
175                 MEM_freeN(properties);
176         }
177
178         if (error_val) {
179                 return NULL;
180         }
181
182         Py_RETURN_NONE;
183 }
184
185 /*-----------------------BPy_OperatorBase method def------------------------------*/
186 PyTypeObject pyop_base_Type = {
187 #if (PY_VERSION_HEX >= 0x02060000)
188         PyVarObject_HEAD_INIT(&PyType_Type, 0)
189 #else
190         /* python 2.5 and below */
191         PyObject_HEAD_INIT( NULL )  /* required py macro */
192         0,                          /* ob_size */
193 #endif
194
195         "Operator",             /* tp_name */
196         sizeof( BPy_OperatorBase ),                     /* tp_basicsize */
197         0,                      /* tp_itemsize */
198         /* methods */
199         NULL,                                           /* tp_dealloc */
200         NULL,                       /* printfunc tp_print; */
201         NULL,                                           /* getattrfunc tp_getattr; */
202         NULL,                       /* setattrfunc tp_setattr; */
203         NULL,                                           /* tp_compare */
204         ( reprfunc ) pyop_base_repr,    /* tp_repr */
205
206         /* Method suites for standard classes */
207
208         NULL,                       /* PyNumberMethods *tp_as_number; */
209         NULL,                                           /* PySequenceMethods *tp_as_sequence; */
210         NULL,                                           /* PyMappingMethods *tp_as_mapping; */
211
212         /* More standard operations (here for binary compatibility) */
213
214         NULL,                                           /* hashfunc tp_hash; */
215         NULL,                       /* ternaryfunc tp_call; */
216         NULL,                       /* reprfunc tp_str; */
217         ( getattrofunc )pyop_base_getattro, /*PyObject_GenericGetAttr - MINGW Complains, assign later */        /* getattrofunc tp_getattro; */
218         NULL, /*PyObject_GenericSetAttr - MINGW Complains, assign later */      /* setattrofunc tp_setattro; */
219
220         /* Functions to access object as input/output buffer */
221         NULL,                       /* PyBufferProcs *tp_as_buffer; */
222
223   /*** Flags to define presence of optional/expanded features ***/
224         Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
225
226         NULL,                                           /*  char *tp_doc;  Documentation string */
227   /*** Assigned meaning in release 2.0 ***/
228         /* call function for all accessible objects */
229         NULL,                       /* traverseproc tp_traverse; */
230
231         /* delete references to contained objects */
232         NULL,                       /* inquiry tp_clear; */
233
234   /***  Assigned meaning in release 2.1 ***/
235   /*** rich comparisons ***/
236         NULL,                       /* richcmpfunc tp_richcompare; */
237
238   /***  weak reference enabler ***/
239         0,                          /* long tp_weaklistoffset; */
240
241   /*** Added in release 2.2 ***/
242         /*   Iterators */
243         NULL,                                           /* getiterfunc tp_iter; */
244         NULL,                       /* iternextfunc tp_iternext; */
245
246   /*** Attribute descriptor and subclassing stuff ***/
247         NULL,                                           /* struct PyMethodDef *tp_methods; */
248         NULL,                       /* struct PyMemberDef *tp_members; */
249         NULL,                                           /* struct PyGetSetDef *tp_getset; */
250         NULL,                       /* struct _typeobject *tp_base; */
251         NULL,                       /* PyObject *tp_dict; */
252         NULL,                       /* descrgetfunc tp_descr_get; */
253         NULL,                       /* descrsetfunc tp_descr_set; */
254         0,                          /* long tp_dictoffset; */
255         NULL,                       /* initproc tp_init; */
256         NULL,                       /* allocfunc tp_alloc; */
257         NULL,                                           /* newfunc tp_new; */
258         /*  Low-level free-memory routine */
259         NULL,                       /* freefunc tp_free;  */
260         /* For PyObject_IS_GC */
261         NULL,                       /* inquiry tp_is_gc;  */
262         NULL,                       /* PyObject *tp_bases; */
263         /* method resolution order */
264         NULL,                       /* PyObject *tp_mro;  */
265         NULL,                       /* PyObject *tp_cache; */
266         NULL,                       /* PyObject *tp_subclasses; */
267         NULL,                       /* PyObject *tp_weaklist; */
268         NULL
269 };
270
271 /*-----------------------BPy_OperatorBase method def------------------------------*/
272 PyTypeObject pyop_func_Type = {
273 #if (PY_VERSION_HEX >= 0x02060000)
274         PyVarObject_HEAD_INIT(&PyType_Type, 0)
275 #else
276         /* python 2.5 and below */
277         PyObject_HEAD_INIT( NULL )  /* required py macro */
278         0,                          /* ob_size */
279 #endif
280
281         "OperatorFunc",         /* tp_name */
282         sizeof( BPy_OperatorFunc ),                     /* tp_basicsize */
283         0,                      /* tp_itemsize */
284         /* methods */
285         NULL,                                           /* tp_dealloc */
286         NULL,                       /* printfunc tp_print; */
287         NULL,                                           /* getattrfunc tp_getattr; */
288         NULL,                       /* setattrfunc tp_setattr; */
289         ( cmpfunc ) pyop_func_compare,  /* tp_compare */
290         ( reprfunc ) pyop_func_repr,    /* tp_repr */
291
292         /* Method suites for standard classes */
293
294         NULL,                       /* PyNumberMethods *tp_as_number; */
295         NULL,                                           /* PySequenceMethods *tp_as_sequence; */
296         NULL,                                           /* PyMappingMethods *tp_as_mapping; */
297
298         /* More standard operations (here for binary compatibility) */
299
300         NULL,                                           /* hashfunc tp_hash; */
301         (ternaryfunc)pyop_func_call,                       /* ternaryfunc tp_call; */
302         NULL,                       /* reprfunc tp_str; */
303         NULL, /*PyObject_GenericGetAttr - MINGW Complains, assign later */      /* getattrofunc tp_getattro; */
304         NULL, /*PyObject_GenericSetAttr - MINGW Complains, assign later */      /* setattrofunc tp_setattro; */
305
306         /* Functions to access object as input/output buffer */
307         NULL,                       /* PyBufferProcs *tp_as_buffer; */
308
309   /*** Flags to define presence of optional/expanded features ***/
310         Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
311
312         NULL,                                           /*  char *tp_doc;  Documentation string */
313   /*** Assigned meaning in release 2.0 ***/
314         /* call function for all accessible objects */
315         NULL,                       /* traverseproc tp_traverse; */
316
317         /* delete references to contained objects */
318         NULL,                       /* inquiry tp_clear; */
319
320   /***  Assigned meaning in release 2.1 ***/
321   /*** rich comparisons ***/
322         NULL,                       /* richcmpfunc tp_richcompare; */
323
324   /***  weak reference enabler ***/
325         0,                          /* long tp_weaklistoffset; */
326
327   /*** Added in release 2.2 ***/
328         /*   Iterators */
329         NULL,                                           /* getiterfunc tp_iter; */
330         NULL,                       /* iternextfunc tp_iternext; */
331
332   /*** Attribute descriptor and subclassing stuff ***/
333         NULL,                                           /* struct PyMethodDef *tp_methods; */
334         NULL,                       /* struct PyMemberDef *tp_members; */
335         NULL,                                           /* struct PyGetSetDef *tp_getset; */
336         NULL,                       /* struct _typeobject *tp_base; */
337         NULL,                       /* PyObject *tp_dict; */
338         NULL,                       /* descrgetfunc tp_descr_get; */
339         NULL,                       /* descrsetfunc tp_descr_set; */
340         0,                          /* long tp_dictoffset; */
341         NULL,                       /* initproc tp_init; */
342         NULL,                       /* allocfunc tp_alloc; */
343         NULL,                                           /* newfunc tp_new; */
344         /*  Low-level free-memory routine */
345         NULL,                       /* freefunc tp_free;  */
346         /* For PyObject_IS_GC */
347         NULL,                       /* inquiry tp_is_gc;  */
348         NULL,                       /* PyObject *tp_bases; */
349         /* method resolution order */
350         NULL,                       /* PyObject *tp_mro;  */
351         NULL,                       /* PyObject *tp_cache; */
352         NULL,                       /* PyObject *tp_subclasses; */
353         NULL,                       /* PyObject *tp_weaklist; */
354         NULL
355 };
356
357 PyObject *pyop_base_CreatePyObject( bContext *C )
358 {
359         BPy_OperatorBase *pyop;
360
361         pyop = ( BPy_OperatorBase * ) PyObject_NEW( BPy_OperatorBase, &pyop_base_Type );
362
363         if( !pyop ) {
364                 PyErr_SetString( PyExc_MemoryError, "couldn't create BPy_OperatorBase object" );
365                 return NULL;
366         }
367
368         pyop->C = C; /* TODO - copy this? */
369
370         return ( PyObject * ) pyop;
371 }
372
373 PyObject *pyop_func_CreatePyObject( bContext *C, char *name )
374 {
375         BPy_OperatorFunc *pyop;
376
377         pyop = ( BPy_OperatorFunc * ) PyObject_NEW( BPy_OperatorFunc, &pyop_func_Type );
378
379         if( !pyop ) {
380                 PyErr_SetString( PyExc_MemoryError, "couldn't create BPy_OperatorFunc object" );
381                 return NULL;
382         }
383
384         strcpy(pyop->name, name);
385         pyop->C= C; /* TODO - how should contexts be dealt with? */
386
387         return ( PyObject * ) pyop;
388 }
389
390 PyObject *BPY_operator_module( bContext *C )
391 {
392         if( PyType_Ready( &pyop_base_Type ) < 0 )
393                 return NULL;
394
395         if( PyType_Ready( &pyop_func_Type ) < 0 )
396                 return NULL;
397
398         //submodule = Py_InitModule3( "operator", M_rna_methods, "rna module" );
399         return pyop_base_CreatePyObject(C);
400 }
401
402
403