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