3888e1b7ecdb87967feed15e7cad280ed89d05b8
[blender.git] / source / blender / python / intern / bpy_props.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_props.c
24  *  \ingroup pythonintern
25  *
26  * This file defines 'bpy.props' module used so scripts can define their own
27  * rna properties for use with python operators or adding new properties to
28  * existing blender types.
29  */
30
31
32 #include <Python.h>
33
34 #include "RNA_types.h"
35
36 #include "BLI_utildefines.h"
37
38 #include "bpy_props.h"
39 #include "bpy_rna.h"
40 #include "bpy_util.h"
41
42 #include "BKE_idprop.h"
43
44 #include "RNA_access.h"
45 #include "RNA_define.h" /* for defining our own rna */
46 #include "RNA_enum_types.h"
47
48 #include "MEM_guardedalloc.h"
49
50 #include "../generic/py_capi_utils.h"
51
52 /* initial definition of callback slots we'll probably have more than 1 */
53 #define BPY_DATA_CB_SLOT_SIZE 3
54
55 #define BPY_DATA_CB_SLOT_UPDATE 0
56 #define BPY_DATA_CB_SLOT_GET 1
57 #define BPY_DATA_CB_SLOT_SET 2
58
59 extern BPy_StructRNA *bpy_context_module;
60
61 static EnumPropertyItem property_flag_items[] = {
62         {PROP_HIDDEN, "HIDDEN", 0, "Hidden", ""},
63         {PROP_SKIP_SAVE, "SKIP_SAVE", 0, "Skip Save", ""},
64         {PROP_ANIMATABLE, "ANIMATABLE", 0, "Animatable", ""},
65         {PROP_LIB_EXCEPTION, "LIBRARY_EDITABLE", 0, "Library Editable", ""},
66         {PROP_PROPORTIONAL, "PROPORTIONAL", 0, "Adjust values proportionally to eachother", ""},
67         {0, NULL, 0, NULL, NULL}};
68
69 #define BPY_PROPDEF_OPTIONS_DOC \
70 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE', 'LIBRARY_EDITABLE', 'PROPORTIONAL'].\n" \
71 "   :type options: set\n" \
72
73 static EnumPropertyItem property_flag_enum_items[] = {
74         {PROP_HIDDEN, "HIDDEN", 0, "Hidden", ""},
75         {PROP_SKIP_SAVE, "SKIP_SAVE", 0, "Skip Save", ""},
76         {PROP_ANIMATABLE, "ANIMATABLE", 0, "Animatable", ""},
77         {PROP_LIB_EXCEPTION, "LIBRARY_EDITABLE", 0, "Library Editable", ""},
78         {PROP_ENUM_FLAG, "ENUM_FLAG", 0, "Enum Flag", ""},
79         {0, NULL, 0, NULL, NULL}};
80
81 #define BPY_PROPDEF_OPTIONS_ENUM_DOC \
82 "   :type default: string or set\n" \
83 "   :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE', 'ENUM_FLAG', 'LIBRARY_EDITABLE'].\n" \
84
85 /* subtypes */
86 /* XXX Keep in sync with rna_rna.c's property_subtype_items ???
87  *     Currently it is not...
88  */
89 static EnumPropertyItem property_subtype_string_items[] = {
90         {PROP_FILEPATH, "FILE_PATH", 0, "File Path", ""},
91         {PROP_DIRPATH, "DIR_PATH", 0, "Directory Path", ""},
92         {PROP_FILENAME, "FILE_NAME", 0, "Filename", ""},
93         {PROP_BYTESTRING, "BYTE_STRING", 0, "Byte String", ""},
94         {PROP_PASSWORD, "PASSWORD", 0, "Password", "A string that is displayed hidden ('********')"},
95
96         {PROP_NONE, "NONE", 0, "None", ""},
97         {0, NULL, 0, NULL, NULL}};
98
99 #define BPY_PROPDEF_SUBTYPE_STRING_DOC \
100 "   :arg subtype: Enumerator in ['FILE_PATH', 'DIR_PATH', 'FILE_NAME', 'BYTE_STRING', 'PASSWORD', 'NONE'].\n" \
101 "   :type subtype: string\n" \
102
103 static EnumPropertyItem property_subtype_number_items[] = {
104         {PROP_PIXEL, "PIXEL", 0, "Pixel", ""},
105         {PROP_UNSIGNED, "UNSIGNED", 0, "Unsigned", ""},
106         {PROP_PERCENTAGE, "PERCENTAGE", 0, "Percentage", ""},
107         {PROP_FACTOR, "FACTOR", 0, "Factor", ""},
108         {PROP_ANGLE, "ANGLE", 0, "Angle", ""},
109         {PROP_TIME, "TIME", 0, "Time", ""},
110         {PROP_DISTANCE, "DISTANCE", 0, "Distance", ""},
111
112         {PROP_NONE, "NONE", 0, "None", ""},
113         {0, NULL, 0, NULL, NULL}};
114
115 #define BPY_PROPDEF_SUBTYPE_NUMBER_DOC \
116 "   :arg subtype: Enumerator in ['PIXEL', 'UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n" \
117 "   :type subtype: string\n" \
118
119 static EnumPropertyItem property_subtype_array_items[] = {
120         {PROP_COLOR, "COLOR", 0, "Color", ""},
121         {PROP_TRANSLATION, "TRANSLATION", 0, "Translation", ""},
122         {PROP_DIRECTION, "DIRECTION", 0, "Direction", ""},
123         {PROP_VELOCITY, "VELOCITY", 0, "Velocity", ""},
124         {PROP_ACCELERATION, "ACCELERATION", 0, "Acceleration", ""},
125         {PROP_MATRIX, "MATRIX", 0, "Matrix", ""},
126         {PROP_EULER, "EULER", 0, "Euler", ""},
127         {PROP_QUATERNION, "QUATERNION", 0, "Quaternion", ""},
128         {PROP_AXISANGLE, "AXISANGLE", 0, "Axis Angle", ""},
129         {PROP_XYZ, "XYZ", 0, "XYZ", ""},
130         {PROP_COLOR_GAMMA, "COLOR_GAMMA", 0, "Color Gamma", ""},
131         {PROP_LAYER, "LAYER", 0, "Layer", ""},
132
133         {PROP_NONE, "NONE", 0, "None", ""},
134         {0, NULL, 0, NULL, NULL}};
135
136 #define BPY_PROPDEF_SUBTYPE_ARRAY_DOC \
137 "   :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', " \
138                                 "'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', " \
139                                 "'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n" \
140 "   :type subtype: string\n"
141
142 /* PyObject's */
143 static PyObject *pymeth_BoolProperty = NULL;
144 static PyObject *pymeth_BoolVectorProperty = NULL;
145 static PyObject *pymeth_IntProperty = NULL;
146 static PyObject *pymeth_IntVectorProperty = NULL;
147 static PyObject *pymeth_FloatProperty = NULL;
148 static PyObject *pymeth_FloatVectorProperty = NULL;
149 static PyObject *pymeth_StringProperty = NULL;
150 static PyObject *pymeth_EnumProperty = NULL;
151 static PyObject *pymeth_PointerProperty = NULL;
152 static PyObject *pymeth_CollectionProperty = NULL;
153 static PyObject *pymeth_RemoveProperty = NULL;
154
155 static PyObject *pyrna_struct_as_instance(PointerRNA *ptr)
156 {
157         PyObject *self = NULL;
158         /* first get self */
159         /* operators can store their own instance for later use */
160         if (ptr->data) {
161                 void **instance = RNA_struct_instance(ptr);
162
163                 if (instance) {
164                         if (*instance) {
165                                 self = *instance;
166                                 Py_INCREF(self);
167                         }
168                 }
169         }
170
171         /* in most cases this will run */
172         if (self == NULL) {
173                 self = pyrna_struct_CreatePyObject(ptr);
174         }
175
176         return self;
177 }
178
179 /* could be moved into bpy_utils */
180 static void printf_func_error(PyObject *py_func)
181 {
182         /* since we return to C code we can't leave the error */
183         PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(py_func);
184         PyErr_Print();
185         PyErr_Clear();
186
187         /* use py style error */
188         fprintf(stderr, "File \"%s\", line %d, in %s\n",
189                 _PyUnicode_AsString(f_code->co_filename),
190                 f_code->co_firstlineno,
191                 _PyUnicode_AsString(((PyFunctionObject *)py_func)->func_name)
192                 );
193 }
194
195 /* operators and classes use this so it can store the args given but defer
196  * running it until the operator runs where these values are used to setup
197  * the default args for that operator instance */
198 static PyObject *bpy_prop_deferred_return(PyObject *func, PyObject *kw)
199 {
200         PyObject *ret = PyTuple_New(2);
201         PyTuple_SET_ITEM(ret, 0, func);
202         Py_INCREF(func);
203
204         if (kw == NULL)
205                 kw = PyDict_New();
206         else
207                 Py_INCREF(kw);
208
209         PyTuple_SET_ITEM(ret, 1, kw);
210
211         return ret;
212 }
213
214 /* callbacks */
215 static void bpy_prop_update_cb(struct bContext *C, struct PointerRNA *ptr, struct PropertyRNA *prop)
216 {
217         PyGILState_STATE gilstate;
218         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
219         PyObject *py_func;
220         PyObject *args;
221         PyObject *self;
222         PyObject *ret;
223         const bool is_write_ok = pyrna_write_check();
224
225         BLI_assert(py_data != NULL);
226
227         if (!is_write_ok) {
228                 pyrna_write_set(true);
229         }
230
231         bpy_context_set(C, &gilstate);
232
233         py_func = py_data[BPY_DATA_CB_SLOT_UPDATE];
234
235         args = PyTuple_New(2);
236         self = pyrna_struct_as_instance(ptr);
237         PyTuple_SET_ITEM(args, 0, self);
238
239         PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
240         Py_INCREF(bpy_context_module);
241
242         ret = PyObject_CallObject(py_func, args);
243
244         Py_DECREF(args);
245
246         if (ret == NULL) {
247                 printf_func_error(py_func);
248         }
249         else {
250                 if (ret != Py_None) {
251                         PyErr_SetString(PyExc_ValueError, "the return value must be None");
252                         printf_func_error(py_func);
253                 }
254
255                 Py_DECREF(ret);
256         }
257
258         bpy_context_clear(C, &gilstate);
259
260         if (!is_write_ok) {
261                 pyrna_write_set(false);
262         }
263 }
264
265 static int bpy_prop_boolean_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop)
266 {
267         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
268         PyObject *py_func;
269         PyObject *args;
270         PyObject *self;
271         PyObject *ret;
272         PyGILState_STATE gilstate;
273         bool use_gil;
274         const bool is_write_ok = pyrna_write_check();
275         int value;
276
277         BLI_assert(py_data != NULL);
278
279         if (!is_write_ok) {
280                 pyrna_write_set(true);
281         }
282
283         use_gil = true;  /* !PyC_IsInterpreterActive(); */
284
285         if (use_gil)
286                 gilstate = PyGILState_Ensure();
287
288         py_func = py_data[BPY_DATA_CB_SLOT_GET];
289
290         args = PyTuple_New(1);
291         self = pyrna_struct_as_instance(ptr);
292         PyTuple_SET_ITEM(args, 0, self);
293
294         ret = PyObject_CallObject(py_func, args);
295
296         Py_DECREF(args);
297
298         if (ret == NULL) {
299                 printf_func_error(py_func);
300                 value = false;
301         }
302         else {
303                 value = PyLong_AsLong(ret);
304
305                 if (value == -1 && PyErr_Occurred()) {
306                         printf_func_error(py_func);
307                         value = false;
308                 }
309
310                 Py_DECREF(ret);
311         }
312
313         if (use_gil)
314                 PyGILState_Release(gilstate);
315
316         if (!is_write_ok) {
317                 pyrna_write_set(false);
318         }
319
320         return value;
321 }
322
323 static void bpy_prop_boolean_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, int value)
324 {
325         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
326         PyObject *py_func;
327         PyObject *args;
328         PyObject *self;
329         PyObject *ret;
330         PyGILState_STATE gilstate;
331         bool use_gil;
332         const bool is_write_ok = pyrna_write_check();
333
334         BLI_assert(py_data != NULL);
335
336         if (!is_write_ok) {
337                 pyrna_write_set(true);
338         }
339
340         use_gil = true;  /* !PyC_IsInterpreterActive(); */
341
342         if (use_gil)
343                 gilstate = PyGILState_Ensure();
344
345         py_func = py_data[BPY_DATA_CB_SLOT_SET];
346
347         args = PyTuple_New(2);
348         self = pyrna_struct_as_instance(ptr);
349         PyTuple_SET_ITEM(args, 0, self);
350
351         PyTuple_SET_ITEM(args, 1, PyBool_FromLong(value));
352
353         ret = PyObject_CallObject(py_func, args);
354
355         Py_DECREF(args);
356
357         if (ret == NULL) {
358                 printf_func_error(py_func);
359         }
360         else {
361                 if (ret != Py_None) {
362                         PyErr_SetString(PyExc_ValueError, "the return value must be None");
363                         printf_func_error(py_func);
364                 }
365
366                 Py_DECREF(ret);
367         }
368         
369         if (use_gil)
370                 PyGILState_Release(gilstate);
371
372         if (!is_write_ok) {
373                 pyrna_write_set(false);
374         }
375 }
376
377 static void bpy_prop_boolean_array_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, int *values)
378 {
379         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
380         PyObject *py_func;
381         PyObject *args;
382         PyObject *self;
383         PyObject *ret;
384         PyGILState_STATE gilstate;
385         bool use_gil;
386         const bool is_write_ok = pyrna_write_check();
387         int i, len = RNA_property_array_length(ptr, prop);
388
389         BLI_assert(py_data != NULL);
390
391         if (!is_write_ok) {
392                 pyrna_write_set(true);
393         }
394
395         use_gil = true;  /* !PyC_IsInterpreterActive(); */
396
397         if (use_gil)
398                 gilstate = PyGILState_Ensure();
399
400         py_func = py_data[BPY_DATA_CB_SLOT_GET];
401
402         args = PyTuple_New(1);
403         self = pyrna_struct_as_instance(ptr);
404         PyTuple_SET_ITEM(args, 0, self);
405
406         ret = PyObject_CallObject(py_func, args);
407
408         Py_DECREF(args);
409
410         if (ret == NULL) {
411                 printf_func_error(py_func);
412
413                 for (i = 0; i < len; ++i)
414                         values[i] = false;
415         }
416         else {
417                 if (PyC_AsArray(values, ret, len, &PyBool_Type, false, "BoolVectorProperty get") == -1) {
418                         printf_func_error(py_func);
419
420                         for (i = 0; i < len; ++i)
421                                 values[i] = false;
422
423                         /* PyC_AsArray decrements refcount internally on error */
424                 }
425                 else {
426                         Py_DECREF(ret);
427                 }
428         }
429
430         if (use_gil)
431                 PyGILState_Release(gilstate);
432
433         if (!is_write_ok) {
434                 pyrna_write_set(false);
435         }
436 }
437
438 static void bpy_prop_boolean_array_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, const int *values)
439 {
440         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
441         PyObject *py_func;
442         PyObject *args;
443         PyObject *self;
444         PyObject *ret;
445         PyObject *py_values;
446         PyGILState_STATE gilstate;
447         bool use_gil;
448         const bool is_write_ok = pyrna_write_check();
449         int len = RNA_property_array_length(ptr, prop);
450
451         BLI_assert(py_data != NULL);
452
453         if (!is_write_ok) {
454                 pyrna_write_set(true);
455         }
456
457         use_gil = true;  /* !PyC_IsInterpreterActive(); */
458
459         if (use_gil)
460                 gilstate = PyGILState_Ensure();
461
462         py_func = py_data[BPY_DATA_CB_SLOT_SET];
463
464         args = PyTuple_New(2);
465         self = pyrna_struct_as_instance(ptr);
466         PyTuple_SET_ITEM(args, 0, self);
467
468         py_values = PyC_FromArray(values, len, &PyBool_Type, false, "BoolVectorProperty set");
469         if (!py_values) {
470                 printf_func_error(py_func);
471         }
472         else
473                 PyTuple_SET_ITEM(args, 1, py_values);
474
475         ret = PyObject_CallObject(py_func, args);
476
477         Py_DECREF(args);
478
479         if (ret == NULL) {
480                 printf_func_error(py_func);
481         }
482         else {
483                 if (ret != Py_None) {
484                         PyErr_SetString(PyExc_ValueError, "the return value must be None");
485                         printf_func_error(py_func);
486                 }
487
488                 Py_DECREF(ret);
489         }
490
491         if (use_gil)
492                 PyGILState_Release(gilstate);
493
494         if (!is_write_ok) {
495                 pyrna_write_set(false);
496         }
497 }
498
499 static int bpy_prop_int_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop)
500 {
501         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
502         PyObject *py_func;
503         PyObject *args;
504         PyObject *self;
505         PyObject *ret;
506         PyGILState_STATE gilstate;
507         bool use_gil;
508         const bool is_write_ok = pyrna_write_check();
509         int value;
510
511         BLI_assert(py_data != NULL);
512
513         if (!is_write_ok) {
514                 pyrna_write_set(true);
515         }
516
517         use_gil = true;  /* !PyC_IsInterpreterActive(); */
518
519         if (use_gil)
520                 gilstate = PyGILState_Ensure();
521
522         py_func = py_data[BPY_DATA_CB_SLOT_GET];
523
524         args = PyTuple_New(1);
525         self = pyrna_struct_as_instance(ptr);
526         PyTuple_SET_ITEM(args, 0, self);
527
528         ret = PyObject_CallObject(py_func, args);
529
530         Py_DECREF(args);
531
532         if (ret == NULL) {
533                 printf_func_error(py_func);
534                 value = 0.0f;
535         }
536         else {
537                 value = PyLong_AsLong(ret);
538
539                 if (value == -1 && PyErr_Occurred()) {
540                         printf_func_error(py_func);
541                         value = 0;
542                 }
543
544                 Py_DECREF(ret);
545         }
546
547         if (use_gil)
548                 PyGILState_Release(gilstate);
549
550         if (!is_write_ok) {
551                 pyrna_write_set(false);
552         }
553
554         return value;
555 }
556
557 static void bpy_prop_int_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, int value)
558 {
559         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
560         PyObject *py_func;
561         PyObject *args;
562         PyObject *self;
563         PyObject *ret;
564         PyGILState_STATE gilstate;
565         bool use_gil;
566         const bool is_write_ok = pyrna_write_check();
567
568         BLI_assert(py_data != NULL);
569
570         if (!is_write_ok) {
571                 pyrna_write_set(true);
572         }
573
574         use_gil = true;  /* !PyC_IsInterpreterActive(); */
575
576         if (use_gil)
577                 gilstate = PyGILState_Ensure();
578
579         py_func = py_data[BPY_DATA_CB_SLOT_SET];
580
581         args = PyTuple_New(2);
582         self = pyrna_struct_as_instance(ptr);
583         PyTuple_SET_ITEM(args, 0, self);
584
585         PyTuple_SET_ITEM(args, 1, PyLong_FromLong(value));
586
587         ret = PyObject_CallObject(py_func, args);
588
589         Py_DECREF(args);
590
591         if (ret == NULL) {
592                 printf_func_error(py_func);
593         }
594         else {
595                 if (ret != Py_None) {
596                         PyErr_SetString(PyExc_ValueError, "the return value must be None");
597                         printf_func_error(py_func);
598                 }
599
600                 Py_DECREF(ret);
601         }
602
603         if (use_gil)
604                 PyGILState_Release(gilstate);
605
606         if (!is_write_ok) {
607                 pyrna_write_set(false);
608         }
609 }
610
611 static void bpy_prop_int_array_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, int *values)
612 {
613         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
614         PyObject *py_func;
615         PyObject *args;
616         PyObject *self;
617         PyObject *ret;
618         PyGILState_STATE gilstate;
619         bool use_gil;
620         const bool is_write_ok = pyrna_write_check();
621         int i, len = RNA_property_array_length(ptr, prop);
622
623         BLI_assert(py_data != NULL);
624
625         if (!is_write_ok) {
626                 pyrna_write_set(true);
627         }
628
629         use_gil = true;  /* !PyC_IsInterpreterActive(); */
630
631         if (use_gil)
632                 gilstate = PyGILState_Ensure();
633
634         py_func = py_data[BPY_DATA_CB_SLOT_GET];
635
636         args = PyTuple_New(1);
637         self = pyrna_struct_as_instance(ptr);
638         PyTuple_SET_ITEM(args, 0, self);
639
640         ret = PyObject_CallObject(py_func, args);
641
642         Py_DECREF(args);
643
644         if (ret == NULL) {
645                 printf_func_error(py_func);
646
647                 for (i = 0; i < len; ++i)
648                         values[i] = 0;
649         }
650         else {
651                 if (PyC_AsArray(values, ret, len, &PyLong_Type, false, "IntVectorProperty get") == -1) {
652                         printf_func_error(py_func);
653
654                         for (i = 0; i < len; ++i)
655                                 values[i] = 0;
656
657                         /* PyC_AsArray decrements refcount internally on error */
658                 }
659                 else {
660                         Py_DECREF(ret);
661                 }
662         }
663
664         if (use_gil)
665                 PyGILState_Release(gilstate);
666
667         if (!is_write_ok) {
668                 pyrna_write_set(false);
669         }
670 }
671
672 static void bpy_prop_int_array_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, const int *values)
673 {
674         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
675         PyObject *py_func;
676         PyObject *args;
677         PyObject *self;
678         PyObject *ret;
679         PyObject *py_values;
680         PyGILState_STATE gilstate;
681         bool use_gil;
682         const bool is_write_ok = pyrna_write_check();
683         int len = RNA_property_array_length(ptr, prop);
684
685         BLI_assert(py_data != NULL);
686
687         if (!is_write_ok) {
688                 pyrna_write_set(true);
689         }
690
691         use_gil = true;  /* !PyC_IsInterpreterActive(); */
692
693         if (use_gil)
694                 gilstate = PyGILState_Ensure();
695
696         py_func = py_data[BPY_DATA_CB_SLOT_SET];
697
698         args = PyTuple_New(2);
699         self = pyrna_struct_as_instance(ptr);
700         PyTuple_SET_ITEM(args, 0, self);
701
702         py_values = PyC_FromArray(values, len, &PyLong_Type, false, "IntVectorProperty set");
703         if (!py_values) {
704                 printf_func_error(py_func);
705         }
706         else
707                 PyTuple_SET_ITEM(args, 1, py_values);
708
709         ret = PyObject_CallObject(py_func, args);
710
711         Py_DECREF(args);
712
713         if (ret == NULL) {
714                 printf_func_error(py_func);
715         }
716         else {
717                 if (ret != Py_None) {
718                         PyErr_SetString(PyExc_ValueError, "the return value must be None");
719                         printf_func_error(py_func);
720                 }
721
722                 Py_DECREF(ret);
723         }
724
725         if (use_gil)
726                 PyGILState_Release(gilstate);
727
728         if (!is_write_ok) {
729                 pyrna_write_set(false);
730         }
731 }
732
733 static float bpy_prop_float_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop)
734 {
735         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
736         PyObject *py_func;
737         PyObject *args;
738         PyObject *self;
739         PyObject *ret;
740         PyGILState_STATE gilstate;
741         bool use_gil;
742         const bool is_write_ok = pyrna_write_check();
743         float value;
744
745         BLI_assert(py_data != NULL);
746
747         if (!is_write_ok) {
748                 pyrna_write_set(true);
749         }
750
751         use_gil = true;  /* !PyC_IsInterpreterActive(); */
752
753         if (use_gil)
754                 gilstate = PyGILState_Ensure();
755
756         py_func = py_data[BPY_DATA_CB_SLOT_GET];
757
758         args = PyTuple_New(1);
759         self = pyrna_struct_as_instance(ptr);
760         PyTuple_SET_ITEM(args, 0, self);
761
762         ret = PyObject_CallObject(py_func, args);
763
764         Py_DECREF(args);
765
766         if (ret == NULL) {
767                 printf_func_error(py_func);
768                 value = 0.0f;
769         }
770         else {
771                 value = PyFloat_AsDouble(ret);
772
773                 if (value == -1.0f && PyErr_Occurred()) {
774                         printf_func_error(py_func);
775                         value = 0.0f;
776                 }
777
778                 Py_DECREF(ret);
779         }
780
781         if (use_gil)
782                 PyGILState_Release(gilstate);
783
784         if (!is_write_ok) {
785                 pyrna_write_set(false);
786         }
787
788         return value;
789 }
790
791 static void bpy_prop_float_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, float value)
792 {
793         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
794         PyObject *py_func;
795         PyObject *args;
796         PyObject *self;
797         PyObject *ret;
798         PyGILState_STATE gilstate;
799         bool use_gil;
800         const bool is_write_ok = pyrna_write_check();
801
802         BLI_assert(py_data != NULL);
803
804         if (!is_write_ok) {
805                 pyrna_write_set(true);
806         }
807
808         use_gil = true;  /* !PyC_IsInterpreterActive(); */
809
810         if (use_gil)
811                 gilstate = PyGILState_Ensure();
812
813         py_func = py_data[BPY_DATA_CB_SLOT_SET];
814
815         args = PyTuple_New(2);
816         self = pyrna_struct_as_instance(ptr);
817         PyTuple_SET_ITEM(args, 0, self);
818
819         PyTuple_SET_ITEM(args, 1, PyFloat_FromDouble(value));
820
821         ret = PyObject_CallObject(py_func, args);
822
823         Py_DECREF(args);
824
825         if (ret == NULL) {
826                 printf_func_error(py_func);
827         }
828         else {
829                 if (ret != Py_None) {
830                         PyErr_SetString(PyExc_ValueError, "the return value must be None");
831                         printf_func_error(py_func);
832                 }
833
834                 Py_DECREF(ret);
835         }
836
837         if (use_gil)
838                 PyGILState_Release(gilstate);
839
840         if (!is_write_ok) {
841                 pyrna_write_set(false);
842         }
843 }
844
845 static void bpy_prop_float_array_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, float *values)
846 {
847         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
848         PyObject *py_func;
849         PyObject *args;
850         PyObject *self;
851         PyObject *ret;
852         PyGILState_STATE gilstate;
853         bool use_gil;
854         const bool is_write_ok = pyrna_write_check();
855         int i, len = RNA_property_array_length(ptr, prop);
856
857         BLI_assert(py_data != NULL);
858
859         if (!is_write_ok) {
860                 pyrna_write_set(true);
861         }
862
863         use_gil = true;  /* !PyC_IsInterpreterActive(); */
864
865         if (use_gil)
866                 gilstate = PyGILState_Ensure();
867
868         py_func = py_data[BPY_DATA_CB_SLOT_GET];
869
870         args = PyTuple_New(1);
871         self = pyrna_struct_as_instance(ptr);
872         PyTuple_SET_ITEM(args, 0, self);
873
874         ret = PyObject_CallObject(py_func, args);
875
876         Py_DECREF(args);
877
878         if (ret == NULL) {
879                 printf_func_error(py_func);
880
881                 for (i = 0; i < len; ++i)
882                         values[i] = 0.0f;
883         }
884         else {
885                 if (PyC_AsArray(values, ret, len, &PyFloat_Type, false, "FloatVectorProperty get") == -1) {
886                         printf_func_error(py_func);
887
888                         for (i = 0; i < len; ++i)
889                                 values[i] = 0.0f;
890
891                         /* PyC_AsArray decrements refcount internally on error */
892                 }
893                 else {
894                         Py_DECREF(ret);
895                 }
896         }
897
898         if (use_gil)
899                 PyGILState_Release(gilstate);
900
901         if (!is_write_ok) {
902                 pyrna_write_set(false);
903         }
904 }
905
906 static void bpy_prop_float_array_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, const float *values)
907 {
908         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
909         PyObject *py_func;
910         PyObject *args;
911         PyObject *self;
912         PyObject *ret;
913         PyObject *py_values;
914         PyGILState_STATE gilstate;
915         bool use_gil;
916         const bool is_write_ok = pyrna_write_check();
917         int len = RNA_property_array_length(ptr, prop);
918
919         BLI_assert(py_data != NULL);
920
921         if (!is_write_ok) {
922                 pyrna_write_set(true);
923         }
924
925         use_gil = true;  /* !PyC_IsInterpreterActive(); */
926
927         if (use_gil)
928                 gilstate = PyGILState_Ensure();
929
930         py_func = py_data[BPY_DATA_CB_SLOT_SET];
931
932         args = PyTuple_New(2);
933         self = pyrna_struct_as_instance(ptr);
934         PyTuple_SET_ITEM(args, 0, self);
935
936         py_values = PyC_FromArray(values, len, &PyFloat_Type, false, "FloatVectorProperty set");
937         if (!py_values) {
938                 printf_func_error(py_func);
939         }
940         else
941                 PyTuple_SET_ITEM(args, 1, py_values);
942
943         ret = PyObject_CallObject(py_func, args);
944
945         Py_DECREF(args);
946
947         if (ret == NULL) {
948                 printf_func_error(py_func);
949         }
950         else {
951                 if (ret != Py_None) {
952                         PyErr_SetString(PyExc_ValueError, "the return value must be None");
953                         printf_func_error(py_func);
954                 }
955
956                 Py_DECREF(ret);
957         }
958
959         if (use_gil)
960                 PyGILState_Release(gilstate);
961
962         if (!is_write_ok) {
963                 pyrna_write_set(false);
964         }
965 }
966
967 static void bpy_prop_string_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, char *value)
968 {
969         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
970         PyObject *py_func;
971         PyObject *args;
972         PyObject *self;
973         PyObject *ret;
974         PyGILState_STATE gilstate;
975         bool use_gil;
976         const bool is_write_ok = pyrna_write_check();
977
978         BLI_assert(py_data != NULL);
979
980         if (!is_write_ok) {
981                 pyrna_write_set(true);
982         }
983
984         use_gil = true;  /* !PyC_IsInterpreterActive(); */
985
986         if (use_gil)
987                 gilstate = PyGILState_Ensure();
988
989         py_func = py_data[BPY_DATA_CB_SLOT_GET];
990
991         args = PyTuple_New(1);
992         self = pyrna_struct_as_instance(ptr);
993         PyTuple_SET_ITEM(args, 0, self);
994
995         ret = PyObject_CallObject(py_func, args);
996
997         Py_DECREF(args);
998
999         if (ret == NULL) {
1000                 printf_func_error(py_func);
1001                 value[0] = '\0';
1002         }
1003         else if (!PyUnicode_Check(ret)) {
1004                 PyErr_Format(PyExc_TypeError,
1005                              "return value must be a string, not %.200s",
1006                              Py_TYPE(ret)->tp_name);
1007                 printf_func_error(py_func);
1008                 value[0] = '\0';
1009                 Py_DECREF(ret);
1010         }
1011         else {
1012                 Py_ssize_t length;
1013                 const char *buffer = _PyUnicode_AsStringAndSize(ret, &length);
1014                 memcpy(value, buffer, length + 1);
1015                 Py_DECREF(ret);
1016         }
1017
1018         if (use_gil)
1019                 PyGILState_Release(gilstate);
1020
1021         if (!is_write_ok) {
1022                 pyrna_write_set(false);
1023         }
1024 }
1025
1026 static int bpy_prop_string_length_cb(struct PointerRNA *ptr, struct PropertyRNA *prop)
1027 {
1028         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
1029         PyObject *py_func;
1030         PyObject *args;
1031         PyObject *self;
1032         PyObject *ret;
1033         PyGILState_STATE gilstate;
1034         bool use_gil;
1035         const bool is_write_ok = pyrna_write_check();
1036         int length;
1037
1038         BLI_assert(py_data != NULL);
1039
1040         if (!is_write_ok) {
1041                 pyrna_write_set(true);
1042         }
1043
1044         use_gil = true;  /* !PyC_IsInterpreterActive(); */
1045
1046         if (use_gil)
1047                 gilstate = PyGILState_Ensure();
1048
1049         py_func = py_data[BPY_DATA_CB_SLOT_GET];
1050
1051         args = PyTuple_New(1);
1052         self = pyrna_struct_as_instance(ptr);
1053         PyTuple_SET_ITEM(args, 0, self);
1054
1055         ret = PyObject_CallObject(py_func, args);
1056
1057         Py_DECREF(args);
1058
1059         if (ret == NULL) {
1060                 printf_func_error(py_func);
1061                 length = 0;
1062         }
1063         else if (!PyUnicode_Check(ret)) {
1064                 PyErr_Format(PyExc_TypeError,
1065                              "return value must be a string, not %.200s",
1066                              Py_TYPE(ret)->tp_name);
1067                 printf_func_error(py_func);
1068                 length = 0;
1069                 Py_DECREF(ret);
1070         }
1071         else {
1072                 Py_ssize_t length_ssize_t = 0;
1073                 _PyUnicode_AsStringAndSize(ret, &length_ssize_t);
1074                 length = length_ssize_t;
1075                 Py_DECREF(ret);
1076         }
1077
1078         if (use_gil)
1079                 PyGILState_Release(gilstate);
1080
1081         if (!is_write_ok) {
1082                 pyrna_write_set(false);
1083         }
1084
1085         return length;
1086 }
1087
1088 static void bpy_prop_string_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, const char *value)
1089 {
1090         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
1091         PyObject *py_func;
1092         PyObject *args;
1093         PyObject *self;
1094         PyObject *ret;
1095         PyGILState_STATE gilstate;
1096         bool use_gil;
1097         const bool is_write_ok = pyrna_write_check();
1098         PyObject *py_value;
1099
1100         BLI_assert(py_data != NULL);
1101
1102         if (!is_write_ok) {
1103                 pyrna_write_set(true);
1104         }
1105
1106         use_gil = true;  /* !PyC_IsInterpreterActive(); */
1107
1108         if (use_gil)
1109                 gilstate = PyGILState_Ensure();
1110
1111         py_func = py_data[BPY_DATA_CB_SLOT_SET];
1112
1113         args = PyTuple_New(2);
1114         self = pyrna_struct_as_instance(ptr);
1115         PyTuple_SET_ITEM(args, 0, self);
1116
1117         py_value = PyUnicode_FromString(value);
1118         if (!py_value) {
1119                 PyErr_SetString(PyExc_ValueError, "the return value must be a string");
1120                 printf_func_error(py_func);
1121         }
1122         else
1123                 PyTuple_SET_ITEM(args, 1, py_value);
1124
1125         ret = PyObject_CallObject(py_func, args);
1126
1127         Py_DECREF(args);
1128
1129         if (ret == NULL) {
1130                 printf_func_error(py_func);
1131         }
1132         else {
1133                 if (ret != Py_None) {
1134                         PyErr_SetString(PyExc_ValueError, "the return value must be None");
1135                         printf_func_error(py_func);
1136                 }
1137
1138                 Py_DECREF(ret);
1139         }
1140
1141         if (use_gil)
1142                 PyGILState_Release(gilstate);
1143
1144         if (!is_write_ok) {
1145                 pyrna_write_set(false);
1146         }
1147 }
1148
1149 static int bpy_prop_enum_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop)
1150 {
1151         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
1152         PyObject *py_func;
1153         PyObject *args;
1154         PyObject *self;
1155         PyObject *ret;
1156         PyGILState_STATE gilstate;
1157         bool use_gil;
1158         const bool is_write_ok = pyrna_write_check();
1159         int value;
1160
1161         BLI_assert(py_data != NULL);
1162
1163         if (!is_write_ok) {
1164                 pyrna_write_set(true);
1165         }
1166
1167         use_gil = true;  /* !PyC_IsInterpreterActive(); */
1168
1169         if (use_gil)
1170                 gilstate = PyGILState_Ensure();
1171
1172         py_func = py_data[BPY_DATA_CB_SLOT_GET];
1173
1174         args = PyTuple_New(1);
1175         self = pyrna_struct_as_instance(ptr);
1176         PyTuple_SET_ITEM(args, 0, self);
1177
1178         ret = PyObject_CallObject(py_func, args);
1179
1180         Py_DECREF(args);
1181
1182         if (ret == NULL) {
1183                 printf_func_error(py_func);
1184                 value = RNA_property_enum_get_default(ptr, prop);
1185         }
1186         else {
1187                 value = PyLong_AsLong(ret);
1188
1189                 if (value == -1 && PyErr_Occurred()) {
1190                         printf_func_error(py_func);
1191                         value = RNA_property_enum_get_default(ptr, prop);
1192                 }
1193
1194                 Py_DECREF(ret);
1195         }
1196
1197         if (use_gil)
1198                 PyGILState_Release(gilstate);
1199
1200         if (!is_write_ok) {
1201                 pyrna_write_set(false);
1202         }
1203
1204         return value;
1205 }
1206
1207 static void bpy_prop_enum_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, int value)
1208 {
1209         PyObject **py_data = (PyObject **)RNA_property_py_data_get(prop);
1210         PyObject *py_func;
1211         PyObject *args;
1212         PyObject *self;
1213         PyObject *ret;
1214         PyGILState_STATE gilstate;
1215         bool use_gil;
1216         const bool is_write_ok = pyrna_write_check();
1217
1218         BLI_assert(py_data != NULL);
1219
1220         if (!is_write_ok) {
1221                 pyrna_write_set(true);
1222         }
1223
1224         use_gil = true;  /* !PyC_IsInterpreterActive(); */
1225
1226         if (use_gil)
1227                 gilstate = PyGILState_Ensure();
1228
1229         py_func = py_data[BPY_DATA_CB_SLOT_SET];
1230
1231         args = PyTuple_New(2);
1232         self = pyrna_struct_as_instance(ptr);
1233         PyTuple_SET_ITEM(args, 0, self);
1234
1235         PyTuple_SET_ITEM(args, 1, PyLong_FromLong(value));
1236
1237         ret = PyObject_CallObject(py_func, args);
1238
1239         Py_DECREF(args);
1240
1241         if (ret == NULL) {
1242                 printf_func_error(py_func);
1243         }
1244         else {
1245                 if (ret != Py_None) {
1246                         PyErr_SetString(PyExc_ValueError, "the return value must be None");
1247                         printf_func_error(py_func);
1248                 }
1249
1250                 Py_DECREF(ret);
1251         }
1252
1253         if (use_gil)
1254                 PyGILState_Release(gilstate);
1255
1256         if (!is_write_ok) {
1257                 pyrna_write_set(false);
1258         }
1259 }
1260
1261 /* utility function we need for parsing int's in an if statement */
1262 static int py_long_as_int(PyObject *py_long, int *r_int)
1263 {
1264         if (PyLong_CheckExact(py_long)) {
1265                 *r_int = (int)PyLong_AS_LONG(py_long);
1266                 return 0;
1267         }
1268         else {
1269                 return -1;
1270         }
1271 }
1272
1273 #if 0
1274 /* copies orig to buf, then sets orig to buf, returns copy length */
1275 static size_t strswapbufcpy(char *buf, const char **orig)
1276 {
1277         const char *src = *orig;
1278         char *dst = buf;
1279         size_t i = 0;
1280         *orig = buf;
1281         while ((*dst = *src)) { dst++; src++; i++; }
1282         return i + 1; /* include '\0' */
1283 }
1284 #endif
1285
1286 static int icon_id_from_name(const char *name)
1287 {
1288         EnumPropertyItem *item;
1289         int id;
1290
1291         if (name[0]) {
1292                 for (item = icon_items, id = 0; item->identifier; item++, id++) {
1293                         if (STREQ(item->name, name)) {
1294                                 return item->value;
1295                         }
1296                 }
1297         }
1298         
1299         return 0;
1300 }
1301
1302 static EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, PyObject *def, int *defvalue, const short is_enum_flag)
1303 {
1304         EnumPropertyItem *items;
1305         PyObject *item;
1306         const Py_ssize_t seq_len = PySequence_Fast_GET_SIZE(seq_fast);
1307         Py_ssize_t totbuf = 0;
1308         int i;
1309         short def_used = 0;
1310         const char *def_cmp = NULL;
1311
1312         if (is_enum_flag) {
1313                 if (seq_len > RNA_ENUM_BITFLAG_SIZE) {
1314                         PyErr_SetString(PyExc_TypeError,
1315                                         "EnumProperty(...): maximum "
1316                                         STRINGIFY(RNA_ENUM_BITFLAG_SIZE)
1317                                         " members for a ENUM_FLAG type property");
1318                         return NULL;
1319                 }
1320                 if (def && !PySet_Check(def)) {
1321                         PyErr_Format(PyExc_TypeError,
1322                                      "EnumProperty(...): default option must be a 'set' "
1323                                      "type when ENUM_FLAG is enabled, not a '%.200s'",
1324                                      Py_TYPE(def)->tp_name);
1325                         return NULL;
1326                 }
1327         }
1328         else {
1329                 if (def) {
1330                         def_cmp = _PyUnicode_AsString(def);
1331                         if (def_cmp == NULL) {
1332                                 PyErr_Format(PyExc_TypeError,
1333                                              "EnumProperty(...): default option must be a 'str' "
1334                                              "type when ENUM_FLAG is disabled, not a '%.200s'",
1335                                              Py_TYPE(def)->tp_name);
1336                                 return NULL;
1337                         }
1338                 }
1339         }
1340
1341         /* blank value */
1342         *defvalue = 0;
1343
1344         items = MEM_callocN(sizeof(EnumPropertyItem) * (seq_len + 1), "enum_items_from_py1");
1345
1346         for (i = 0; i < seq_len; i++) {
1347                 EnumPropertyItem tmp = {0, "", 0, "", ""};
1348                 const char *tmp_icon = NULL;
1349                 Py_ssize_t item_size;
1350                 Py_ssize_t id_str_size;
1351                 Py_ssize_t name_str_size;
1352                 Py_ssize_t desc_str_size;
1353
1354                 item = PySequence_Fast_GET_ITEM(seq_fast, i);
1355
1356                 if ((PyTuple_CheckExact(item)) &&
1357                     (item_size = PyTuple_GET_SIZE(item)) &&
1358                     (item_size >= 3 && item_size <= 5) &&
1359                     (tmp.identifier =  _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 0), &id_str_size)) &&
1360                     (tmp.name =        _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 1), &name_str_size)) &&
1361                     (tmp.description = _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 2), &desc_str_size)) &&
1362                     /* TODO, number isn't ensured to be unique from the script author */
1363                     (item_size != 4 || py_long_as_int(PyTuple_GET_ITEM(item, 3), &tmp.value) != -1) &&
1364                     (item_size != 5 || ((tmp_icon = _PyUnicode_AsString(PyTuple_GET_ITEM(item, 3))) &&
1365                                         py_long_as_int(PyTuple_GET_ITEM(item, 4), &tmp.value) != -1)))
1366                 {
1367                         if (is_enum_flag) {
1368                                 if (item_size < 4) {
1369                                         tmp.value = 1 << i;
1370                                 }
1371
1372                                 if (def && PySet_Contains(def, PyTuple_GET_ITEM(item, 0))) {
1373                                         *defvalue |= tmp.value;
1374                                         def_used++;
1375                                 }
1376                         }
1377                         else {
1378                                 if (item_size < 4) {
1379                                         tmp.value = i;
1380                                 }
1381
1382                                 if (def && def_used == 0 && STREQ(def_cmp, tmp.identifier)) {
1383                                         *defvalue = tmp.value;
1384                                         def_used++; /* only ever 1 */
1385                                 }
1386                         }
1387
1388                         if (tmp_icon)
1389                                 tmp.icon = icon_id_from_name(tmp_icon);
1390
1391                         items[i] = tmp;
1392
1393                         /* calculate combine string length */
1394                         totbuf += id_str_size + name_str_size + desc_str_size + 3; /* 3 is for '\0's */
1395                 }
1396                 else {
1397                         MEM_freeN(items);
1398                         PyErr_SetString(PyExc_TypeError,
1399                                         "EnumProperty(...): expected a tuple containing "
1400                                         "(identifier, name, description) and optionally an "
1401                                         "icon name and unique number");
1402                         return NULL;
1403                 }
1404
1405         }
1406
1407         if (is_enum_flag) {
1408                 /* strict check that all set members were used */
1409                 if (def && def_used != PySet_GET_SIZE(def)) {
1410                         MEM_freeN(items);
1411
1412                         PyErr_Format(PyExc_TypeError,
1413                                      "EnumProperty(..., default={...}): set has %d unused member(s)",
1414                                      PySet_GET_SIZE(def) - def_used);
1415                         return NULL;
1416                 }
1417         }
1418         else {
1419                 if (def && def_used == 0) {
1420                         MEM_freeN(items);
1421
1422                         PyErr_Format(PyExc_TypeError,
1423                                      "EnumProperty(..., default=\'%s\'): not found in enum members",
1424                                      def_cmp);
1425                         return NULL;
1426                 }
1427         }
1428
1429         /* disabled duplicating strings because the array can still be freed and
1430          * the strings from it referenced, for now we can't support dynamically
1431          * created strings from python. */
1432 #if 0
1433         /* this would all work perfectly _but_ the python strings may be freed
1434          * immediately after use, so we need to duplicate them, ugh.
1435          * annoying because it works most of the time without this. */
1436         {
1437                 EnumPropertyItem *items_dup = MEM_mallocN((sizeof(EnumPropertyItem) * (seq_len + 1)) + (sizeof(char) * totbuf),
1438                                                           "enum_items_from_py2");
1439                 EnumPropertyItem *items_ptr = items_dup;
1440                 char *buf = ((char *)items_dup) + (sizeof(EnumPropertyItem) * (seq_len + 1));
1441                 memcpy(items_dup, items, sizeof(EnumPropertyItem) * (seq_len + 1));
1442                 for (i = 0; i < seq_len; i++, items_ptr++) {
1443                         buf += strswapbufcpy(buf, &items_ptr->identifier);
1444                         buf += strswapbufcpy(buf, &items_ptr->name);
1445                         buf += strswapbufcpy(buf, &items_ptr->description);
1446                 }
1447                 MEM_freeN(items);
1448                 items = items_dup;
1449         }
1450         /* end string duplication */
1451 #endif
1452
1453         return items;
1454 }
1455
1456 static EnumPropertyItem *bpy_prop_enum_itemf_cb(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, bool *r_free)
1457 {
1458         PyGILState_STATE gilstate;
1459
1460         PyObject *py_func = RNA_property_enum_py_data_get(prop);
1461         PyObject *self = NULL;
1462         PyObject *args;
1463         PyObject *items; /* returned from the function call */
1464
1465         EnumPropertyItem *eitems = NULL;
1466         int err = 0;
1467
1468         bpy_context_set(C, &gilstate);
1469
1470         args = PyTuple_New(2);
1471         self = pyrna_struct_as_instance(ptr);
1472         PyTuple_SET_ITEM(args, 0, self);
1473
1474         /* now get the context */
1475         PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
1476         Py_INCREF(bpy_context_module);
1477
1478         items = PyObject_CallObject(py_func, args);
1479
1480         Py_DECREF(args);
1481
1482         if (items == NULL) {
1483                 err = -1;
1484         }
1485         else {
1486                 PyObject *items_fast;
1487                 int defvalue_dummy = 0;
1488
1489                 if (!(items_fast = PySequence_Fast(items, "EnumProperty(...): "
1490                                                    "return value from the callback was not a sequence")))
1491                 {
1492                         err = -1;
1493                 }
1494                 else {
1495                         eitems = enum_items_from_py(items_fast, NULL, &defvalue_dummy,
1496                                                     (RNA_property_flag(prop) & PROP_ENUM_FLAG) != 0);
1497
1498                         Py_DECREF(items_fast);
1499
1500                         if (!eitems) {
1501                                 err = -1;
1502                         }
1503                 }
1504
1505                 Py_DECREF(items);
1506         }
1507
1508         if (err != -1) { /* worked */
1509                 *r_free = true;
1510         }
1511         else {
1512                 printf_func_error(py_func);
1513
1514                 eitems = DummyRNA_NULL_items;
1515         }
1516
1517
1518         bpy_context_clear(C, &gilstate);
1519         return eitems;
1520 }
1521
1522 static int bpy_prop_callback_check(PyObject *py_func, const char *keyword, int argcount)
1523 {
1524         if (py_func && py_func != Py_None) {
1525                 if (!PyFunction_Check(py_func)) {
1526                         PyErr_Format(PyExc_TypeError,
1527                                      "%s keyword: expected a function type, not a %.200s",
1528                                      keyword, Py_TYPE(py_func)->tp_name);
1529                         return -1;
1530                 }
1531                 else {
1532                         PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(py_func);
1533                         if (f_code->co_argcount != argcount) {
1534                                 PyErr_Format(PyExc_TypeError,
1535                                              "%s keyword: expected a function taking %d arguments, not %d",
1536                                              keyword, argcount, f_code->co_argcount);
1537                                 return -1;
1538                         }
1539                 }
1540         }
1541
1542         return 0;
1543 }
1544
1545 static PyObject **bpy_prop_py_data_get(struct PropertyRNA *prop)
1546 {
1547         PyObject **py_data = RNA_property_py_data_get(prop);
1548         if (!py_data) {
1549                 py_data = MEM_callocN(sizeof(PyObject *) * BPY_DATA_CB_SLOT_SIZE, __func__);
1550                 RNA_def_py_data(prop, py_data);
1551         }
1552         return py_data;
1553 }
1554
1555 static void bpy_prop_callback_assign_update(struct PropertyRNA *prop, PyObject *update_cb)
1556 {
1557         /* assume this is already checked for type and arg length */
1558         if (update_cb && update_cb != Py_None) {
1559                 PyObject **py_data = bpy_prop_py_data_get(prop);
1560
1561                 RNA_def_property_update_runtime(prop, (void *)bpy_prop_update_cb);
1562                 py_data[BPY_DATA_CB_SLOT_UPDATE] = update_cb;
1563
1564                 RNA_def_property_flag(prop, PROP_CONTEXT_PROPERTY_UPDATE);
1565         }
1566 }
1567
1568 static void bpy_prop_callback_assign_boolean(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1569 {
1570         BooleanPropertyGetFunc rna_get_cb = NULL;
1571         BooleanPropertySetFunc rna_set_cb = NULL;
1572         
1573         if (get_cb && get_cb != Py_None) {
1574                 PyObject **py_data = bpy_prop_py_data_get(prop);
1575
1576                 rna_get_cb = bpy_prop_boolean_get_cb;
1577                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1578         }
1579
1580         if (set_cb && set_cb != Py_None) {
1581                 PyObject **py_data = bpy_prop_py_data_get(prop);
1582
1583                 rna_set_cb = bpy_prop_boolean_set_cb;
1584                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1585         }
1586
1587         RNA_def_property_boolean_funcs_runtime(prop, rna_get_cb, rna_set_cb);
1588 }
1589
1590 static void bpy_prop_callback_assign_boolean_array(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1591 {
1592         BooleanArrayPropertyGetFunc rna_get_cb = NULL;
1593         BooleanArrayPropertySetFunc rna_set_cb = NULL;
1594         
1595         if (get_cb && get_cb != Py_None) {
1596                 PyObject **py_data = bpy_prop_py_data_get(prop);
1597
1598                 rna_get_cb = bpy_prop_boolean_array_get_cb;
1599                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1600         }
1601
1602         if (set_cb && set_cb != Py_None) {
1603                 PyObject **py_data = bpy_prop_py_data_get(prop);
1604
1605                 rna_set_cb = bpy_prop_boolean_array_set_cb;
1606                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1607         }
1608
1609         RNA_def_property_boolean_array_funcs_runtime(prop, rna_get_cb, rna_set_cb);
1610 }
1611
1612 static void bpy_prop_callback_assign_int(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1613 {
1614         IntPropertyGetFunc rna_get_cb = NULL;
1615         IntPropertySetFunc rna_set_cb = NULL;
1616         
1617         if (get_cb && get_cb != Py_None) {
1618                 PyObject **py_data = bpy_prop_py_data_get(prop);
1619
1620                 rna_get_cb = bpy_prop_int_get_cb;
1621                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1622         }
1623
1624         if (set_cb && set_cb != Py_None) {
1625                 PyObject **py_data = bpy_prop_py_data_get(prop);
1626
1627                 rna_set_cb = bpy_prop_int_set_cb;
1628                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1629         }
1630
1631         RNA_def_property_int_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1632 }
1633
1634 static void bpy_prop_callback_assign_int_array(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1635 {
1636         IntArrayPropertyGetFunc rna_get_cb = NULL;
1637         IntArrayPropertySetFunc rna_set_cb = NULL;
1638         
1639         if (get_cb && get_cb != Py_None) {
1640                 PyObject **py_data = bpy_prop_py_data_get(prop);
1641
1642                 rna_get_cb = bpy_prop_int_array_get_cb;
1643                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1644         }
1645
1646         if (set_cb && set_cb != Py_None) {
1647                 PyObject **py_data = bpy_prop_py_data_get(prop);
1648
1649                 rna_set_cb = bpy_prop_int_array_set_cb;
1650                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1651         }
1652
1653         RNA_def_property_int_array_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1654 }
1655
1656 static void bpy_prop_callback_assign_float(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1657 {
1658         FloatPropertyGetFunc rna_get_cb = NULL;
1659         FloatPropertySetFunc rna_set_cb = NULL;
1660         
1661         if (get_cb && get_cb != Py_None) {
1662                 PyObject **py_data = bpy_prop_py_data_get(prop);
1663
1664                 rna_get_cb = bpy_prop_float_get_cb;
1665                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1666         }
1667
1668         if (set_cb && set_cb != Py_None) {
1669                 PyObject **py_data = bpy_prop_py_data_get(prop);
1670
1671                 rna_set_cb = bpy_prop_float_set_cb;
1672                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1673         }
1674
1675         RNA_def_property_float_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1676 }
1677
1678 static void bpy_prop_callback_assign_float_array(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1679 {
1680         FloatArrayPropertyGetFunc rna_get_cb = NULL;
1681         FloatArrayPropertySetFunc rna_set_cb = NULL;
1682         
1683         if (get_cb && get_cb != Py_None) {
1684                 PyObject **py_data = bpy_prop_py_data_get(prop);
1685
1686                 rna_get_cb = bpy_prop_float_array_get_cb;
1687                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1688         }
1689
1690         if (set_cb && set_cb != Py_None) {
1691                 PyObject **py_data = bpy_prop_py_data_get(prop);
1692
1693                 rna_set_cb = bpy_prop_float_array_set_cb;
1694                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1695         }
1696
1697         RNA_def_property_float_array_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1698 }
1699
1700 static void bpy_prop_callback_assign_string(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1701 {
1702         StringPropertyGetFunc rna_get_cb = NULL;
1703         StringPropertyLengthFunc rna_length_cb = NULL;
1704         StringPropertySetFunc rna_set_cb = NULL;
1705         
1706         if (get_cb && get_cb != Py_None) {
1707                 PyObject **py_data = bpy_prop_py_data_get(prop);
1708
1709                 rna_get_cb = bpy_prop_string_get_cb;
1710                 rna_length_cb = bpy_prop_string_length_cb;
1711                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1712         }
1713
1714         if (set_cb && set_cb != Py_None) {
1715                 PyObject **py_data = bpy_prop_py_data_get(prop);
1716
1717                 rna_set_cb = bpy_prop_string_set_cb;
1718                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1719         }
1720
1721         RNA_def_property_string_funcs_runtime(prop, rna_get_cb, rna_length_cb, rna_set_cb);
1722 }
1723
1724 static void bpy_prop_callback_assign_enum(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb, PyObject *itemf_cb)
1725 {
1726         EnumPropertyGetFunc rna_get_cb = NULL;
1727         EnumPropertyItemFunc rna_itemf_cb = NULL;
1728         EnumPropertySetFunc rna_set_cb = NULL;
1729         
1730         if (get_cb && get_cb != Py_None) {
1731                 PyObject **py_data = bpy_prop_py_data_get(prop);
1732
1733                 rna_get_cb = bpy_prop_enum_get_cb;
1734                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1735         }
1736
1737         if (set_cb && set_cb != Py_None) {
1738                 PyObject **py_data = bpy_prop_py_data_get(prop);
1739
1740                 rna_set_cb = bpy_prop_enum_set_cb;
1741                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1742         }
1743
1744         if (itemf_cb && itemf_cb != Py_None) {
1745                 rna_itemf_cb = bpy_prop_enum_itemf_cb;
1746                 RNA_def_property_enum_py_data(prop, (void *)itemf_cb);
1747
1748                 /* watch out!, if a user is tricky they can probably crash blender
1749                  * if they manage to free the callback, take care! */
1750                 /* Py_INCREF(itemf_cb); */
1751         }
1752
1753         RNA_def_property_enum_funcs_runtime(prop, rna_get_cb, rna_set_cb, rna_itemf_cb);
1754 }
1755
1756 /* this define runs at the start of each function and deals with 
1757  * returning a deferred property (to be registered later) */
1758 #define BPY_PROPDEF_HEAD(_func)                                               \
1759         if (PyTuple_GET_SIZE(args) == 1) {                                        \
1760                 PyObject *ret;                                                        \
1761                 self = PyTuple_GET_ITEM(args, 0);                                     \
1762                 args = PyTuple_New(0);                                                \
1763                 ret = BPy_##_func(self, args, kw);                                    \
1764                 Py_DECREF(args);                                                      \
1765                 return ret;                                                           \
1766         }                                                                         \
1767         else if (PyTuple_GET_SIZE(args) > 1) {                                    \
1768                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");       \
1769                 return NULL;                                                          \
1770         }                                                                         \
1771         srna = srna_from_self(self, #_func"(...):");                              \
1772         if (srna == NULL) {                                                       \
1773                 if (PyErr_Occurred())                                                 \
1774                         return NULL;                                                      \
1775                 return bpy_prop_deferred_return(pymeth_##_func, kw);                  \
1776         } (void)0
1777
1778 /* terse macros for error checks shared between all funcs cant use function
1779  * calls because of static strings passed to pyrna_set_to_enum_bitfield */
1780 #define BPY_PROPDEF_CHECK(_func, _property_flag_items)                        \
1781         if (UNLIKELY(id_len >= MAX_IDPROP_NAME)) {                                \
1782                 PyErr_Format(PyExc_TypeError,                                         \
1783                              #_func"(): '%.200s' too long, max length is %d",         \
1784                              id, MAX_IDPROP_NAME - 1);                                \
1785                 return NULL;                                                          \
1786         }                                                                         \
1787         if (UNLIKELY(RNA_def_property_free_identifier(srna, id) == -1)) {         \
1788                 PyErr_Format(PyExc_TypeError,                                         \
1789                              #_func"(): '%s' is defined as a non-dynamic type",       \
1790                              id);                                                     \
1791                 return NULL;                                                          \
1792         }                                                                         \
1793         if (UNLIKELY(pyopts && pyrna_set_to_enum_bitfield(_property_flag_items,   \
1794                                                  pyopts,                          \
1795                                                  &opts,                           \
1796                                                  #_func"(options={ ...}):")))     \
1797         {                                                                         \
1798                 return NULL;                                                          \
1799         } (void)0
1800
1801 #define BPY_PROPDEF_SUBTYPE_CHECK(_func, _property_flag_items, _subtype)      \
1802         BPY_PROPDEF_CHECK(_func, _property_flag_items);                           \
1803         if (UNLIKELY(pysubtype && RNA_enum_value_from_id(_subtype,                \
1804                                                 pysubtype,                        \
1805                                                 &subtype) == 0))                  \
1806         {                                                                         \
1807                 const char *enum_str = BPy_enum_as_string(_subtype);                  \
1808                 PyErr_Format(PyExc_TypeError,                                         \
1809                              #_func"(subtype='%s'): "                                 \
1810                              "subtype not found in (%s)",                             \
1811                              pysubtype, enum_str);                                    \
1812                 MEM_freeN((void *)enum_str);                                          \
1813                 return NULL;                                                          \
1814         } (void)0
1815
1816
1817 #define BPY_PROPDEF_NAME_DOC \
1818 "   :arg name: Name used in the user interface.\n" \
1819 "   :type name: string\n" \
1820
1821
1822 #define BPY_PROPDEF_DESC_DOC \
1823 "   :arg description: Text used for the tooltip and api documentation.\n" \
1824 "   :type description: string\n" \
1825
1826
1827 #define BPY_PROPDEF_UNIT_DOC \
1828 "   :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION'].\n" \
1829 "   :type unit: string\n"       \
1830
1831
1832 #define BPY_PROPDEF_UPDATE_DOC \
1833 "   :arg update: function to be called when this value is modified,\n" \
1834 "      This function must take 2 values (self, context) and return None.\n" \
1835 "      *Warning* there are no safety checks to avoid infinite recursion.\n" \
1836 "   :type update: function\n" \
1837
1838 #if 0
1839 static int bpy_struct_id_used(StructRNA *srna, char *identifier)
1840 {
1841         PointerRNA ptr;
1842         RNA_pointer_create(NULL, srna, NULL, &ptr);
1843         return (RNA_struct_find_property(&ptr, identifier) != NULL);
1844 }
1845 #endif
1846
1847
1848 /* Function that sets RNA, NOTE - self is NULL when called from python,
1849  * but being abused from C so we can pass the srna along.
1850  * This isn't incorrect since its a python object - but be careful */
1851 PyDoc_STRVAR(BPy_BoolProperty_doc,
1852 ".. function:: BoolProperty(name=\"\", "
1853                            "description=\"\", "
1854                            "default=False, "
1855                            "options={'ANIMATABLE'}, "
1856                            "subtype='NONE', "
1857                            "update=None, "
1858                            "get=None, "
1859                            "set=None)\n"
1860 "\n"
1861 "   Returns a new boolean property definition.\n"
1862 "\n"
1863 BPY_PROPDEF_NAME_DOC
1864 BPY_PROPDEF_DESC_DOC
1865 BPY_PROPDEF_OPTIONS_DOC
1866 BPY_PROPDEF_SUBTYPE_NUMBER_DOC
1867 BPY_PROPDEF_UPDATE_DOC
1868 );
1869 static PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
1870 {
1871         StructRNA *srna;
1872
1873         BPY_PROPDEF_HEAD(BoolProperty);
1874
1875         if (srna) {
1876                 static const char *kwlist[] = {"attr", "name", "description", "default",
1877                                                "options", "subtype", "update", "get", "set", NULL};
1878                 const char *id = NULL, *name = NULL, *description = "";
1879                 int id_len;
1880                 int def = 0;
1881                 PropertyRNA *prop;
1882                 PyObject *pyopts = NULL;
1883                 int opts = 0;
1884                 char *pysubtype = NULL;
1885                 int subtype = PROP_NONE;
1886                 PyObject *update_cb = NULL;
1887                 PyObject *get_cb = NULL;
1888                 PyObject *set_cb = NULL;
1889
1890                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1891                                                  "s#|ssiO!sOOO:BoolProperty",
1892                                                  (char **)kwlist, &id, &id_len,
1893                                                  &name, &description, &def,
1894                                                  &PySet_Type, &pyopts, &pysubtype,
1895                                                  &update_cb, &get_cb, &set_cb))
1896                 {
1897                         return NULL;
1898                 }
1899
1900                 BPY_PROPDEF_SUBTYPE_CHECK(BoolProperty, property_flag_items, property_subtype_number_items);
1901
1902                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
1903                         return NULL;
1904                 }
1905                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
1906                         return NULL;
1907                 }
1908                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
1909                         return NULL;
1910                 }
1911
1912                 prop = RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
1913                 RNA_def_property_boolean_default(prop, def);
1914                 RNA_def_property_ui_text(prop, name ? name : id, description);
1915
1916                 if (pyopts) {
1917                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
1918                         if ((opts & PROP_ANIMATABLE) == 0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
1919                         if (opts & PROP_SKIP_SAVE) RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1920                         if (opts & PROP_LIB_EXCEPTION) RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
1921                 }
1922                 bpy_prop_callback_assign_update(prop, update_cb);
1923                 bpy_prop_callback_assign_boolean(prop, get_cb, set_cb);
1924                 RNA_def_property_duplicate_pointers(srna, prop);
1925         }
1926
1927         Py_RETURN_NONE;
1928 }
1929
1930 PyDoc_STRVAR(BPy_BoolVectorProperty_doc,
1931 ".. function:: BoolVectorProperty(name=\"\", "
1932                                  "description=\"\", "
1933                                  "default=(False, False, False), "
1934                                  "options={'ANIMATABLE'}, "
1935                                  "subtype='NONE', "
1936                                  "size=3, "
1937                                  "update=None, "
1938                                  "get=None, "
1939                                  "set=None)\n"
1940 "\n"
1941 "   Returns a new vector boolean property definition.\n"
1942 "\n"
1943 BPY_PROPDEF_NAME_DOC
1944 BPY_PROPDEF_DESC_DOC
1945 "   :arg default: sequence of booleans the length of *size*.\n"
1946 "   :type default: sequence\n"
1947 BPY_PROPDEF_OPTIONS_DOC
1948 BPY_PROPDEF_SUBTYPE_ARRAY_DOC
1949 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
1950 "   :type size: int\n"
1951 BPY_PROPDEF_UPDATE_DOC
1952 );
1953 static PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
1954 {
1955         StructRNA *srna;
1956
1957         BPY_PROPDEF_HEAD(BoolVectorProperty);
1958
1959         if (srna) {
1960                 static const char *kwlist[] = {"attr", "name", "description", "default",
1961                                                "options", "subtype", "size", "update", "get", "set", NULL};
1962                 const char *id = NULL, *name = NULL, *description = "";
1963                 int id_len;
1964                 int def[PYRNA_STACK_ARRAY] = {0};
1965                 int size = 3;
1966                 PropertyRNA *prop;
1967                 PyObject *pydef = NULL;
1968                 PyObject *pyopts = NULL;
1969                 int opts = 0;
1970                 char *pysubtype = NULL;
1971                 int subtype = PROP_NONE;
1972                 PyObject *update_cb = NULL;
1973                 PyObject *get_cb = NULL;
1974                 PyObject *set_cb = NULL;
1975
1976                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1977                                                  "s#|ssOO!siOOO:BoolVectorProperty",
1978                                                  (char **)kwlist, &id, &id_len,
1979                                                  &name, &description, &pydef,
1980                                                  &PySet_Type, &pyopts, &pysubtype, &size,
1981                                                  &update_cb, &get_cb, &set_cb))
1982                 {
1983                         return NULL;
1984                 }
1985
1986                 BPY_PROPDEF_SUBTYPE_CHECK(BoolVectorProperty, property_flag_items, property_subtype_array_items);
1987
1988                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
1989                         PyErr_Format(PyExc_TypeError,
1990                                      "BoolVectorProperty(size=%d): size must be between 0 and "
1991                                      STRINGIFY(PYRNA_STACK_ARRAY), size);
1992                         return NULL;
1993                 }
1994
1995                 if (pydef && PyC_AsArray(def, pydef, size, &PyBool_Type, false, "BoolVectorProperty(default=sequence)") == -1)
1996                         return NULL;
1997
1998                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
1999                         return NULL;
2000                 }
2001                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2002                         return NULL;
2003                 }
2004                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2005                         return NULL;
2006                 }
2007
2008                 // prop = RNA_def_boolean_array(srna, id, size, pydef ? def:NULL, name ? name : id, description);
2009                 prop = RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
2010                 RNA_def_property_array(prop, size);
2011                 if (pydef) RNA_def_property_boolean_array_default(prop, def);
2012                 RNA_def_property_ui_text(prop, name ? name : id, description);
2013
2014                 if (pyopts) {
2015                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
2016                         if ((opts & PROP_ANIMATABLE) == 0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
2017                         if (opts & PROP_SKIP_SAVE) RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2018                         if (opts & PROP_LIB_EXCEPTION) RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
2019                 }
2020                 bpy_prop_callback_assign_update(prop, update_cb);
2021                 bpy_prop_callback_assign_boolean_array(prop, get_cb, set_cb);
2022                 RNA_def_property_duplicate_pointers(srna, prop);
2023         }
2024         
2025         Py_RETURN_NONE;
2026 }
2027
2028 PyDoc_STRVAR(BPy_IntProperty_doc,
2029 ".. function:: IntProperty(name=\"\", "
2030                           "description=\"\", "
2031                           "default=0, "
2032                           "min=-2**31, max=2**31-1, "
2033                           "soft_min=-2**31, soft_max=2**31-1, "
2034                           "step=1, "
2035                           "options={'ANIMATABLE'}, "
2036                           "subtype='NONE', "
2037                           "update=None, "
2038                           "get=None, "
2039                           "set=None)\n"
2040 "\n"
2041 "   Returns a new int property definition.\n"
2042 "\n"
2043 BPY_PROPDEF_NAME_DOC
2044 BPY_PROPDEF_DESC_DOC
2045 BPY_PROPDEF_OPTIONS_DOC
2046 BPY_PROPDEF_SUBTYPE_NUMBER_DOC
2047 BPY_PROPDEF_UPDATE_DOC
2048 );
2049 static PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
2050 {
2051         StructRNA *srna;
2052
2053         BPY_PROPDEF_HEAD(IntProperty);
2054
2055         if (srna) {
2056                 static const char *kwlist[] = {"attr", "name", "description", "default",
2057                                                "min", "max", "soft_min", "soft_max",
2058                                                "step", "options", "subtype", "update", "get", "set", NULL};
2059                 const char *id = NULL, *name = NULL, *description = "";
2060                 int id_len;
2061                 int min = INT_MIN, max = INT_MAX, soft_min = INT_MIN, soft_max = INT_MAX, step = 1, def = 0;
2062                 PropertyRNA *prop;
2063                 PyObject *pyopts = NULL;
2064                 int opts = 0;
2065                 char *pysubtype = NULL;
2066                 int subtype = PROP_NONE;
2067                 PyObject *update_cb = NULL;
2068                 PyObject *get_cb = NULL;
2069                 PyObject *set_cb = NULL;
2070
2071                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2072                                                  "s#|ssiiiiiiO!sOOO:IntProperty",
2073                                                  (char **)kwlist, &id, &id_len,
2074                                                  &name, &description, &def,
2075                                                  &min, &max, &soft_min, &soft_max,
2076                                                  &step, &PySet_Type, &pyopts, &pysubtype,
2077                                                  &update_cb, &get_cb, &set_cb))
2078                 {
2079                         return NULL;
2080                 }
2081
2082                 BPY_PROPDEF_SUBTYPE_CHECK(IntProperty, property_flag_items, property_subtype_number_items);
2083
2084                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2085                         return NULL;
2086                 }
2087                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2088                         return NULL;
2089                 }
2090                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2091                         return NULL;
2092                 }
2093
2094                 prop = RNA_def_property(srna, id, PROP_INT, subtype);
2095                 RNA_def_property_int_default(prop, def);
2096                 RNA_def_property_ui_text(prop, name ? name : id, description);
2097                 RNA_def_property_range(prop, min, max);
2098                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
2099
2100                 if (pyopts) {
2101                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
2102                         if ((opts & PROP_ANIMATABLE) == 0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
2103                         if (opts & PROP_SKIP_SAVE) RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2104                         if (opts & PROP_LIB_EXCEPTION) RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
2105                 }
2106                 bpy_prop_callback_assign_update(prop, update_cb);
2107                 bpy_prop_callback_assign_int(prop, get_cb, set_cb);
2108                 RNA_def_property_duplicate_pointers(srna, prop);
2109         }
2110         Py_RETURN_NONE;
2111 }
2112
2113 PyDoc_STRVAR(BPy_IntVectorProperty_doc,
2114 ".. function:: IntVectorProperty(name=\"\", "
2115                                 "description=\"\", "
2116                                 "default=(0, 0, 0), min=-2**31, max=2**31-1, "
2117                                 "soft_min=-2**31, "
2118                                 "soft_max=2**31-1, "
2119                                 "options={'ANIMATABLE'}, "
2120                                 "subtype='NONE', "
2121                                 "size=3, "
2122                                 "update=None, "
2123                                 "get=None, "
2124                                 "set=None)\n"
2125 "\n"
2126 "   Returns a new vector int property definition.\n"
2127 "\n"
2128 BPY_PROPDEF_NAME_DOC
2129 BPY_PROPDEF_DESC_DOC
2130 "   :arg default: sequence of ints the length of *size*.\n"
2131 "   :type default: sequence\n"
2132 BPY_PROPDEF_OPTIONS_DOC
2133 BPY_PROPDEF_SUBTYPE_ARRAY_DOC
2134 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
2135 "   :type size: int\n"
2136 BPY_PROPDEF_UPDATE_DOC
2137 );
2138 static PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
2139 {
2140         StructRNA *srna;
2141
2142         BPY_PROPDEF_HEAD(IntVectorProperty);
2143
2144         if (srna) {
2145                 static const char *kwlist[] = {"attr", "name", "description", "default",
2146                                                "min", "max", "soft_min", "soft_max",
2147                                                "step", "options", "subtype", "size", "update", "get", "set", NULL};
2148                 const char *id = NULL, *name = NULL, *description = "";
2149                 int id_len;
2150                 int min = INT_MIN, max = INT_MAX, soft_min = INT_MIN, soft_max = INT_MAX, step = 1;
2151                 int def[PYRNA_STACK_ARRAY] = {0};
2152                 int size = 3;
2153                 PropertyRNA *prop;
2154                 PyObject *pydef = NULL;
2155                 PyObject *pyopts = NULL;
2156                 int opts = 0;
2157                 char *pysubtype = NULL;
2158                 int subtype = PROP_NONE;
2159                 PyObject *update_cb = NULL;
2160                 PyObject *get_cb = NULL;
2161                 PyObject *set_cb = NULL;
2162
2163                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2164                                                  "s#|ssOiiiiiO!siOOO:IntVectorProperty",
2165                                                  (char **)kwlist, &id, &id_len,
2166                                                  &name, &description, &pydef,
2167                                                  &min, &max, &soft_min, &soft_max,
2168                                                  &step, &PySet_Type, &pyopts,
2169                                                  &pysubtype, &size,
2170                                                  &update_cb, &get_cb, &set_cb))
2171                 {
2172                         return NULL;
2173                 }
2174
2175                 BPY_PROPDEF_SUBTYPE_CHECK(IntVectorProperty, property_flag_items, property_subtype_array_items);
2176
2177                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
2178                         PyErr_Format(PyExc_TypeError,
2179                                      "IntVectorProperty(size=%d): size must be between 0 and "
2180                                      STRINGIFY(PYRNA_STACK_ARRAY), size);
2181                         return NULL;
2182                 }
2183
2184                 if (pydef && PyC_AsArray(def, pydef, size, &PyLong_Type, false, "IntVectorProperty(default=sequence)") == -1)
2185                         return NULL;
2186
2187                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2188                         return NULL;
2189                 }
2190                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2191                         return NULL;
2192                 }
2193                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2194                         return NULL;
2195                 }
2196
2197                 prop = RNA_def_property(srna, id, PROP_INT, subtype);
2198                 RNA_def_property_array(prop, size);
2199                 if (pydef) RNA_def_property_int_array_default(prop, def);
2200                 RNA_def_property_range(prop, min, max);
2201                 RNA_def_property_ui_text(prop, name ? name : id, description);
2202                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
2203
2204                 if (pyopts) {
2205                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
2206                         if ((opts & PROP_ANIMATABLE) == 0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
2207                         if (opts & PROP_SKIP_SAVE) RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2208                         if (opts & PROP_LIB_EXCEPTION) RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
2209                 }
2210                 bpy_prop_callback_assign_update(prop, update_cb);
2211                 bpy_prop_callback_assign_int_array(prop, get_cb, set_cb);
2212                 RNA_def_property_duplicate_pointers(srna, prop);
2213         }
2214         Py_RETURN_NONE;
2215 }
2216
2217
2218 PyDoc_STRVAR(BPy_FloatProperty_doc,
2219 ".. function:: FloatProperty(name=\"\", "
2220                             "description=\"\", "
2221                             "default=0.0, "
2222                             "min=sys.float_info.min, max=sys.float_info.max, "
2223                             "soft_min=sys.float_info.min, soft_max=sys.float_info.max, "
2224                             "step=3, "
2225                             "precision=2, "
2226                             "options={'ANIMATABLE'}, "
2227                             "subtype='NONE', "
2228                             "unit='NONE', "
2229                             "update=None, "
2230                             "get=None, "
2231                             "set=None)\n"
2232 "\n"
2233 "   Returns a new float property definition.\n"
2234 "\n"
2235 BPY_PROPDEF_NAME_DOC
2236 BPY_PROPDEF_DESC_DOC
2237 BPY_PROPDEF_OPTIONS_DOC
2238 BPY_PROPDEF_SUBTYPE_NUMBER_DOC
2239 BPY_PROPDEF_UNIT_DOC
2240 BPY_PROPDEF_UPDATE_DOC
2241 "   :arg precision: Number of digits of precision to display.\n"
2242 "   :type precision: int\n"
2243 );
2244 static PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
2245 {
2246         StructRNA *srna;
2247
2248         BPY_PROPDEF_HEAD(FloatProperty);
2249
2250         if (srna) {
2251                 static const char *kwlist[] = {"attr", "name", "description", "default",
2252                                                "min", "max", "soft_min", "soft_max",
2253                                                "step", "precision", "options", "subtype",
2254                                                "unit", "update", "get", "set", NULL};
2255                 const char *id = NULL, *name = NULL, *description = "";
2256                 int id_len;
2257                 float min = -FLT_MAX, max = FLT_MAX, soft_min = -FLT_MAX, soft_max = FLT_MAX, step = 3, def = 0.0f;
2258                 int precision = 2;
2259                 PropertyRNA *prop;
2260                 PyObject *pyopts = NULL;
2261                 int opts = 0;
2262                 char *pysubtype = NULL;
2263                 int subtype = PROP_NONE;
2264                 char *pyunit = NULL;
2265                 int unit = PROP_UNIT_NONE;
2266                 PyObject *update_cb = NULL;
2267                 PyObject *get_cb = NULL;
2268                 PyObject *set_cb = NULL;
2269
2270                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2271                                                  "s#|ssffffffiO!ssOOO:FloatProperty",
2272                                                  (char **)kwlist, &id, &id_len,
2273                                                  &name, &description, &def,
2274                                                  &min, &max, &soft_min, &soft_max,
2275                                                  &step, &precision, &PySet_Type,
2276                                                  &pyopts, &pysubtype, &pyunit,
2277                                                  &update_cb, &get_cb, &set_cb))
2278                 {
2279                         return NULL;
2280                 }
2281
2282                 BPY_PROPDEF_SUBTYPE_CHECK(FloatProperty, property_flag_items, property_subtype_number_items);
2283
2284                 if (pyunit && RNA_enum_value_from_id(property_unit_items, pyunit, &unit) == 0) {
2285                         PyErr_Format(PyExc_TypeError, "FloatProperty(unit='%s'): invalid unit", pyunit);
2286                         return NULL;
2287                 }
2288
2289                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2290                         return NULL;
2291                 }
2292                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2293                         return NULL;
2294                 }
2295                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2296                         return NULL;
2297                 }
2298
2299                 prop = RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
2300                 RNA_def_property_float_default(prop, def);
2301                 RNA_def_property_range(prop, min, max);
2302                 RNA_def_property_ui_text(prop, name ? name : id, description);
2303                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
2304
2305                 if (pyopts) {
2306                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
2307                         if ((opts & PROP_ANIMATABLE) == 0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
2308                         if (opts & PROP_SKIP_SAVE) RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2309                         if (opts & PROP_LIB_EXCEPTION) RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
2310                 }
2311                 bpy_prop_callback_assign_update(prop, update_cb);
2312                 bpy_prop_callback_assign_float(prop, get_cb, set_cb);
2313                 RNA_def_property_duplicate_pointers(srna, prop);
2314         }
2315         Py_RETURN_NONE;
2316 }
2317
2318 PyDoc_STRVAR(BPy_FloatVectorProperty_doc,
2319 ".. function:: FloatVectorProperty(name=\"\", "
2320                                   "description=\"\", "
2321                                   "default=(0.0, 0.0, 0.0), "
2322                                   "min=sys.float_info.min, max=sys.float_info.max, "
2323                                   "soft_min=sys.float_info.min, soft_max=sys.float_info.max, "
2324                                   "step=3, "
2325                                   "precision=2, "
2326                                   "options={'ANIMATABLE'}, "
2327                                   "subtype='NONE', "
2328                                   "size=3, "
2329                                   "update=None, "
2330                                   "get=None, "
2331                                   "set=None)\n"
2332 "\n"
2333 "   Returns a new vector float property definition.\n"
2334 "\n"
2335 BPY_PROPDEF_NAME_DOC
2336 BPY_PROPDEF_DESC_DOC
2337 "   :arg default: sequence of floats the length of *size*.\n"
2338 "   :type default: sequence\n"
2339 BPY_PROPDEF_OPTIONS_DOC
2340 BPY_PROPDEF_SUBTYPE_ARRAY_DOC
2341 BPY_PROPDEF_UNIT_DOC
2342 "   :arg size: Vector dimensions in [1, and " STRINGIFY(PYRNA_STACK_ARRAY) "].\n"
2343 "   :type size: int\n"
2344 "   :arg precision: Number of digits of precision to display.\n"
2345 "   :type precision: int\n"
2346 BPY_PROPDEF_UPDATE_DOC
2347 );
2348 static PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
2349 {
2350         StructRNA *srna;
2351
2352         BPY_PROPDEF_HEAD(FloatVectorProperty);
2353
2354         if (srna) {
2355                 static const char *kwlist[] = {"attr", "name", "description", "default",
2356                                                "min", "max", "soft_min", "soft_max",
2357                                                "step", "precision", "options", "subtype",
2358                                                "unit", "size", "update", "get", "set", NULL};
2359                 const char *id = NULL, *name = NULL, *description = "";
2360                 int id_len;
2361                 float min = -FLT_MAX, max = FLT_MAX, soft_min = -FLT_MAX, soft_max = FLT_MAX, step = 3;
2362                 float def[PYRNA_STACK_ARRAY] = {0.0f};
2363                 int precision = 2, size = 3;
2364                 PropertyRNA *prop;
2365                 PyObject *pydef = NULL;
2366                 PyObject *pyopts = NULL;
2367                 int opts = 0;
2368                 char *pysubtype = NULL;
2369                 int subtype = PROP_NONE;
2370                 char *pyunit = NULL;
2371                 int unit = PROP_UNIT_NONE;
2372                 PyObject *update_cb = NULL;
2373                 PyObject *get_cb = NULL;
2374                 PyObject *set_cb = NULL;
2375
2376                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2377                                                  "s#|ssOfffffiO!ssiOOO:FloatVectorProperty",
2378                                                  (char **)kwlist, &id, &id_len,
2379                                                  &name, &description, &pydef,
2380                                                  &min, &max, &soft_min, &soft_max,
2381                                                  &step, &precision, &PySet_Type,
2382                                                  &pyopts, &pysubtype, &pyunit, &size,
2383                                                  &update_cb, &get_cb, &set_cb))
2384                 {
2385                         return NULL;
2386                 }
2387
2388                 BPY_PROPDEF_SUBTYPE_CHECK(FloatVectorProperty, property_flag_items, property_subtype_array_items);
2389
2390                 if (pyunit && RNA_enum_value_from_id(property_unit_items, pyunit, &unit) == 0) {
2391                         PyErr_Format(PyExc_TypeError, "FloatVectorProperty(unit='%s'): invalid unit", pyunit);
2392                         return NULL;
2393                 }
2394
2395                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
2396                         PyErr_Format(PyExc_TypeError,
2397                                      "FloatVectorProperty(size=%d): size must be between 0 and "
2398                                      STRINGIFY(PYRNA_STACK_ARRAY), size);
2399                         return NULL;
2400                 }
2401
2402                 if (pydef && PyC_AsArray(def, pydef, size, &PyFloat_Type, false, "FloatVectorProperty(default=sequence)") == -1)
2403                         return NULL;
2404
2405                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2406                         return NULL;
2407                 }
2408                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2409                         return NULL;
2410                 }
2411                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2412                         return NULL;
2413                 }
2414
2415                 prop = RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
2416                 RNA_def_property_array(prop, size);
2417                 if (pydef) RNA_def_property_float_array_default(prop, def);
2418                 RNA_def_property_range(prop, min, max);
2419                 RNA_def_property_ui_text(prop, name ? name : id, description);
2420                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
2421
2422                 if (pyopts) {
2423                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
2424                         if ((opts & PROP_ANIMATABLE) == 0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
2425                         if (opts & PROP_SKIP_SAVE) RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2426                         if (opts & PROP_LIB_EXCEPTION) RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
2427                 }
2428                 bpy_prop_callback_assign_update(prop, update_cb);
2429                 bpy_prop_callback_assign_float_array(prop, get_cb, set_cb);
2430                 RNA_def_property_duplicate_pointers(srna, prop);
2431         }
2432         Py_RETURN_NONE;
2433 }
2434
2435 PyDoc_STRVAR(BPy_StringProperty_doc,
2436 ".. function:: StringProperty(name=\"\", "
2437                              "description=\"\", "
2438                              "default=\"\", "
2439                              "maxlen=0, "
2440                              "options={'ANIMATABLE'}, "
2441                              "subtype='NONE', "
2442                              "update=None, "
2443                              "get=None, "
2444                              "set=None)\n"
2445 "\n"
2446 "   Returns a new string property definition.\n"
2447 "\n"
2448 BPY_PROPDEF_NAME_DOC
2449 BPY_PROPDEF_DESC_DOC
2450 "   :arg default: initializer string.\n"
2451 "   :type default: string\n"
2452 BPY_PROPDEF_OPTIONS_DOC
2453 BPY_PROPDEF_SUBTYPE_STRING_DOC
2454 BPY_PROPDEF_UPDATE_DOC
2455 );
2456 static PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
2457 {
2458         StructRNA *srna;
2459
2460         BPY_PROPDEF_HEAD(StringProperty);
2461
2462         if (srna) {
2463                 static const char *kwlist[] = {"attr", "name", "description", "default",
2464                                                "maxlen", "options", "subtype", "update", "get", "set", NULL};
2465                 const char *id = NULL, *name = NULL, *description = "", *def = "";
2466                 int id_len;
2467                 int maxlen = 0;
2468                 PropertyRNA *prop;
2469                 PyObject *pyopts = NULL;
2470                 int opts = 0;
2471                 char *pysubtype = NULL;
2472                 int subtype = PROP_NONE;
2473                 PyObject *update_cb = NULL;
2474                 PyObject *get_cb = NULL;
2475                 PyObject *set_cb = NULL;
2476
2477                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2478                                                  "s#|sssiO!sOOO:StringProperty",
2479                                                  (char **)kwlist, &id, &id_len,
2480                                                  &name, &description, &def,
2481                                                  &maxlen, &PySet_Type, &pyopts, &pysubtype,
2482                                                  &update_cb, &get_cb, &set_cb))
2483                 {
2484                         return NULL;
2485                 }
2486
2487                 BPY_PROPDEF_SUBTYPE_CHECK(StringProperty, property_flag_items, property_subtype_string_items);
2488
2489                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2490                         return NULL;
2491                 }
2492                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2493                         return NULL;
2494                 }
2495                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2496                         return NULL;
2497                 }
2498
2499                 prop = RNA_def_property(srna, id, PROP_STRING, subtype);
2500                 if (maxlen != 0) RNA_def_property_string_maxlength(prop, maxlen + 1);  /* +1 since it includes null terminator */
2501                 if (def && def[0]) RNA_def_property_string_default(prop, def);
2502                 RNA_def_property_ui_text(prop, name ? name : id, description);
2503
2504                 if (pyopts) {
2505                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
2506                         if ((opts & PROP_ANIMATABLE) == 0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
2507                         if (opts & PROP_SKIP_SAVE) RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2508                         if (opts & PROP_LIB_EXCEPTION) RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
2509                 }
2510                 bpy_prop_callback_assign_update(prop, update_cb);
2511                 bpy_prop_callback_assign_string(prop, get_cb, set_cb);
2512                 RNA_def_property_duplicate_pointers(srna, prop);
2513         }
2514         Py_RETURN_NONE;
2515 }
2516
2517 PyDoc_STRVAR(BPy_EnumProperty_doc,
2518 ".. function:: EnumProperty(items, "
2519                            "name=\"\", "
2520                            "description=\"\", "
2521                            "default=\"\", "
2522                            "options={'ANIMATABLE'}, "
2523                            "update=None, "
2524                            "get=None, "
2525                            "set=None)\n"
2526 "\n"
2527 "   Returns a new enumerator property definition.\n"
2528 "\n"
2529 BPY_PROPDEF_NAME_DOC
2530 BPY_PROPDEF_DESC_DOC
2531 "   :arg default: The default value for this enum, a string from the identifiers used in *items*.\n"
2532 "      If the *ENUM_FLAG* option is used this must be a set of such string identifiers instead.\n"
2533 BPY_PROPDEF_OPTIONS_ENUM_DOC
2534 "   :type options: set\n"
2535 "   :arg items: sequence of enum items formatted:\n"
2536 "      [(identifier, name, description, icon, number), ...] where the identifier is used\n"
2537 "      for python access and other values are used for the interface.\n"
2538 "      The three first elements of the tuples are mandatory.\n"
2539 "      The forth one is either the (unique!) number id of the item or, if followed by a fith element \n"
2540 "      (which must be the numid), an icon string identifier.\n"
2541 "      Note the item is optional.\n"
2542 "      For dynamic values a callback can be passed which returns a list in\n"
2543 "      the same format as the static list.\n"
2544 "      This function must take 2 arguments (self, context)\n"
2545 "      WARNING: Do not use generators here (they will work the first time, but will lead to empty values\n"
2546 "      in some unload/reload scenarii)!\n"
2547 "   :type items: sequence of string tuples or a function\n"
2548 BPY_PROPDEF_UPDATE_DOC
2549 );
2550 static PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw)
2551 {
2552         StructRNA *srna;
2553
2554         BPY_PROPDEF_HEAD(EnumProperty);
2555         
2556         if (srna) {
2557                 static const char *kwlist[] = {"attr", "items", "name", "description", "default",
2558                                                "options", "update", "get", "set", NULL};
2559                 const char *id = NULL, *name = NULL, *description = "";
2560                 PyObject *def = NULL;
2561                 int id_len;
2562                 int defvalue = 0;
2563                 PyObject *items, *items_fast;
2564                 EnumPropertyItem *eitems;
2565                 PropertyRNA *prop;
2566                 PyObject *pyopts = NULL;
2567                 int opts = 0;
2568                 bool is_itemf = false;
2569                 PyObject *update_cb = NULL;
2570                 PyObject *get_cb = NULL;
2571                 PyObject *set_cb = NULL;
2572
2573                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2574                                                  "s#O|ssOO!OOO:EnumProperty",
2575                                                  (char **)kwlist, &id, &id_len,
2576                                                  &items, &name, &description,
2577                                                  &def, &PySet_Type, &pyopts,
2578                                                  &update_cb, &get_cb, &set_cb))
2579                 {
2580                         return NULL;
2581                 }
2582
2583                 BPY_PROPDEF_CHECK(EnumProperty, property_flag_enum_items);
2584
2585                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2586                         return NULL;
2587                 }
2588                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2589                         return NULL;
2590                 }
2591                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2592                         return NULL;
2593                 }
2594
2595                 /* items can be a list or a callable */
2596                 if (PyFunction_Check(items)) { /* don't use PyCallable_Check because we need the function code for errors */
2597                         PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(items);
2598                         if (f_code->co_argcount != 2) {
2599                                 PyErr_Format(PyExc_ValueError,
2600                                              "EnumProperty(...): expected 'items' function to take 2 arguments, not %d",
2601                                              f_code->co_argcount);
2602                                 return NULL;
2603                         }
2604
2605                         if (def) {
2606                                 /* note, using type error here is odd but python does this for invalid arguments */
2607                                 PyErr_SetString(PyExc_TypeError,
2608                                                 "EnumProperty(...): 'default' can't be set when 'items' is a function");
2609                                 return NULL;
2610                         }
2611
2612                         is_itemf = true;
2613                         eitems = DummyRNA_NULL_items;
2614                 }
2615                 else {
2616                         if (!(items_fast = PySequence_Fast(items, "EnumProperty(...): "
2617                                                            "expected a sequence of tuples for the enum items or a function")))
2618                         {
2619                                 return NULL;
2620                         }
2621
2622                         eitems = enum_items_from_py(items_fast, def, &defvalue,
2623                                                     (opts & PROP_ENUM_FLAG) != 0);
2624
2625                         if (!eitems) {
2626                                 Py_DECREF(items_fast);
2627                                 return NULL;
2628                         }
2629                 }
2630
2631                 if (opts & PROP_ENUM_FLAG)  prop = RNA_def_enum_flag(srna, id, eitems, defvalue, name ? name : id, description);
2632                 else                        prop = RNA_def_enum(srna, id, eitems, defvalue, name ? name : id, description);
2633
2634                 if (pyopts) {
2635                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
2636                         if ((opts & PROP_ANIMATABLE) == 0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
2637                         if (opts & PROP_SKIP_SAVE) RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2638                         if (opts & PROP_LIB_EXCEPTION) RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
2639                 }
2640                 bpy_prop_callback_assign_update(prop, update_cb);
2641                 bpy_prop_callback_assign_enum(prop, get_cb, set_cb, (is_itemf ? items : NULL));
2642                 RNA_def_property_duplicate_pointers(srna, prop);
2643
2644                 if (is_itemf == false) {
2645                         /* note: this must be postponed until after #RNA_def_property_duplicate_pointers
2646                          * otherwise if this is a generator it may free the strings before we copy them */
2647                         Py_DECREF(items_fast);
2648
2649                         MEM_freeN(eitems);
2650                 }
2651         }
2652         Py_RETURN_NONE;
2653 }
2654
2655 static StructRNA *pointer_type_from_py(PyObject *value, const char *error_prefix)
2656 {
2657         StructRNA *srna;
2658
2659         srna = srna_from_self(value, "");
2660         if (!srna) {
2661                 if (PyErr_Occurred()) {
2662                         PyObject *msg = PyC_ExceptionBuffer();
2663                         const char *msg_char = _PyUnicode_AsString(msg);
2664                         PyErr_Format(PyExc_TypeError,
2665                                      "%.200s expected an RNA type derived from PropertyGroup, failed with: %s",
2666                                      error_prefix, msg_char);
2667                         Py_DECREF(msg);
2668                 }
2669                 else {
2670                         PyErr_Format(PyExc_TypeError,
2671                                      "%.200s expected an RNA type derived from PropertyGroup, failed with type '%s'",
2672                                      error_prefix, Py_TYPE(value)->tp_name);
2673                 }
2674                 return NULL;
2675         }
2676
2677         if (!RNA_struct_is_a(srna, &RNA_PropertyGroup)) {
2678                 PyErr_Format(PyExc_TypeError,
2679                              "%.200s expected an RNA type derived from PropertyGroup",
2680                              error_prefix);
2681                 return NULL;
2682         }
2683
2684         return srna;
2685 }
2686
2687 PyDoc_STRVAR(BPy_PointerProperty_doc,
2688 ".. function:: PointerProperty(type=\"\", "
2689                               "description=\"\", "
2690                               "options={'ANIMATABLE'}, "
2691                               "update=None)\n"
2692 "\n"
2693 "   Returns a new pointer property definition.\n"
2694 "\n"
2695 "   :arg type: A subclass of :class:`bpy.types.PropertyGroup`.\n"
2696 "   :type type: class\n"
2697 BPY_PROPDEF_NAME_DOC
2698 BPY_PROPDEF_DESC_DOC
2699 BPY_PROPDEF_OPTIONS_DOC
2700 BPY_PROPDEF_UPDATE_DOC
2701 );
2702 static PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw)
2703 {
2704         StructRNA *srna;
2705
2706         BPY_PROPDEF_HEAD(PointerProperty);
2707
2708         if (srna) {
2709                 static const char *kwlist[] = {"attr", "type", "name", "description", "options", "update", NULL};
2710                 const char *id = NULL, *name = NULL, *description = "";
2711                 int id_len;
2712                 PropertyRNA *prop;
2713                 StructRNA *ptype;
2714                 PyObject *type = Py_None;
2715                 PyObject *pyopts = NULL;
2716                 int opts = 0;
2717                 PyObject *update_cb = NULL;
2718
2719                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2720                                                  "s#O|ssO!O:PointerProperty",
2721                                                  (char **)kwlist, &id, &id_len,
2722                                                  &type, &name, &description,
2723                                                  &PySet_Type, &pyopts,
2724                                                  &update_cb))
2725                 {
2726                         return NULL;
2727                 }
2728
2729                 BPY_PROPDEF_CHECK(PointerProperty, property_flag_items);
2730
2731                 ptype = pointer_type_from_py(type, "PointerProperty(...):");
2732                 if (!ptype)
2733                         return NULL;
2734
2735                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2736                         return NULL;
2737                 }
2738
2739                 prop = RNA_def_pointer_runtime(srna, id, ptype, name ? name : id, description);
2740                 if (pyopts) {
2741                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
2742                         if ((opts & PROP_ANIMATABLE) == 0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
2743                         if (opts & PROP_SKIP_SAVE) RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2744                         if (opts & PROP_LIB_EXCEPTION) RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
2745                 }
2746                 bpy_prop_callback_assign_update(prop, update_cb);
2747                 RNA_def_property_duplicate_pointers(srna, prop);
2748         }
2749         Py_RETURN_NONE;
2750 }
2751
2752 PyDoc_STRVAR(BPy_CollectionProperty_doc,
2753 ".. function:: CollectionProperty(items, "
2754                                  "type=\"\", "
2755                                  "description=\"\", "
2756                                  "options={'ANIMATABLE'})\n"
2757 "\n"
2758 "   Returns a new collection property definition.\n"
2759 "\n"
2760 "   :arg type: A subclass of :class:`bpy.types.PropertyGroup`.\n"
2761 "   :type type: class\n"
2762 BPY_PROPDEF_NAME_DOC
2763 BPY_PROPDEF_DESC_DOC
2764 BPY_PROPDEF_OPTIONS_DOC
2765 );
2766 static PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
2767 {
2768         StructRNA *srna;
2769
2770         BPY_PROPDEF_HEAD(CollectionProperty);
2771
2772         if (srna) {
2773                 static const char *kwlist[] = {"attr", "type", "name", "description", "options", NULL};
2774                 const char *id = NULL, *name = NULL, *description = "";
2775                 int id_len;
2776                 PropertyRNA *prop;
2777                 StructRNA *ptype;
2778                 PyObject *type = Py_None;
2779                 PyObject *pyopts = NULL;
2780                 int opts = 0;
2781
2782                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2783                                                  "s#O|ssO!:CollectionProperty",
2784                                                  (char **)kwlist, &id, &id_len,
2785                                                  &type, &name, &description,
2786                                                  &PySet_Type, &pyopts))
2787                 {
2788                         return NULL;
2789                 }
2790
2791                 BPY_PROPDEF_CHECK(CollectionProperty, property_flag_items);
2792
2793                 ptype = pointer_type_from_py(type, "CollectionProperty(...):");
2794                 if (!ptype)
2795                         return NULL;
2796
2797                 prop = RNA_def_collection_runtime(srna, id, ptype, name ? name : id, description);
2798                 if (pyopts) {
2799                         if (opts & PROP_HIDDEN) RNA_def_property_flag(prop, PROP_HIDDEN);
2800                         if ((opts & PROP_ANIMATABLE) == 0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
2801                         if (opts & PROP_SKIP_SAVE) RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2802                         if (opts & PROP_LIB_EXCEPTION) RNA_def_property_flag(prop, PROP_LIB_EXCEPTION);
2803                 }
2804                 RNA_def_property_duplicate_pointers(srna, prop);
2805         }
2806         Py_RETURN_NONE;
2807 }
2808
2809 PyDoc_STRVAR(BPy_RemoveProperty_doc,
2810 ".. function:: RemoveProperty(cls, attr="")\n"
2811 "\n"
2812 "   Removes a dynamically defined property.\n"
2813 "\n"
2814 "   :arg cls: The class containing the property (must be a positional argument).\n"
2815 "   :type cls: type\n"
2816 "   :arg attr: Property name (must be passed as a keyword).\n"
2817 "   :type attr: string\n"
2818 "\n"
2819 ".. note:: Typically this function doesn't need to be accessed directly.\n"
2820 "   Instead use ``del cls.attr``\n"
2821 );
2822 static PyObject *BPy_RemoveProperty(PyObject *self, PyObject *args, PyObject *kw)
2823 {
2824         StructRNA *srna;
2825
2826         if (PyTuple_GET_SIZE(args) == 1) {
2827                 PyObject *ret;
2828                 self = PyTuple_GET_ITEM(args, 0);
2829                 args = PyTuple_New(0);
2830                 ret = BPy_RemoveProperty(self, args, kw);
2831                 Py_DECREF(args);
2832                 return ret;
2833         }
2834         else if (PyTuple_GET_SIZE(args) > 1) {
2835                 PyErr_SetString(PyExc_ValueError, "expected one positional arg, one keyword arg");
2836                 return NULL;
2837         }
2838
2839         srna = srna_from_self(self, "RemoveProperty(...):");
2840         if (srna == NULL && PyErr_Occurred()) {
2841                 return NULL; /* self's type was compatible but error getting the srna */
2842         }
2843         else if (srna == NULL) {
2844                 PyErr_SetString(PyExc_TypeError, "RemoveProperty(): struct rna not available for this type");
2845                 return NULL;
2846         }
2847         else {
2848                 static const char *kwlist[] = {"attr", NULL};
2849                 
2850                 char *id = NULL;
2851
2852                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2853                                                  "s:RemoveProperty",
2854                                                  (char **)kwlist, &id))
2855                 {
2856                         return NULL;
2857                 }
2858
2859                 if (RNA_def_property_free_identifier(srna, id) != 1) {
2860                         PyErr_Format(PyExc_TypeError, "RemoveProperty(): '%s' not a defined dynamic property", id);
2861                         return NULL;
2862                 }
2863         }
2864         Py_RETURN_NONE;
2865 }
2866
2867 static struct PyMethodDef props_methods[] = {
2868         {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS | METH_KEYWORDS, BPy_BoolProperty_doc},
2869         {"BoolVectorProperty", (PyCFunction)BPy_BoolVectorProperty, METH_VARARGS | METH_KEYWORDS, BPy_BoolVectorProperty_doc},
2870         {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS | METH_KEYWORDS, BPy_IntProperty_doc},
2871         {"IntVectorProperty", (PyCFunction)BPy_IntVectorProperty, METH_VARARGS | METH_KEYWORDS, BPy_IntVectorProperty_doc},
2872         {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS | METH_KEYWORDS, BPy_FloatProperty_doc},
2873         {"FloatVectorProperty", (PyCFunction)BPy_FloatVectorProperty, METH_VARARGS | METH_KEYWORDS, BPy_FloatVectorProperty_doc},
2874         {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS | METH_KEYWORDS, BPy_StringProperty_doc},
2875         {"EnumProperty", (PyCFunction)BPy_EnumProperty, METH_VARARGS | METH_KEYWORDS, BPy_EnumProperty_doc},
2876         {"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS | METH_KEYWORDS, BPy_PointerProperty_doc},
2877         {"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS | METH_KEYWORDS, BPy_CollectionProperty_doc},
2878
2879         {"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS | METH_KEYWORDS, BPy_RemoveProperty_doc},
2880         {NULL, NULL, 0, NULL}
2881 };
2882
2883 static struct PyModuleDef props_module = {
2884         PyModuleDef_HEAD_INIT,
2885         "bpy.props",
2886         "This module defines properties to extend blenders internal data, the result of these functions"
2887         " is used to assign properties to classes registered with blender and can't be used directly.",
2888         -1, /* multiple "initialization" just copies the module dict. */
2889         props_methods,
2890         NULL, NULL, NULL, NULL
2891 };
2892
2893 PyObject *BPY_rna_props(void)
2894 {
2895         PyObject *submodule;
2896         PyObject *submodule_dict;
2897         
2898         submodule = PyModule_Create(&props_module);
2899         PyDict_SetItemString(PyImport_GetModuleDict(), props_module.m_name, submodule);
2900
2901         /* INCREF since its its assumed that all these functions return the
2902          * module with a new ref like PyDict_New, since they are passed to
2903          * PyModule_AddObject which steals a ref */
2904         Py_INCREF(submodule);
2905         
2906         /* api needs the PyObjects internally */
2907         submodule_dict = PyModule_GetDict(submodule);
2908
2909 #define ASSIGN_STATIC(_name) pymeth_##_name = PyDict_GetItemString(submodule_dict, #_name)
2910
2911         ASSIGN_STATIC(BoolProperty);
2912         ASSIGN_STATIC(BoolVectorProperty);
2913         ASSIGN_STATIC(IntProperty);
2914         ASSIGN_STATIC(IntVectorProperty);
2915         ASSIGN_STATIC(FloatProperty);
2916         ASSIGN_STATIC(FloatVectorProperty);
2917         ASSIGN_STATIC(StringProperty);
2918         ASSIGN_STATIC(EnumProperty);
2919         ASSIGN_STATIC(PointerProperty);
2920         ASSIGN_STATIC(CollectionProperty);
2921         ASSIGN_STATIC(RemoveProperty);
2922         
2923         return submodule;
2924 }