merge from trunk #37722
[blender-staging.git] / source / blender / python / intern / bpy_props.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Campbell Barton
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/python/intern/bpy_props.c
26  *  \ingroup pythonintern
27  */
28
29
30 #include <Python.h>
31
32 #include "RNA_types.h"
33
34 #include "bpy_props.h"
35 #include "bpy_rna.h"
36 #include "bpy_util.h"
37
38 #include "BLI_utildefines.h"
39
40 #include "BKE_idprop.h"
41
42 #include "RNA_access.h"
43 #include "RNA_define.h" /* for defining our own rna */
44 #include "RNA_enum_types.h"
45
46 #include "MEM_guardedalloc.h"
47
48 #include "../generic/py_capi_utils.h"
49
50 /* initial definition of callback slots we'll probably have more then 1 */
51 #define BPY_DATA_CB_SLOT_SIZE 1
52
53 #define BPY_DATA_CB_SLOT_UPDATE 0
54
55 extern BPy_StructRNA *bpy_context_module;
56
57 static EnumPropertyItem property_flag_items[]= {
58         {PROP_HIDDEN, "HIDDEN", 0, "Hidden", ""},
59     {PROP_SKIP_SAVE, "SKIP_SAVE", 0, "Skip Save", ""},
60         {PROP_ANIMATABLE, "ANIMATABLE", 0, "Animateable", ""},
61         {0, NULL, 0, NULL, NULL}};
62
63 static EnumPropertyItem property_flag_enum_items[]= {
64         {PROP_HIDDEN, "HIDDEN", 0, "Hidden", ""},
65     {PROP_SKIP_SAVE, "SKIP_SAVE", 0, "Skip Save", ""},
66         {PROP_ANIMATABLE, "ANIMATABLE", 0, "Animateable", ""},
67         {PROP_ENUM_FLAG, "ENUM_FLAG", 0, "Enum Flag", ""},
68         {0, NULL, 0, NULL, NULL}};
69
70 /* subtypes */
71 static EnumPropertyItem property_subtype_string_items[]= {
72         {PROP_FILEPATH, "FILE_PATH", 0, "File Path", ""},
73         {PROP_DIRPATH, "DIR_PATH", 0, "Directory Path", ""},
74         {PROP_FILENAME, "FILENAME", 0, "Filename", ""},
75
76         {PROP_NONE, "NONE", 0, "None", ""},
77         {0, NULL, 0, NULL, NULL}};
78
79 static EnumPropertyItem property_subtype_number_items[]= {
80         {PROP_UNSIGNED, "UNSIGNED", 0, "Unsigned", ""},
81         {PROP_PERCENTAGE, "PERCENTAGE", 0, "Percentage", ""},
82         {PROP_FACTOR, "FACTOR", 0, "Factor", ""},
83         {PROP_ANGLE, "ANGLE", 0, "Angle", ""},
84         {PROP_TIME, "TIME", 0, "Time", ""},
85         {PROP_DISTANCE, "DISTANCE", 0, "Distance", ""},
86
87         {PROP_NONE, "NONE", 0, "None", ""},
88         {0, NULL, 0, NULL, NULL}};
89
90 static EnumPropertyItem property_subtype_array_items[]= {
91         {PROP_COLOR, "COLOR", 0, "Color", ""},
92         {PROP_TRANSLATION, "TRANSLATION", 0, "Translation", ""},
93         {PROP_DIRECTION, "DIRECTION", 0, "Direction", ""},
94         {PROP_VELOCITY, "VELOCITY", 0, "Velocity", ""},
95         {PROP_ACCELERATION, "ACCELERATION", 0, "Acceleration", ""},
96         {PROP_MATRIX, "MATRIX", 0, "Matrix", ""},
97         {PROP_EULER, "EULER", 0, "Euler", ""},
98         {PROP_QUATERNION, "QUATERNION", 0, "Quaternion", ""},
99         {PROP_AXISANGLE, "AXISANGLE", 0, "Axis Angle", ""},
100         {PROP_XYZ, "XYZ", 0, "XYZ", ""},
101         {PROP_COLOR_GAMMA, "COLOR_GAMMA", 0, "Color Gamma", ""},
102         {PROP_LAYER, "LAYER", 0, "Layer", ""},
103
104         {PROP_NONE, "NONE", 0, "None", ""},
105         {0, NULL, 0, NULL, NULL}};
106
107 /* PyObject's */
108 static PyObject *pymeth_BoolProperty= NULL;
109 static PyObject *pymeth_BoolVectorProperty= NULL;
110 static PyObject *pymeth_IntProperty= NULL;
111 static PyObject *pymeth_IntVectorProperty= NULL;
112 static PyObject *pymeth_FloatProperty= NULL;
113 static PyObject *pymeth_FloatVectorProperty= NULL;
114 static PyObject *pymeth_StringProperty= NULL;
115 static PyObject *pymeth_EnumProperty= NULL;
116 static PyObject *pymeth_PointerProperty= NULL;
117 static PyObject *pymeth_CollectionProperty= NULL;
118 static PyObject *pymeth_RemoveProperty= NULL;
119
120 PyObject *pyrna_struct_as_instance(PointerRNA *ptr)
121 {
122         PyObject *self= NULL;
123         /* first get self */
124         /* operators can store their own instance for later use */
125         if(ptr->data) {
126                 void **instance= RNA_struct_instance(ptr);
127
128                 if(instance) {
129                         if(*instance) {
130                                 self= *instance;
131                                 Py_INCREF(self);
132                         }
133                 }
134         }
135
136         /* in most cases this will run */
137         if(self == NULL) {
138                 self= pyrna_struct_CreatePyObject(ptr);
139         }
140
141         return self;
142 }
143
144 /* could be moved into bpy_utils */
145 static void printf_func_error(PyObject *py_func)
146 {
147         /* since we return to C code we can't leave the error */
148         PyCodeObject *f_code= (PyCodeObject *)PyFunction_GET_CODE(py_func);
149         PyErr_Print();
150         PyErr_Clear();
151
152         /* use py style error */
153         fprintf(stderr, "File \"%s\", line %d, in %s\n",
154                         _PyUnicode_AsString(f_code->co_filename),
155                         f_code->co_firstlineno,
156                         _PyUnicode_AsString(((PyFunctionObject *)py_func)->func_name)
157                         );
158 }
159
160 /* operators and classes use this so it can store the args given but defer
161  * running it until the operator runs where these values are used to setup
162  * the default args for that operator instance */
163 static PyObject *bpy_prop_deferred_return(PyObject *func, PyObject *kw)
164 {
165         PyObject *ret= PyTuple_New(2);
166         PyTuple_SET_ITEM(ret, 0, func);
167         Py_INCREF(func);
168
169         if(kw==NULL)
170                 kw= PyDict_New();
171         else
172                 Py_INCREF(kw);
173
174         PyTuple_SET_ITEM(ret, 1, kw);
175
176         return ret;
177 }
178
179 /* callbacks */
180 void bpy_prop_update_cb(struct bContext *C, struct PointerRNA *ptr, struct PropertyRNA *prop)
181 {
182         PyGILState_STATE gilstate;
183         PyObject **py_data= (PyObject **)RNA_property_py_data_get(prop);
184         PyObject *py_func;
185         PyObject *args;
186         PyObject *self;
187         PyObject *ret;
188         const int is_write_ok= pyrna_write_check();
189
190         BLI_assert(py_data != NULL);
191
192         if(!is_write_ok) {
193                 pyrna_write_set(TRUE);
194         }
195
196         bpy_context_set(C, &gilstate);
197
198         py_func= py_data[BPY_DATA_CB_SLOT_UPDATE];
199
200         args= PyTuple_New(2);
201         self= pyrna_struct_as_instance(ptr);
202         PyTuple_SET_ITEM(args, 0, self);
203
204         PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
205         Py_INCREF(bpy_context_module);
206
207         ret= PyObject_CallObject(py_func, args);
208
209         Py_DECREF(args);
210
211         if(ret == NULL) {
212                 printf_func_error(py_func);
213         }
214         else {
215                 if(ret != Py_None) {
216                         PyErr_SetString(PyExc_ValueError, "the return value must be None");
217                         printf_func_error(py_func);
218                 }
219
220                 Py_DECREF(ret);
221         }
222
223         bpy_context_clear(C, &gilstate);
224
225         if(!is_write_ok) {
226                 pyrna_write_set(FALSE);
227         }
228 }
229
230 static int bpy_prop_callback_check(PyObject *py_func, int argcount)
231 {
232         if(py_func) {
233                 if(!PyFunction_Check(py_func)) {
234                         PyErr_Format(PyExc_TypeError,
235                                      "update keyword: expected a function type, not a %.200s",
236                                      Py_TYPE(py_func)->tp_name);
237                         return -1;
238                 }
239                 else {
240                         PyCodeObject *f_code= (PyCodeObject *)PyFunction_GET_CODE(py_func);
241                         if (f_code->co_argcount != argcount) {
242                                 PyErr_Format(PyExc_TypeError,
243                                              "update keyword: expected a function taking %d arguments, not %d",
244                                              argcount, f_code->co_argcount);
245                                 return -1;
246                         }
247                 }
248         }
249
250         return 0;
251 }
252
253
254 static int bpy_prop_callback_assign(struct PropertyRNA *prop, PyObject *update_cb)
255 {
256         /* assume this is already checked for type and arg length */
257         if(update_cb) {
258                 PyObject **py_data= MEM_callocN(sizeof(PyObject *) * BPY_DATA_CB_SLOT_SIZE, "bpy_prop_callback_assign");
259                 RNA_def_property_update_runtime(prop, (void *)bpy_prop_update_cb);
260                 py_data[BPY_DATA_CB_SLOT_UPDATE]= update_cb;
261                 RNA_def_py_data(prop, py_data);
262
263                 RNA_def_property_flag(prop, PROP_CONTEXT_PROPERTY_UPDATE);
264         }
265
266         return 0;
267 }
268
269 /* this define runs at the start of each function and deals with 
270  * returning a deferred property (to be registered later) */
271 #define BPY_PROPDEF_HEAD(_func) \
272         if (PyTuple_GET_SIZE(args) == 1) { \
273                 PyObject *ret; \
274                 self= PyTuple_GET_ITEM(args, 0); \
275                 args= PyTuple_New(0); \
276                 ret= BPy_##_func(self, args, kw); \
277                 Py_DECREF(args); \
278                 return ret; \
279         } \
280         else if (PyTuple_GET_SIZE(args) > 1) { \
281                 PyErr_SetString(PyExc_ValueError, "all args must be keywords"); \
282                 return NULL; \
283         } \
284         srna= srna_from_self(self, #_func"(...):"); \
285         if(srna==NULL) { \
286                 if(PyErr_Occurred()) \
287                         return NULL; \
288                 return bpy_prop_deferred_return((void *)pymeth_##_func, kw); \
289         } \
290
291 /* terse macros for error checks shared between all funcs cant use function
292  * calls because of static strins passed to pyrna_set_to_enum_bitfield */
293 #define BPY_PROPDEF_CHECK(_func, _property_flag_items) \
294         if(id_len >= MAX_IDPROP_NAME) { \
295                 PyErr_Format(PyExc_TypeError, \
296                              #_func"(): '%.200s' too long, max length is %d", \
297                              id, MAX_IDPROP_NAME-1); \
298                 return NULL; \
299         } \
300         if(RNA_def_property_free_identifier(srna, id) == -1) { \
301                 PyErr_Format(PyExc_TypeError, \
302                              #_func"(): '%s' is defined as a non-dynamic type", \
303                              id); \
304                 return NULL; \
305         } \
306         if(pyopts && pyrna_set_to_enum_bitfield(_property_flag_items, pyopts, &opts, #_func"(options={...}):")) \
307                 return NULL; \
308
309 #define BPY_PROPDEF_SUBTYPE_CHECK(_func, _property_flag_items, _subtype) \
310         BPY_PROPDEF_CHECK(_func, _property_flag_items) \
311         if(pysubtype && RNA_enum_value_from_id(_subtype, pysubtype, &subtype)==0) { \
312                 PyErr_Format(PyExc_TypeError, \
313                              #_func"(subtype='%s'): invalid subtype", \
314                              pysubtype); \
315                 return NULL; \
316         } \
317
318
319 #define BPY_PROPDEF_NAME_DOC \
320 "   :arg name: Name used in the user interface.\n" \
321 "   :type name: string\n" \
322
323
324 #define BPY_PROPDEF_DESC_DOC \
325 "   :arg description: Text used for the tooltip and api documentation.\n" \
326 "   :type description: string\n" \
327
328
329 #define BPY_PROPDEF_UPDATE_DOC \
330 "   :arg update: function to be called when this value is modified,\n" \
331 "      This function must take 2 values (self, context) and return None.\n" \
332 "   :type update: function\n" \
333
334 #if 0
335 static int bpy_struct_id_used(StructRNA *srna, char *identifier)
336 {
337         PointerRNA ptr;
338         RNA_pointer_create(NULL, srna, NULL, &ptr);
339         return (RNA_struct_find_property(&ptr, identifier) != NULL);
340 }
341 #endif
342
343
344 /* Function that sets RNA, NOTE - self is NULL when called from python, but being abused from C so we can pass the srna allong
345  * This isnt incorrect since its a python object - but be careful */
346 PyDoc_STRVAR(BPy_BoolProperty_doc,
347 ".. function:: BoolProperty(name=\"\", description=\"\", default=False, options={'ANIMATABLE'}, subtype='NONE', update=None)\n"
348 "\n"
349 "   Returns a new boolean property definition.\n"
350 "\n"
351 BPY_PROPDEF_NAME_DOC
352 BPY_PROPDEF_DESC_DOC
353 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
354 "   :type options: set\n"
355 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
356 "   :type subtype: string\n"
357 BPY_PROPDEF_UPDATE_DOC
358 );
359 static PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
360 {
361         StructRNA *srna;
362
363         BPY_PROPDEF_HEAD(BoolProperty)
364
365         if(srna) {
366                 static const char *kwlist[]= {"attr", "name", "description", "default", "options", "subtype", "update", NULL};
367                 const char *id=NULL, *name="", *description="";
368                 int id_len;
369                 int def=0;
370                 PropertyRNA *prop;
371                 PyObject *pyopts= NULL;
372                 int opts=0;
373                 char *pysubtype= NULL;
374                 int subtype= PROP_NONE;
375                 PyObject *update_cb= NULL;
376
377                 if (!PyArg_ParseTupleAndKeywords(args, kw,
378                                                  "s#|ssiO!sO:BoolProperty",
379                                                  (char **)kwlist, &id, &id_len,
380                                                  &name, &description, &def,
381                                                  &PySet_Type, &pyopts, &pysubtype,
382                                                  &update_cb))
383                 {
384                         return NULL;
385                 }
386
387                 BPY_PROPDEF_SUBTYPE_CHECK(BoolProperty, property_flag_items, property_subtype_number_items)
388
389                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
390                         return NULL;
391                 }
392
393                 prop= RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
394                 RNA_def_property_boolean_default(prop, def);
395                 RNA_def_property_ui_text(prop, name, description);
396
397                 if(pyopts) {
398                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
399                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
400                 }
401                 bpy_prop_callback_assign(prop, update_cb);
402                 RNA_def_property_duplicate_pointers(srna, prop);
403         }
404
405         Py_RETURN_NONE;
406 }
407
408 PyDoc_STRVAR(BPy_BoolVectorProperty_doc,
409 ".. function:: BoolVectorProperty(name=\"\", description=\"\", default=(False, False, False), options={'ANIMATABLE'}, subtype='NONE', size=3, update=None)\n"
410 "\n"
411 "   Returns a new vector boolean property definition.\n"
412 "\n"
413 BPY_PROPDEF_NAME_DOC
414 BPY_PROPDEF_DESC_DOC
415 "   :arg default: sequence of booleans the length of *size*.\n"
416 "   :type default: sequence\n"
417 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
418 "   :type options: set\n"
419 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
420 "   :type subtype: string\n"
421 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
422 "   :type size: int\n"
423 BPY_PROPDEF_UPDATE_DOC
424 );
425 static PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
426 {
427         StructRNA *srna;
428
429         BPY_PROPDEF_HEAD(BoolVectorProperty)
430
431         if(srna) {
432                 static const char *kwlist[]= {"attr", "name", "description", "default", "options", "subtype", "size", "update", NULL};
433                 const char *id=NULL, *name="", *description="";
434                 int id_len;
435                 int def[PYRNA_STACK_ARRAY]={0};
436                 int size=3;
437                 PropertyRNA *prop;
438                 PyObject *pydef= NULL;
439                 PyObject *pyopts= NULL;
440                 int opts=0;
441                 char *pysubtype= NULL;
442                 int subtype= PROP_NONE;
443                 PyObject *update_cb= NULL;
444
445                 if (!PyArg_ParseTupleAndKeywords(args, kw,
446                                                  "s#|ssOO!siO:BoolVectorProperty",
447                                                  (char **)kwlist, &id, &id_len,
448                                                  &name, &description, &pydef,
449                                                  &PySet_Type, &pyopts, &pysubtype, &size,
450                                                  &update_cb))
451                 {
452                         return NULL;
453                 }
454
455                 BPY_PROPDEF_SUBTYPE_CHECK(BoolVectorProperty, property_flag_items, property_subtype_array_items)
456
457                 if(size < 1 || size > PYRNA_STACK_ARRAY) {
458                         PyErr_Format(PyExc_TypeError, "BoolVectorProperty(size=%d): size must be between 0 and " STRINGIFY(PYRNA_STACK_ARRAY), size);
459                         return NULL;
460                 }
461
462                 if(pydef && PyC_AsArray(def, pydef, size, &PyBool_Type, FALSE, "BoolVectorProperty(default=sequence)") < 0)
463                         return NULL;
464
465                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
466                         return NULL;
467                 }
468
469                 // prop= RNA_def_boolean_array(srna, id, size, pydef ? def:NULL, name, description);
470                 prop= RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
471                 RNA_def_property_array(prop, size);
472                 if(pydef) RNA_def_property_boolean_array_default(prop, def);
473                 RNA_def_property_ui_text(prop, name, description);
474
475                 if(pyopts) {
476                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
477                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
478                 }
479                 bpy_prop_callback_assign(prop, update_cb);
480                 RNA_def_property_duplicate_pointers(srna, prop);
481         }
482         
483         Py_RETURN_NONE;
484 }
485
486 PyDoc_STRVAR(BPy_IntProperty_doc,
487 ".. function:: IntProperty(name=\"\", description=\"\", default=0, min=-sys.maxint, max=sys.maxint, soft_min=-sys.maxint, soft_max=sys.maxint, step=1, options={'ANIMATABLE'}, subtype='NONE', update=None)\n"
488 "\n"
489 "   Returns a new int property definition.\n"
490 "\n"
491 BPY_PROPDEF_NAME_DOC
492 BPY_PROPDEF_DESC_DOC
493 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
494 "   :type options: set\n"
495 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
496 "   :type subtype: string\n"
497 BPY_PROPDEF_UPDATE_DOC
498 );
499 static PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
500 {
501         StructRNA *srna;
502
503         BPY_PROPDEF_HEAD(IntProperty)
504
505         if(srna) {
506                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", "update", NULL};
507                 const char *id=NULL, *name="", *description="";
508                 int id_len;
509                 int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def=0;
510                 PropertyRNA *prop;
511                 PyObject *pyopts= NULL;
512                 int opts=0;
513                 char *pysubtype= NULL;
514                 int subtype= PROP_NONE;
515                 PyObject *update_cb= NULL;
516
517                 if (!PyArg_ParseTupleAndKeywords(args, kw,
518                                                  "s#|ssiiiiiiO!sO:IntProperty",
519                                                  (char **)kwlist, &id, &id_len,
520                                                  &name, &description, &def,
521                                                  &min, &max, &soft_min, &soft_max,
522                                                  &step, &PySet_Type, &pyopts, &pysubtype,
523                                                  &update_cb))
524                 {
525                         return NULL;
526                 }
527
528                 BPY_PROPDEF_SUBTYPE_CHECK(IntProperty, property_flag_items, property_subtype_number_items)
529
530                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
531                         return NULL;
532                 }
533
534                 prop= RNA_def_property(srna, id, PROP_INT, subtype);
535                 RNA_def_property_int_default(prop, def);
536                 RNA_def_property_range(prop, min, max);
537                 RNA_def_property_ui_text(prop, name, description);
538                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
539
540                 if(pyopts) {
541                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
542                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
543                 }
544                 bpy_prop_callback_assign(prop, update_cb);
545                 RNA_def_property_duplicate_pointers(srna, prop);
546         }
547         Py_RETURN_NONE;
548 }
549
550 PyDoc_STRVAR(BPy_IntVectorProperty_doc,
551 ".. function:: IntVectorProperty(name=\"\", description=\"\", default=(0, 0, 0), min=-sys.maxint, max=sys.maxint, soft_min=-sys.maxint, soft_max=sys.maxint, options={'ANIMATABLE'}, subtype='NONE', size=3, update=None)\n"
552 "\n"
553 "   Returns a new vector int property definition.\n"
554 "\n"
555 BPY_PROPDEF_NAME_DOC
556 BPY_PROPDEF_DESC_DOC
557 "   :arg default: sequence of ints the length of *size*.\n"
558 "   :type default: sequence\n"
559 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
560 "   :type options: set\n"
561 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
562 "   :type subtype: string\n"
563 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
564 "   :type size: int\n"
565 BPY_PROPDEF_UPDATE_DOC
566 );
567 static PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
568 {
569         StructRNA *srna;
570
571         BPY_PROPDEF_HEAD(IntVectorProperty)
572
573         if(srna) {
574                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", "size", "update", NULL};
575                 const char *id=NULL, *name="", *description="";
576                 int id_len;
577                 int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def[PYRNA_STACK_ARRAY]={0};
578                 int size=3;
579                 PropertyRNA *prop;
580                 PyObject *pydef= NULL;
581                 PyObject *pyopts= NULL;
582                 int opts=0;
583                 char *pysubtype= NULL;
584                 int subtype= PROP_NONE;
585                 PyObject *update_cb= NULL;
586
587                 if (!PyArg_ParseTupleAndKeywords(args, kw,
588                                                  "s#|ssOiiiiiO!siO:IntVectorProperty",
589                                                  (char **)kwlist, &id, &id_len,
590                                                  &name, &description, &pydef,
591                                                  &min, &max, &soft_min, &soft_max,
592                                                  &step, &PySet_Type, &pyopts,
593                                                  &pysubtype, &size,
594                                                  &update_cb))
595                 {
596                         return NULL;
597                 }
598
599                 BPY_PROPDEF_SUBTYPE_CHECK(IntVectorProperty, property_flag_items, property_subtype_array_items)
600
601                 if(size < 1 || size > PYRNA_STACK_ARRAY) {
602                         PyErr_Format(PyExc_TypeError, "IntVectorProperty(size=%d): size must be between 0 and " STRINGIFY(PYRNA_STACK_ARRAY), size);
603                         return NULL;
604                 }
605
606                 if(pydef && PyC_AsArray(def, pydef, size, &PyLong_Type, FALSE, "IntVectorProperty(default=sequence)") < 0)
607                         return NULL;
608
609                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
610                         return NULL;
611                 }
612
613                 prop= RNA_def_property(srna, id, PROP_INT, subtype);
614                 RNA_def_property_array(prop, size);
615                 if(pydef) RNA_def_property_int_array_default(prop, def);
616                 RNA_def_property_range(prop, min, max);
617                 RNA_def_property_ui_text(prop, name, description);
618                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
619
620                 if(pyopts) {
621                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
622                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
623                 }
624                 bpy_prop_callback_assign(prop, update_cb);
625                 RNA_def_property_duplicate_pointers(srna, prop);
626         }
627         Py_RETURN_NONE;
628 }
629
630
631 PyDoc_STRVAR(BPy_FloatProperty_doc,
632 ".. function:: FloatProperty(name=\"\", description=\"\", default=0.0, min=sys.float_info.min, max=sys.float_info.max, soft_min=sys.float_info.min, soft_max=sys.float_info.max, step=3, precision=2, options={'ANIMATABLE'}, subtype='NONE', unit='NONE', update=None)\n"
633 "\n"
634 "   Returns a new float property definition.\n"
635 "\n"
636 BPY_PROPDEF_NAME_DOC
637 BPY_PROPDEF_DESC_DOC
638 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
639 "   :type options: set\n"
640 "   :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n"
641 "   :type subtype: string\n"
642 "   :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION'].\n"
643 "   :type unit: string\n"
644 BPY_PROPDEF_UPDATE_DOC
645 );
646 static PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
647 {
648         StructRNA *srna;
649
650         BPY_PROPDEF_HEAD(FloatProperty)
651
652         if(srna) {
653                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "unit", "update", NULL};
654                 const char *id=NULL, *name="", *description="";
655                 int id_len;
656                 float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def=0.0f;
657                 int precision= 2;
658                 PropertyRNA *prop;
659                 PyObject *pyopts= NULL;
660                 int opts=0;
661                 char *pysubtype= NULL;
662                 int subtype= PROP_NONE;
663                 char *pyunit= NULL;
664                 int unit= PROP_UNIT_NONE;
665                 PyObject *update_cb= NULL;
666
667                 if (!PyArg_ParseTupleAndKeywords(args, kw,
668                                                  "s#|ssffffffiO!ssO:FloatProperty",
669                                                  (char **)kwlist, &id, &id_len,
670                                                  &name, &description, &def,
671                                                  &min, &max, &soft_min, &soft_max,
672                                                  &step, &precision, &PySet_Type,
673                                                  &pyopts, &pysubtype, &pyunit,
674                                                  &update_cb))
675                 {
676                         return NULL;
677                 }
678
679                 BPY_PROPDEF_SUBTYPE_CHECK(FloatProperty, property_flag_items, property_subtype_number_items)
680
681                 if(pyunit && RNA_enum_value_from_id(property_unit_items, pyunit, &unit)==0) {
682                         PyErr_Format(PyExc_TypeError, "FloatProperty(unit='%s'): invalid unit");
683                         return NULL;
684                 }
685
686                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
687                         return NULL;
688                 }
689
690                 prop= RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
691                 RNA_def_property_float_default(prop, def);
692                 RNA_def_property_range(prop, min, max);
693                 RNA_def_property_ui_text(prop, name, description);
694                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
695
696                 if(pyopts) {
697                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
698                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
699                 }
700                 bpy_prop_callback_assign(prop, update_cb);
701                 RNA_def_property_duplicate_pointers(srna, prop);
702         }
703         Py_RETURN_NONE;
704 }
705
706 PyDoc_STRVAR(BPy_FloatVectorProperty_doc,
707 ".. function:: FloatVectorProperty(name=\"\", description=\"\", default=(0.0, 0.0, 0.0), min=sys.float_info.min, max=sys.float_info.max, soft_min=sys.float_info.min, soft_max=sys.float_info.max, step=3, precision=2, options={'ANIMATABLE'}, subtype='NONE', size=3, update=None)\n"
708 "\n"
709 "   Returns a new vector float property definition.\n"
710 "\n"
711 BPY_PROPDEF_NAME_DOC
712 BPY_PROPDEF_DESC_DOC
713 "   :arg default: sequence of floats the length of *size*.\n"
714 "   :type default: sequence\n"
715 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
716 "   :type options: set\n"
717 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', 'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', 'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n"
718 "   :type subtype: string\n"
719 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
720 "   :type size: int\n"
721 BPY_PROPDEF_UPDATE_DOC
722 );
723 static PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
724 {
725         StructRNA *srna;
726
727         BPY_PROPDEF_HEAD(FloatVectorProperty)
728
729         if(srna) {
730                 static const char *kwlist[]= {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "size", "update", NULL};
731                 const char *id=NULL, *name="", *description="";
732                 int id_len;
733                 float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def[PYRNA_STACK_ARRAY]={0.0f};
734                 int precision= 2, size=3;
735                 PropertyRNA *prop;
736                 PyObject *pydef= NULL;
737                 PyObject *pyopts= NULL;
738                 int opts=0;
739                 char *pysubtype= NULL;
740                 int subtype= PROP_NONE;
741                 PyObject *update_cb= NULL;
742
743                 if (!PyArg_ParseTupleAndKeywords(args, kw,
744                                                  "s#|ssOfffffiO!siO:FloatVectorProperty",
745                                                  (char **)kwlist, &id, &id_len,
746                                                  &name, &description, &pydef,
747                                                  &min, &max, &soft_min, &soft_max,
748                                                  &step, &precision, &PySet_Type,
749                                                  &pyopts, &pysubtype, &size,
750                                                  &update_cb))
751                 {
752                         return NULL;
753                 }
754
755                 BPY_PROPDEF_SUBTYPE_CHECK(FloatVectorProperty, property_flag_items, property_subtype_array_items)
756
757                 if(size < 1 || size > PYRNA_STACK_ARRAY) {
758                         PyErr_Format(PyExc_TypeError, "FloatVectorProperty(size=%d): size must be between 0 and " STRINGIFY(PYRNA_STACK_ARRAY), size);
759                         return NULL;
760                 }
761
762                 if(pydef && PyC_AsArray(def, pydef, size, &PyFloat_Type, FALSE, "FloatVectorProperty(default=sequence)") < 0)
763                         return NULL;
764
765                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
766                         return NULL;
767                 }
768
769                 prop= RNA_def_property(srna, id, PROP_FLOAT, subtype);
770                 RNA_def_property_array(prop, size);
771                 if(pydef) RNA_def_property_float_array_default(prop, def);
772                 RNA_def_property_range(prop, min, max);
773                 RNA_def_property_ui_text(prop, name, description);
774                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
775
776                 if(pyopts) {
777                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
778                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
779                 }
780                 bpy_prop_callback_assign(prop, update_cb);
781                 RNA_def_property_duplicate_pointers(srna, prop);
782         }
783         Py_RETURN_NONE;
784 }
785
786 PyDoc_STRVAR(BPy_StringProperty_doc,
787 ".. function:: StringProperty(name=\"\", description=\"\", default=\"\", maxlen=0, options={'ANIMATABLE'}, subtype='NONE', update=None)\n"
788 "\n"
789 "   Returns a new string property definition.\n"
790 "\n"
791 BPY_PROPDEF_NAME_DOC
792 BPY_PROPDEF_DESC_DOC
793 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
794 "   :type options: set\n"
795 "   :arg subtype: Enumerator in ['FILE_PATH', 'DIR_PATH', 'FILENAME', 'NONE'].\n"
796 "   :type subtype: string\n"
797 BPY_PROPDEF_UPDATE_DOC
798 );
799 static PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
800 {
801         StructRNA *srna;
802
803         BPY_PROPDEF_HEAD(StringProperty)
804
805         if(srna) {
806                 static const char *kwlist[]= {"attr", "name", "description", "default", "maxlen", "options", "subtype", "update", NULL};
807                 const char *id=NULL, *name="", *description="", *def="";
808                 int id_len;
809                 int maxlen=0;
810                 PropertyRNA *prop;
811                 PyObject *pyopts= NULL;
812                 int opts=0;
813                 char *pysubtype= NULL;
814                 int subtype= PROP_NONE;
815                 PyObject *update_cb= NULL;
816
817                 if (!PyArg_ParseTupleAndKeywords(args, kw,
818                                                  "s#|sssiO!sO:StringProperty",
819                                                  (char **)kwlist, &id, &id_len,
820                                                  &name, &description, &def,
821                                                  &maxlen, &PySet_Type, &pyopts, &pysubtype,
822                                                  &update_cb))
823                 {
824                         return NULL;
825                 }
826
827                 BPY_PROPDEF_SUBTYPE_CHECK(StringProperty, property_flag_items, property_subtype_string_items)
828
829                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
830                         return NULL;
831                 }
832
833                 prop= RNA_def_property(srna, id, PROP_STRING, subtype);
834                 if(maxlen != 0) RNA_def_property_string_maxlength(prop, maxlen + 1); /* +1 since it includes null terminator */
835                 if(def) RNA_def_property_string_default(prop, def);
836                 RNA_def_property_ui_text(prop, name, description);
837
838                 if(pyopts) {
839                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
840                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
841                 }
842                 bpy_prop_callback_assign(prop, update_cb);
843                 RNA_def_property_duplicate_pointers(srna, prop);
844         }
845         Py_RETURN_NONE;
846 }
847
848 #if 0
849 /* copies orig to buf, then sets orig to buf, returns copy length */
850 static size_t strswapbufcpy(char *buf, const char **orig)
851 {
852         const char *src= *orig;
853         char *dst= buf;
854         size_t i= 0;
855         *orig= buf;
856         while((*dst= *src)) { dst++; src++; i++; }
857         return i + 1; /* include '\0' */
858 }
859 #endif
860
861 static EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, PyObject *def, int *defvalue, const short is_enum_flag)
862 {
863         EnumPropertyItem *items;
864         PyObject *item;
865         const Py_ssize_t seq_len= PySequence_Fast_GET_SIZE(seq_fast);
866         Py_ssize_t totbuf= 0;
867         int i;
868         short def_used= 0;
869         const char *def_cmp= NULL;
870
871         if(is_enum_flag) {
872                 if(seq_len > RNA_ENUM_BITFLAG_SIZE) {
873                         PyErr_SetString(PyExc_TypeError, "EnumProperty(...): maximum " STRINGIFY(RNA_ENUM_BITFLAG_SIZE) " members for a ENUM_FLAG type property");
874                         return NULL;
875                 }
876                 if(def && !PySet_Check(def)) {
877                         PyErr_Format(PyExc_TypeError,
878                                      "EnumProperty(...): default option must be a 'set' "
879                                      "type when ENUM_FLAG is enabled, not a '%.200s'",
880                                      Py_TYPE(def)->tp_name);
881                         return NULL;
882                 }
883         }
884         else {
885                 if(def) {
886                         def_cmp= _PyUnicode_AsString(def);
887                         if(def_cmp==NULL) {
888                                 PyErr_Format(PyExc_TypeError,
889                                              "EnumProperty(...): default option must be a 'str' "
890                                              "type when ENUM_FLAG is disabled, not a '%.200s'",
891                                              Py_TYPE(def)->tp_name);
892                                 return NULL;
893                         }
894                 }
895         }
896
897         /* blank value */
898         *defvalue= 0;
899
900         items= MEM_callocN(sizeof(EnumPropertyItem) * (seq_len + 1), "enum_items_from_py1");
901
902         for(i=0; i<seq_len; i++) {
903                 EnumPropertyItem tmp= {0, "", 0, "", ""};
904                 Py_ssize_t id_str_size;
905                 Py_ssize_t name_str_size;
906                 Py_ssize_t desc_str_size;
907
908                 item= PySequence_Fast_GET_ITEM(seq_fast, i);
909
910                 if(             (PyTuple_CheckExact(item)) &&
911                         (PyTuple_GET_SIZE(item) == 3) &&
912                         (tmp.identifier=  _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 0), &id_str_size)) &&
913                         (tmp.name=        _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 1), &name_str_size)) &&
914                         (tmp.description= _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 2), &desc_str_size))
915                 ) {
916                         if(is_enum_flag) {
917                                 tmp.value= 1<<i;
918
919                                 if(def && PySet_Contains(def, PyTuple_GET_ITEM(item, 0))) {
920                                         *defvalue |= tmp.value;
921                                         def_used++;
922                                 }
923                         }
924                         else {
925                                 tmp.value= i;
926
927                                 if(def && def_used == 0 && strcmp(def_cmp, tmp.identifier)==0) {
928                                         *defvalue= tmp.value;
929                                         def_used++; /* only ever 1 */
930                                 }
931                         }
932
933                         items[i]= tmp;
934
935                         /* calculate combine string length */
936                         totbuf += id_str_size + name_str_size + desc_str_size + 3; /* 3 is for '\0's */
937                 }
938                 else {
939                         MEM_freeN(items);
940                         PyErr_SetString(PyExc_TypeError, "EnumProperty(...): expected an tuple containing (identifier, name description)");
941                         return NULL;
942                 }
943
944         }
945
946         if(is_enum_flag) {
947                 /* strict check that all set members were used */
948                 if(def && def_used != PySet_GET_SIZE(def)) {
949                         MEM_freeN(items);
950
951                         PyErr_Format(PyExc_TypeError,
952                                      "EnumProperty(..., default={...}): set has %d unused member(s)",
953                                      PySet_GET_SIZE(def) - def_used);
954                         return NULL;
955                 }
956         }
957         else {
958                 if(def && def_used == 0) {
959                         MEM_freeN(items);
960
961                         PyErr_Format(PyExc_TypeError,
962                                      "EnumProperty(..., default=\'%s\'): not found in enum members",
963                                      def);
964                         return NULL;
965                 }
966         }
967
968         /* disabled duplicating strings because the array can still be freed and
969          * the strings from it referenced, for now we can't support dynamically
970          * created strings from python. */
971 #if 0
972         /* this would all work perfectly _but_ the python strings may be freed
973          * immediately after use, so we need to duplicate them, ugh.
974          * annoying because it works most of the time without this. */
975         {
976                 EnumPropertyItem *items_dup= MEM_mallocN((sizeof(EnumPropertyItem) * (seq_len + 1)) + (sizeof(char) * totbuf), "enum_items_from_py2");
977                 EnumPropertyItem *items_ptr= items_dup;
978                 char *buf= ((char *)items_dup) + (sizeof(EnumPropertyItem) * (seq_len + 1));
979                 memcpy(items_dup, items, sizeof(EnumPropertyItem) * (seq_len + 1));
980                 for(i=0; i<seq_len; i++, items_ptr++) {
981                         buf += strswapbufcpy(buf, &items_ptr->identifier);
982                         buf += strswapbufcpy(buf, &items_ptr->name);
983                         buf += strswapbufcpy(buf, &items_ptr->description);
984                 }
985                 MEM_freeN(items);
986                 items=items_dup;
987         }
988         /* end string duplication */
989 #endif
990
991         return items;
992 }
993
994 static EnumPropertyItem *bpy_props_enum_itemf(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, int *free)
995 {
996         PyGILState_STATE gilstate;
997
998         PyObject *py_func= RNA_property_enum_py_data_get(prop);
999         PyObject *self= NULL;
1000         PyObject *args;
1001         PyObject *items; /* returned from the function call */
1002
1003         EnumPropertyItem *eitems= NULL;
1004         int err= 0;
1005
1006         bpy_context_set(C, &gilstate);
1007
1008         args= PyTuple_New(2);
1009         self= pyrna_struct_as_instance(ptr);
1010         PyTuple_SET_ITEM(args, 0, self);
1011
1012         /* now get the context */
1013         PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
1014         Py_INCREF(bpy_context_module);
1015
1016         items= PyObject_CallObject(py_func, args);
1017
1018         Py_DECREF(args);
1019
1020         if(items==NULL) {
1021                 err= -1;
1022         }
1023         else {
1024                 PyObject *items_fast;
1025                 int defvalue_dummy=0;
1026
1027                 if(!(items_fast= PySequence_Fast(items, "EnumProperty(...): return value from the callback was not a sequence"))) {
1028                         err= -1;
1029                 }
1030                 else {
1031                         eitems= enum_items_from_py(items_fast, NULL, &defvalue_dummy, (RNA_property_flag(prop) & PROP_ENUM_FLAG)!=0);
1032
1033                         Py_DECREF(items_fast);
1034
1035                         if(!eitems) {
1036                                 err= -1;
1037                         }
1038                 }
1039
1040                 Py_DECREF(items);
1041         }
1042
1043         if(err != -1) { /* worked */
1044                 *free= 1;
1045         }
1046         else {
1047                 printf_func_error(py_func);
1048
1049                 eitems= DummyRNA_NULL_items;
1050         }
1051
1052
1053         bpy_context_clear(C, &gilstate);
1054         return eitems;
1055 }
1056
1057 PyDoc_STRVAR(BPy_EnumProperty_doc,
1058 ".. function:: EnumProperty(items, name=\"\", description=\"\", default=\"\", options={'ANIMATABLE'}, update=None)\n"
1059 "\n"
1060 "   Returns a new enumerator property definition.\n"
1061 "\n"
1062 BPY_PROPDEF_NAME_DOC
1063 BPY_PROPDEF_DESC_DOC
1064 "   :arg default: The default value for this enum, A string when *ENUM_FLAG*\n"
1065 "      is disabled otherwise a set which may only contain string identifiers\n"
1066 "      used in *items*.\n"
1067 "   :type default: string or set\n"
1068 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE', 'ENUM_FLAG'].\n"
1069 "   :type options: set\n"
1070 "   :arg items: sequence of enum items formatted:\n"
1071 "      [(identifier, name, description), ...] where the identifier is used\n"
1072 "      for python access and other values are used for the interface.\n"
1073 "      For dynamic values a callback can be passed which returns a list in\n"
1074 "      the same format as the static list.\n"
1075 "      This function must take 2 arguments (self, context)\n"
1076 "   :type items: sequence of string triplets or a function\n"
1077 BPY_PROPDEF_UPDATE_DOC
1078 );
1079 static PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw)
1080 {
1081         StructRNA *srna;
1082
1083         BPY_PROPDEF_HEAD(EnumProperty)
1084         
1085         if(srna) {
1086                 static const char *kwlist[]= {"attr", "items", "name", "description", "default", "options", "update", NULL};
1087                 const char *id=NULL, *name="", *description="";
1088                 PyObject *def= NULL;
1089                 int id_len;
1090                 int defvalue=0;
1091                 PyObject *items, *items_fast;
1092                 EnumPropertyItem *eitems;
1093                 PropertyRNA *prop;
1094                 PyObject *pyopts= NULL;
1095                 int opts=0;
1096                 short is_itemf= FALSE;
1097                 PyObject *update_cb= NULL;
1098
1099                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1100                                                  "s#O|ssOO!O:EnumProperty",
1101                                                  (char **)kwlist, &id, &id_len,
1102                                                  &items, &name, &description,
1103                                                  &def, &PySet_Type, &pyopts,
1104                                                  &update_cb))
1105                 {
1106                         return NULL;
1107                 }
1108
1109                 BPY_PROPDEF_CHECK(EnumProperty, property_flag_enum_items)
1110
1111                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
1112                         return NULL;
1113                 }
1114
1115                 /* items can be a list or a callable */
1116                 if(PyFunction_Check(items)) { /* dont use PyCallable_Check because we need the function code for errors */
1117                         PyCodeObject *f_code= (PyCodeObject *)PyFunction_GET_CODE(items);
1118                         if(f_code->co_argcount != 2) {
1119                                 PyErr_Format(PyExc_ValueError,
1120                                              "EnumProperty(...): expected 'items' function to take 2 arguments, not %d",
1121                                              f_code->co_argcount);
1122                                 return NULL;
1123                         }
1124
1125                         if(def) {
1126                                 /* note, using type error here is odd but python does this for invalid arguments */
1127                                 PyErr_SetString(PyExc_TypeError,
1128                                                 "EnumProperty(...): 'default' can't be set when 'items' is a function");
1129                                 return NULL;
1130                         }
1131
1132                         is_itemf= TRUE;
1133                         eitems= DummyRNA_NULL_items;
1134                 }
1135                 else {
1136                         if(!(items_fast= PySequence_Fast(items, "EnumProperty(...): expected a sequence of tuples for the enum items or a function"))) {
1137                                 return NULL;
1138                         }
1139
1140                         eitems= enum_items_from_py(items_fast, def, &defvalue, (opts & PROP_ENUM_FLAG)!=0);
1141
1142                         Py_DECREF(items_fast);
1143
1144                         if(!eitems) {
1145                                 return NULL;
1146                         }
1147                 }
1148
1149                 if(opts & PROP_ENUM_FLAG)       prop= RNA_def_enum_flag(srna, id, eitems, defvalue, name, description);
1150                 else                                            prop= RNA_def_enum(srna, id, eitems, defvalue, name, description);
1151
1152                 if(is_itemf) {
1153                         RNA_def_enum_funcs(prop, bpy_props_enum_itemf);
1154                         RNA_def_enum_py_data(prop, (void *)items);
1155                         /* Py_INCREF(items); */ /* watch out!, if user is tricky they can probably crash blender if they manage to free the callback, take care! */
1156                 }
1157
1158                 if(pyopts) {
1159                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
1160                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
1161                 }
1162                 bpy_prop_callback_assign(prop, update_cb);
1163                 RNA_def_property_duplicate_pointers(srna, prop);
1164
1165                 if(is_itemf == FALSE) {
1166                         MEM_freeN(eitems);
1167                 }
1168         }
1169         Py_RETURN_NONE;
1170 }
1171
1172 static StructRNA *pointer_type_from_py(PyObject *value, const char *error_prefix)
1173 {
1174         StructRNA *srna;
1175
1176         srna= srna_from_self(value, "");
1177         if(!srna) {
1178                 if(PyErr_Occurred()) {
1179                         PyObject *msg= PyC_ExceptionBuffer();
1180                         char *msg_char= _PyUnicode_AsString(msg);
1181                         PyErr_Format(PyExc_TypeError,
1182                                      "%.200s expected an RNA type derived from PropertyGroup, failed with: %s",
1183                                      error_prefix, msg_char);
1184                         Py_DECREF(msg);
1185                 }
1186                 else {
1187                         PyErr_Format(PyExc_TypeError,
1188                                      "%.200s expected an RNA type derived from PropertyGroup, failed with type '%s'",
1189                                      error_prefix, Py_TYPE(value)->tp_name);
1190                 }
1191                 return NULL;
1192         }
1193
1194         if(!RNA_struct_is_a(srna, &RNA_PropertyGroup)) {
1195                 PyErr_Format(PyExc_TypeError,
1196                              "%.200s expected an RNA type derived from PropertyGroup",
1197                              error_prefix);
1198                 return NULL;
1199         }
1200
1201         return srna;
1202 }
1203
1204 PyDoc_STRVAR(BPy_PointerProperty_doc,
1205 ".. function:: PointerProperty(type=\"\", description=\"\", options={'ANIMATABLE'}, update=None)\n"
1206 "\n"
1207 "   Returns a new pointer property definition.\n"
1208 "\n"
1209 "   :arg type: A subclass of :class:`bpy.types.PropertyGroup`.\n"
1210 "   :type type: class\n"
1211 BPY_PROPDEF_NAME_DOC
1212 BPY_PROPDEF_DESC_DOC
1213 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
1214 "   :type options: set\n"
1215 BPY_PROPDEF_UPDATE_DOC
1216 );
1217 static PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw)
1218 {
1219         StructRNA *srna;
1220
1221         BPY_PROPDEF_HEAD(PointerProperty)
1222
1223         if(srna) {
1224                 static const char *kwlist[]= {"attr", "type", "name", "description", "options", "update", NULL};
1225                 const char *id=NULL, *name="", *description="";
1226                 int id_len;
1227                 PropertyRNA *prop;
1228                 StructRNA *ptype;
1229                 PyObject *type= Py_None;
1230                 PyObject *pyopts= NULL;
1231                 int opts=0;
1232                 PyObject *update_cb= NULL;
1233
1234                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1235                                                  "s#O|ssO!O:PointerProperty",
1236                                                  (char **)kwlist, &id, &id_len,
1237                                                  &type, &name, &description,
1238                                                  &PySet_Type, &pyopts,
1239                                                  &update_cb))
1240                 {
1241                         return NULL;
1242                 }
1243
1244                 BPY_PROPDEF_CHECK(PointerProperty, property_flag_items)
1245
1246                 ptype= pointer_type_from_py(type, "PointerProperty(...):");
1247                 if(!ptype)
1248                         return NULL;
1249
1250                 if (bpy_prop_callback_check(update_cb, 2) == -1) {
1251                         return NULL;
1252                 }
1253
1254                 prop= RNA_def_pointer_runtime(srna, id, ptype, name, description);
1255                 if(pyopts) {
1256                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
1257                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
1258                 }
1259                 bpy_prop_callback_assign(prop, update_cb);
1260                 RNA_def_property_duplicate_pointers(srna, prop);
1261         }
1262         Py_RETURN_NONE;
1263 }
1264
1265 PyDoc_STRVAR(BPy_CollectionProperty_doc,
1266 ".. function:: CollectionProperty(items, type=\"\", description=\"\", default=\"\", options={'ANIMATABLE'})\n"
1267 "\n"
1268 "   Returns a new collection property definition.\n"
1269 "\n"
1270 "   :arg type: A subclass of :class:`bpy.types.PropertyGroup`.\n"
1271 "   :type type: class\n"
1272 BPY_PROPDEF_NAME_DOC
1273 BPY_PROPDEF_DESC_DOC
1274 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE'].\n"
1275 "   :type options: set\n"
1276 );
1277 static PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
1278 {
1279         StructRNA *srna;
1280
1281         BPY_PROPDEF_HEAD(CollectionProperty)
1282
1283         if(srna) {
1284                 static const char *kwlist[]= {"attr", "type", "name", "description", "options", NULL};
1285                 const char *id=NULL, *name="", *description="";
1286                 int id_len;
1287                 PropertyRNA *prop;
1288                 StructRNA *ptype;
1289                 PyObject *type= Py_None;
1290                 PyObject *pyopts= NULL;
1291                 int opts=0;
1292
1293                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1294                                                  "s#O|ssO!:CollectionProperty",
1295                                                  (char **)kwlist, &id, &id_len,
1296                                                  &type, &name, &description,
1297                                                  &PySet_Type, &pyopts))
1298                 {
1299                         return NULL;
1300                 }
1301
1302                 BPY_PROPDEF_CHECK(CollectionProperty, property_flag_items)
1303
1304                 ptype= pointer_type_from_py(type, "CollectionProperty(...):");
1305                 if(!ptype)
1306                         return NULL;
1307
1308                 prop= RNA_def_collection_runtime(srna, id, ptype, name, description);
1309                 if(pyopts) {
1310                         if(opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
1311                         if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
1312                 }
1313                 RNA_def_property_duplicate_pointers(srna, prop);
1314         }
1315         Py_RETURN_NONE;
1316 }
1317
1318 PyDoc_STRVAR(BPy_RemoveProperty_doc,
1319 ".. function:: RemoveProperty(attr)\n"
1320 "\n"
1321 "   Removes a dynamically defined property.\n"
1322 "\n"
1323 "   :arg attr: Property name.\n"
1324 "   :type attr: string\n"
1325 );
1326 static PyObject *BPy_RemoveProperty(PyObject *self, PyObject *args, PyObject *kw)
1327 {
1328         StructRNA *srna;
1329
1330         if(PyTuple_GET_SIZE(args) == 1) {
1331                 PyObject *ret;
1332                 self= PyTuple_GET_ITEM(args, 0);
1333                 args= PyTuple_New(0);
1334                 ret= BPy_RemoveProperty(self, args, kw);
1335                 Py_DECREF(args);
1336                 return ret;     
1337         }
1338         else if (PyTuple_GET_SIZE(args) > 1) {
1339                 PyErr_SetString(PyExc_ValueError, "all args must be keywords"); \
1340                 return NULL;
1341         }
1342
1343         srna= srna_from_self(self, "RemoveProperty(...):");
1344         if(srna==NULL && PyErr_Occurred()) {
1345                 return NULL; /* self's type was compatible but error getting the srna */
1346         }
1347         else if(srna==NULL) {
1348                 PyErr_SetString(PyExc_TypeError, "RemoveProperty(): struct rna not available for this type");
1349                 return NULL;
1350         }
1351         else {
1352                 static const char *kwlist[]= {"attr", NULL};
1353                 
1354                 char *id=NULL;
1355
1356                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1357                                                  "s:RemoveProperty",
1358                                                  (char **)kwlist, &id))
1359                 {
1360                         return NULL;
1361                 }
1362
1363                 if(RNA_def_property_free_identifier(srna, id) != 1) {
1364                         PyErr_Format(PyExc_TypeError, "RemoveProperty(): '%s' not a defined dynamic property", id);
1365                         return NULL;
1366                 }
1367         }
1368         Py_RETURN_NONE;
1369 }
1370
1371 static struct PyMethodDef props_methods[]= {
1372         {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolProperty_doc},
1373         {"BoolVectorProperty", (PyCFunction)BPy_BoolVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_BoolVectorProperty_doc},
1374         {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntProperty_doc},
1375         {"IntVectorProperty", (PyCFunction)BPy_IntVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_IntVectorProperty_doc},
1376         {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatProperty_doc},
1377         {"FloatVectorProperty", (PyCFunction)BPy_FloatVectorProperty, METH_VARARGS|METH_KEYWORDS, BPy_FloatVectorProperty_doc},
1378         {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS|METH_KEYWORDS, BPy_StringProperty_doc},
1379         {"EnumProperty", (PyCFunction)BPy_EnumProperty, METH_VARARGS|METH_KEYWORDS, BPy_EnumProperty_doc},
1380         {"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS|METH_KEYWORDS, BPy_PointerProperty_doc},
1381         {"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS|METH_KEYWORDS, BPy_CollectionProperty_doc},
1382
1383         {"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS|METH_KEYWORDS, BPy_RemoveProperty_doc},
1384         {NULL, NULL, 0, NULL}
1385 };
1386
1387 static struct PyModuleDef props_module= {
1388         PyModuleDef_HEAD_INIT,
1389         "bpy.props",
1390         "This module defines properties to extend blenders internal data, the result of these functions"
1391         " is used to assign properties to classes registered with blender and can't be used directly.",
1392         -1,/* multiple "initialization" just copies the module dict. */
1393         props_methods,
1394         NULL, NULL, NULL, NULL
1395 };
1396
1397 PyObject *BPY_rna_props(void)
1398 {
1399         PyObject *submodule;
1400         PyObject *submodule_dict;
1401         
1402         submodule= PyModule_Create(&props_module);
1403         PyDict_SetItemString(PyImport_GetModuleDict(), props_module.m_name, submodule);
1404
1405         /* INCREF since its its assumed that all these functions return the
1406          * module with a new ref like PyDict_New, since they are passed to
1407          * PyModule_AddObject which steals a ref */
1408         Py_INCREF(submodule);
1409         
1410         /* api needs the PyObjects internally */
1411         submodule_dict= PyModule_GetDict(submodule);
1412
1413 #define ASSIGN_STATIC(_name) pymeth_##_name= PyDict_GetItemString(submodule_dict, #_name)
1414
1415         ASSIGN_STATIC(BoolProperty);
1416         ASSIGN_STATIC(BoolVectorProperty);
1417         ASSIGN_STATIC(IntProperty);
1418         ASSIGN_STATIC(IntVectorProperty);
1419         ASSIGN_STATIC(FloatProperty);
1420         ASSIGN_STATIC(FloatVectorProperty);
1421         ASSIGN_STATIC(StringProperty);
1422         ASSIGN_STATIC(EnumProperty);
1423         ASSIGN_STATIC(PointerProperty);
1424         ASSIGN_STATIC(CollectionProperty);
1425         ASSIGN_STATIC(RemoveProperty);
1426         
1427         return submodule;
1428 }