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