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