f7348ac22501445ef234c1db3465148cf5f7341d
[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 rna_enum_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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = rna_enum_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 bool is_enum_flag)
1318 {
1319         EnumPropertyItem *items;
1320         PyObject *item;
1321         const Py_ssize_t seq_len = PySequence_Fast_GET_SIZE(seq_fast);
1322         PyObject **seq_fast_items = PySequence_Fast_ITEMS(seq_fast);
1323         Py_ssize_t totbuf = 0;
1324         int i;
1325         short def_used = 0;
1326         const char *def_cmp = NULL;
1327
1328         if (is_enum_flag) {
1329                 if (seq_len > RNA_ENUM_BITFLAG_SIZE) {
1330                         PyErr_SetString(PyExc_TypeError,
1331                                         "EnumProperty(...): maximum "
1332                                         STRINGIFY(RNA_ENUM_BITFLAG_SIZE)
1333                                         " members for a ENUM_FLAG type property");
1334                         return NULL;
1335                 }
1336                 if (def && !PySet_Check(def)) {
1337                         PyErr_Format(PyExc_TypeError,
1338                                      "EnumProperty(...): default option must be a 'set' "
1339                                      "type when ENUM_FLAG is enabled, not a '%.200s'",
1340                                      Py_TYPE(def)->tp_name);
1341                         return NULL;
1342                 }
1343         }
1344         else {
1345                 if (def) {
1346                         def_cmp = _PyUnicode_AsString(def);
1347                         if (def_cmp == NULL) {
1348                                 PyErr_Format(PyExc_TypeError,
1349                                              "EnumProperty(...): default option must be a 'str' "
1350                                              "type when ENUM_FLAG is disabled, not a '%.200s'",
1351                                              Py_TYPE(def)->tp_name);
1352                                 return NULL;
1353                         }
1354                 }
1355         }
1356
1357         /* blank value */
1358         *defvalue = 0;
1359
1360         items = MEM_callocN(sizeof(EnumPropertyItem) * (seq_len + 1), "enum_items_from_py1");
1361
1362         for (i = 0; i < seq_len; i++) {
1363                 EnumPropertyItem tmp = {0, "", 0, "", ""};
1364                 const char *tmp_icon = NULL;
1365                 Py_ssize_t item_size;
1366                 Py_ssize_t id_str_size;
1367                 Py_ssize_t name_str_size;
1368                 Py_ssize_t desc_str_size;
1369
1370                 item = seq_fast_items[i];
1371
1372                 if ((PyTuple_CheckExact(item)) &&
1373                     (item_size = PyTuple_GET_SIZE(item)) &&
1374                     (item_size >= 3 && item_size <= 5) &&
1375                     (tmp.identifier =  _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 0), &id_str_size)) &&
1376                     (tmp.name =        _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 1), &name_str_size)) &&
1377                     (tmp.description = _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 2), &desc_str_size)) &&
1378                     /* TODO, number isn't ensured to be unique from the script author */
1379                     (item_size != 4 || py_long_as_int(PyTuple_GET_ITEM(item, 3), &tmp.value) != -1) &&
1380                     (item_size != 5 || ((py_long_as_int(PyTuple_GET_ITEM(item, 3), &tmp.icon) != -1 ||
1381                                          (tmp_icon = _PyUnicode_AsString(PyTuple_GET_ITEM(item, 3)))) &&
1382                                         py_long_as_int(PyTuple_GET_ITEM(item, 4), &tmp.value) != -1)))
1383                 {
1384                         if (is_enum_flag) {
1385                                 if (item_size < 4) {
1386                                         tmp.value = 1 << i;
1387                                 }
1388
1389                                 if (def && PySet_Contains(def, PyTuple_GET_ITEM(item, 0))) {
1390                                         *defvalue |= tmp.value;
1391                                         def_used++;
1392                                 }
1393                         }
1394                         else {
1395                                 if (item_size < 4) {
1396                                         tmp.value = i;
1397                                 }
1398
1399                                 if (def && def_used == 0 && STREQ(def_cmp, tmp.identifier)) {
1400                                         *defvalue = tmp.value;
1401                                         def_used++; /* only ever 1 */
1402                                 }
1403                         }
1404
1405                         if (tmp_icon)
1406                                 tmp.icon = icon_id_from_name(tmp_icon);
1407
1408                         items[i] = tmp;
1409
1410                         /* calculate combine string length */
1411                         totbuf += id_str_size + name_str_size + desc_str_size + 3; /* 3 is for '\0's */
1412                 }
1413                 else {
1414                         MEM_freeN(items);
1415                         PyErr_SetString(PyExc_TypeError,
1416                                         "EnumProperty(...): expected a tuple containing "
1417                                         "(identifier, name, description) and optionally an "
1418                                         "icon name and unique number");
1419                         return NULL;
1420                 }
1421
1422         }
1423
1424         if (is_enum_flag) {
1425                 /* strict check that all set members were used */
1426                 if (def && def_used != PySet_GET_SIZE(def)) {
1427                         MEM_freeN(items);
1428
1429                         PyErr_Format(PyExc_TypeError,
1430                                      "EnumProperty(..., default={...}): set has %d unused member(s)",
1431                                      PySet_GET_SIZE(def) - def_used);
1432                         return NULL;
1433                 }
1434         }
1435         else {
1436                 if (def && def_used == 0) {
1437                         MEM_freeN(items);
1438
1439                         PyErr_Format(PyExc_TypeError,
1440                                      "EnumProperty(..., default=\'%s\'): not found in enum members",
1441                                      def_cmp);
1442                         return NULL;
1443                 }
1444         }
1445
1446         /* disabled duplicating strings because the array can still be freed and
1447          * the strings from it referenced, for now we can't support dynamically
1448          * created strings from python. */
1449 #if 0
1450         /* this would all work perfectly _but_ the python strings may be freed
1451          * immediately after use, so we need to duplicate them, ugh.
1452          * annoying because it works most of the time without this. */
1453         {
1454                 EnumPropertyItem *items_dup = MEM_mallocN((sizeof(EnumPropertyItem) * (seq_len + 1)) + (sizeof(char) * totbuf),
1455                                                           "enum_items_from_py2");
1456                 EnumPropertyItem *items_ptr = items_dup;
1457                 char *buf = ((char *)items_dup) + (sizeof(EnumPropertyItem) * (seq_len + 1));
1458                 memcpy(items_dup, items, sizeof(EnumPropertyItem) * (seq_len + 1));
1459                 for (i = 0; i < seq_len; i++, items_ptr++) {
1460                         buf += strswapbufcpy(buf, &items_ptr->identifier);
1461                         buf += strswapbufcpy(buf, &items_ptr->name);
1462                         buf += strswapbufcpy(buf, &items_ptr->description);
1463                 }
1464                 MEM_freeN(items);
1465                 items = items_dup;
1466         }
1467         /* end string duplication */
1468 #endif
1469
1470         return items;
1471 }
1472
1473 static EnumPropertyItem *bpy_prop_enum_itemf_cb(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, bool *r_free)
1474 {
1475         PyGILState_STATE gilstate;
1476
1477         PyObject *py_func = RNA_property_enum_py_data_get(prop);
1478         PyObject *self = NULL;
1479         PyObject *args;
1480         PyObject *items; /* returned from the function call */
1481
1482         EnumPropertyItem *eitems = NULL;
1483         int err = 0;
1484
1485         if (C) {
1486                 bpy_context_set(C, &gilstate);
1487         }
1488         else {
1489                 gilstate = PyGILState_Ensure();
1490         }
1491
1492         args = PyTuple_New(2);
1493         self = pyrna_struct_as_instance(ptr);
1494         PyTuple_SET_ITEM(args, 0, self);
1495
1496         /* now get the context */
1497         if (C) {
1498                 PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
1499                 Py_INCREF(bpy_context_module);
1500         }
1501         else {
1502                 PyTuple_SET_ITEM(args, 1, Py_None);
1503                 Py_INCREF(Py_None);
1504         }
1505
1506         items = PyObject_CallObject(py_func, args);
1507
1508         Py_DECREF(args);
1509
1510         if (items == NULL) {
1511                 err = -1;
1512         }
1513         else {
1514                 PyObject *items_fast;
1515                 int defvalue_dummy = 0;
1516
1517                 if (!(items_fast = PySequence_Fast(items, "EnumProperty(...): "
1518                                                    "return value from the callback was not a sequence")))
1519                 {
1520                         err = -1;
1521                 }
1522                 else {
1523                         eitems = enum_items_from_py(items_fast, NULL, &defvalue_dummy,
1524                                                     (RNA_property_flag(prop) & PROP_ENUM_FLAG) != 0);
1525
1526                         Py_DECREF(items_fast);
1527
1528                         if (!eitems) {
1529                                 err = -1;
1530                         }
1531                 }
1532
1533                 Py_DECREF(items);
1534         }
1535
1536         if (err != -1) { /* worked */
1537                 *r_free = true;
1538         }
1539         else {
1540                 printf_func_error(py_func);
1541
1542                 eitems = DummyRNA_NULL_items;
1543         }
1544
1545         if (C) {
1546                 bpy_context_clear(C, &gilstate);
1547         }
1548         else {
1549                 PyGILState_Release(gilstate);
1550         }
1551
1552         return eitems;
1553 }
1554
1555 static int bpy_prop_callback_check(PyObject *py_func, const char *keyword, int argcount)
1556 {
1557         if (py_func && py_func != Py_None) {
1558                 if (!PyFunction_Check(py_func)) {
1559                         PyErr_Format(PyExc_TypeError,
1560                                      "%s keyword: expected a function type, not a %.200s",
1561                                      keyword, Py_TYPE(py_func)->tp_name);
1562                         return -1;
1563                 }
1564                 else {
1565                         PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(py_func);
1566                         if (f_code->co_argcount != argcount) {
1567                                 PyErr_Format(PyExc_TypeError,
1568                                              "%s keyword: expected a function taking %d arguments, not %d",
1569                                              keyword, argcount, f_code->co_argcount);
1570                                 return -1;
1571                         }
1572                 }
1573         }
1574
1575         return 0;
1576 }
1577
1578 static PyObject **bpy_prop_py_data_get(struct PropertyRNA *prop)
1579 {
1580         PyObject **py_data = RNA_property_py_data_get(prop);
1581         if (!py_data) {
1582                 py_data = MEM_callocN(sizeof(PyObject *) * BPY_DATA_CB_SLOT_SIZE, __func__);
1583                 RNA_def_py_data(prop, py_data);
1584         }
1585         return py_data;
1586 }
1587
1588 static void bpy_prop_callback_assign_update(struct PropertyRNA *prop, PyObject *update_cb)
1589 {
1590         /* assume this is already checked for type and arg length */
1591         if (update_cb && update_cb != Py_None) {
1592                 PyObject **py_data = bpy_prop_py_data_get(prop);
1593
1594                 RNA_def_property_update_runtime(prop, (void *)bpy_prop_update_cb);
1595                 py_data[BPY_DATA_CB_SLOT_UPDATE] = update_cb;
1596
1597                 RNA_def_property_flag(prop, PROP_CONTEXT_PROPERTY_UPDATE);
1598         }
1599 }
1600
1601 static void bpy_prop_callback_assign_boolean(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1602 {
1603         BooleanPropertyGetFunc rna_get_cb = NULL;
1604         BooleanPropertySetFunc rna_set_cb = NULL;
1605         
1606         if (get_cb && get_cb != Py_None) {
1607                 PyObject **py_data = bpy_prop_py_data_get(prop);
1608
1609                 rna_get_cb = bpy_prop_boolean_get_cb;
1610                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1611         }
1612
1613         if (set_cb && set_cb != Py_None) {
1614                 PyObject **py_data = bpy_prop_py_data_get(prop);
1615
1616                 rna_set_cb = bpy_prop_boolean_set_cb;
1617                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1618         }
1619
1620         RNA_def_property_boolean_funcs_runtime(prop, rna_get_cb, rna_set_cb);
1621 }
1622
1623 static void bpy_prop_callback_assign_boolean_array(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1624 {
1625         BooleanArrayPropertyGetFunc rna_get_cb = NULL;
1626         BooleanArrayPropertySetFunc rna_set_cb = NULL;
1627         
1628         if (get_cb && get_cb != Py_None) {
1629                 PyObject **py_data = bpy_prop_py_data_get(prop);
1630
1631                 rna_get_cb = bpy_prop_boolean_array_get_cb;
1632                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1633         }
1634
1635         if (set_cb && set_cb != Py_None) {
1636                 PyObject **py_data = bpy_prop_py_data_get(prop);
1637
1638                 rna_set_cb = bpy_prop_boolean_array_set_cb;
1639                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1640         }
1641
1642         RNA_def_property_boolean_array_funcs_runtime(prop, rna_get_cb, rna_set_cb);
1643 }
1644
1645 static void bpy_prop_callback_assign_int(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1646 {
1647         IntPropertyGetFunc rna_get_cb = NULL;
1648         IntPropertySetFunc rna_set_cb = NULL;
1649         
1650         if (get_cb && get_cb != Py_None) {
1651                 PyObject **py_data = bpy_prop_py_data_get(prop);
1652
1653                 rna_get_cb = bpy_prop_int_get_cb;
1654                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1655         }
1656
1657         if (set_cb && set_cb != Py_None) {
1658                 PyObject **py_data = bpy_prop_py_data_get(prop);
1659
1660                 rna_set_cb = bpy_prop_int_set_cb;
1661                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1662         }
1663
1664         RNA_def_property_int_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1665 }
1666
1667 static void bpy_prop_callback_assign_int_array(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1668 {
1669         IntArrayPropertyGetFunc rna_get_cb = NULL;
1670         IntArrayPropertySetFunc rna_set_cb = NULL;
1671         
1672         if (get_cb && get_cb != Py_None) {
1673                 PyObject **py_data = bpy_prop_py_data_get(prop);
1674
1675                 rna_get_cb = bpy_prop_int_array_get_cb;
1676                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1677         }
1678
1679         if (set_cb && set_cb != Py_None) {
1680                 PyObject **py_data = bpy_prop_py_data_get(prop);
1681
1682                 rna_set_cb = bpy_prop_int_array_set_cb;
1683                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1684         }
1685
1686         RNA_def_property_int_array_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1687 }
1688
1689 static void bpy_prop_callback_assign_float(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1690 {
1691         FloatPropertyGetFunc rna_get_cb = NULL;
1692         FloatPropertySetFunc rna_set_cb = NULL;
1693         
1694         if (get_cb && get_cb != Py_None) {
1695                 PyObject **py_data = bpy_prop_py_data_get(prop);
1696
1697                 rna_get_cb = bpy_prop_float_get_cb;
1698                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1699         }
1700
1701         if (set_cb && set_cb != Py_None) {
1702                 PyObject **py_data = bpy_prop_py_data_get(prop);
1703
1704                 rna_set_cb = bpy_prop_float_set_cb;
1705                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1706         }
1707
1708         RNA_def_property_float_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1709 }
1710
1711 static void bpy_prop_callback_assign_float_array(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1712 {
1713         FloatArrayPropertyGetFunc rna_get_cb = NULL;
1714         FloatArrayPropertySetFunc rna_set_cb = NULL;
1715         
1716         if (get_cb && get_cb != Py_None) {
1717                 PyObject **py_data = bpy_prop_py_data_get(prop);
1718
1719                 rna_get_cb = bpy_prop_float_array_get_cb;
1720                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1721         }
1722
1723         if (set_cb && set_cb != Py_None) {
1724                 PyObject **py_data = bpy_prop_py_data_get(prop);
1725
1726                 rna_set_cb = bpy_prop_float_array_set_cb;
1727                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1728         }
1729
1730         RNA_def_property_float_array_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1731 }
1732
1733 static void bpy_prop_callback_assign_string(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1734 {
1735         StringPropertyGetFunc rna_get_cb = NULL;
1736         StringPropertyLengthFunc rna_length_cb = NULL;
1737         StringPropertySetFunc rna_set_cb = NULL;
1738         
1739         if (get_cb && get_cb != Py_None) {
1740                 PyObject **py_data = bpy_prop_py_data_get(prop);
1741
1742                 rna_get_cb = bpy_prop_string_get_cb;
1743                 rna_length_cb = bpy_prop_string_length_cb;
1744                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1745         }
1746
1747         if (set_cb && set_cb != Py_None) {
1748                 PyObject **py_data = bpy_prop_py_data_get(prop);
1749
1750                 rna_set_cb = bpy_prop_string_set_cb;
1751                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1752         }
1753
1754         RNA_def_property_string_funcs_runtime(prop, rna_get_cb, rna_length_cb, rna_set_cb);
1755 }
1756
1757 static void bpy_prop_callback_assign_enum(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb, PyObject *itemf_cb)
1758 {
1759         EnumPropertyGetFunc rna_get_cb = NULL;
1760         EnumPropertyItemFunc rna_itemf_cb = NULL;
1761         EnumPropertySetFunc rna_set_cb = NULL;
1762         
1763         if (get_cb && get_cb != Py_None) {
1764                 PyObject **py_data = bpy_prop_py_data_get(prop);
1765
1766                 rna_get_cb = bpy_prop_enum_get_cb;
1767                 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1768         }
1769
1770         if (set_cb && set_cb != Py_None) {
1771                 PyObject **py_data = bpy_prop_py_data_get(prop);
1772
1773                 rna_set_cb = bpy_prop_enum_set_cb;
1774                 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1775         }
1776
1777         if (itemf_cb && itemf_cb != Py_None) {
1778                 rna_itemf_cb = bpy_prop_enum_itemf_cb;
1779                 RNA_def_property_enum_py_data(prop, (void *)itemf_cb);
1780
1781                 /* watch out!, if a user is tricky they can probably crash blender
1782                  * if they manage to free the callback, take care! */
1783                 /* Py_INCREF(itemf_cb); */
1784         }
1785
1786         RNA_def_property_enum_funcs_runtime(prop, rna_get_cb, rna_set_cb, rna_itemf_cb);
1787 }
1788
1789 /* this define runs at the start of each function and deals with 
1790  * returning a deferred property (to be registered later) */
1791 #define BPY_PROPDEF_HEAD(_func)                                               \
1792         if (PyTuple_GET_SIZE(args) == 1) {                                        \
1793                 PyObject *ret;                                                        \
1794                 self = PyTuple_GET_ITEM(args, 0);                                     \
1795                 args = PyTuple_New(0);                                                \
1796                 ret = BPy_##_func(self, args, kw);                                    \
1797                 Py_DECREF(args);                                                      \
1798                 return ret;                                                           \
1799         }                                                                         \
1800         else if (PyTuple_GET_SIZE(args) > 1) {                                    \
1801                 PyErr_SetString(PyExc_ValueError, "all args must be keywords");       \
1802                 return NULL;                                                          \
1803         }                                                                         \
1804         srna = srna_from_self(self, #_func"(...):");                              \
1805         if (srna == NULL) {                                                       \
1806                 if (PyErr_Occurred())                                                 \
1807                         return NULL;                                                      \
1808                 return bpy_prop_deferred_return(pymeth_##_func, kw);                  \
1809         } (void)0
1810
1811 /* terse macros for error checks shared between all funcs cant use function
1812  * calls because of static strings passed to pyrna_set_to_enum_bitfield */
1813 #define BPY_PROPDEF_CHECK(_func, _property_flag_items)                        \
1814         if (UNLIKELY(id_len >= MAX_IDPROP_NAME)) {                                \
1815                 PyErr_Format(PyExc_TypeError,                                         \
1816                              #_func"(): '%.200s' too long, max length is %d",         \
1817                              id, MAX_IDPROP_NAME - 1);                                \
1818                 return NULL;                                                          \
1819         }                                                                         \
1820         if (UNLIKELY(RNA_def_property_free_identifier(srna, id) == -1)) {         \
1821                 PyErr_Format(PyExc_TypeError,                                         \
1822                              #_func"(): '%s' is defined as a non-dynamic type",       \
1823                              id);                                                     \
1824                 return NULL;                                                          \
1825         }                                                                         \
1826         if (UNLIKELY(pyopts && pyrna_set_to_enum_bitfield(_property_flag_items,   \
1827                                                  pyopts,                          \
1828                                                  &opts,                           \
1829                                                  #_func"(options={ ...}):")))     \
1830         {                                                                         \
1831                 return NULL;                                                          \
1832         } (void)0
1833
1834 #define BPY_PROPDEF_SUBTYPE_CHECK(_func, _property_flag_items, _subtype)      \
1835         BPY_PROPDEF_CHECK(_func, _property_flag_items);                           \
1836         if (UNLIKELY(pysubtype && RNA_enum_value_from_id(_subtype,                \
1837                                                 pysubtype,                        \
1838                                                 &subtype) == 0))                  \
1839         {                                                                         \
1840                 const char *enum_str = BPy_enum_as_string(_subtype);                  \
1841                 PyErr_Format(PyExc_TypeError,                                         \
1842                              #_func"(subtype='%s'): "                                 \
1843                              "subtype not found in (%s)",                             \
1844                              pysubtype, enum_str);                                    \
1845                 MEM_freeN((void *)enum_str);                                          \
1846                 return NULL;                                                          \
1847         } (void)0
1848
1849
1850 #define BPY_PROPDEF_NAME_DOC \
1851 "   :arg name: Name used in the user interface.\n" \
1852 "   :type name: string\n" \
1853
1854 #define BPY_PROPDEF_DESC_DOC \
1855 "   :arg description: Text used for the tooltip and api documentation.\n" \
1856 "   :type description: string\n" \
1857
1858 #define BPY_PROPDEF_UNIT_DOC \
1859 "   :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION'].\n" \
1860 "   :type unit: string\n"       \
1861
1862 #define BPY_PROPDEF_NUM_MIN_DOC \
1863 "   :arg min: Hard minimum, trying to assign a value below will silently assign this minimum instead.\n" \
1864
1865 #define BPY_PROPDEF_NUM_MAX_DOC \
1866 "   :arg max: Hard maximum, trying to assign a value above will silently assign this maximum instead.\n" \
1867
1868 #define BPY_PROPDEF_NUM_SOFTMIN_DOC \
1869 "   :arg soft_min: Soft minimum (>= *min*), user won't be able to drag the widget below this value in the UI.\n" \
1870
1871 #define BPY_PROPDEF_NUM_SOFTMAX_DOC \
1872 "   :arg soft_max: Soft maximum (<= *max*), user won't be able to drag the widget above this value in the UI.\n" \
1873
1874 #define BPY_PROPDEF_VECSIZE_DOC \
1875 "   :arg size: Vector dimensions in [1, " STRINGIFY(PYRNA_STACK_ARRAY) "].\n" \
1876 "   :type size: int\n" \
1877
1878 #define BPY_PROPDEF_INT_STEP_DOC \
1879 "   :arg step: Step of increment/decrement in UI, in [1, 100], defaults to 1 (WARNING: unused currently!).\n" \
1880 "   :type step: int\n" \
1881
1882 #define BPY_PROPDEF_FLOAT_STEP_DOC \
1883 "   :arg step: Step of increment/decrement in UI, in [1, 100], defaults to 3 (WARNING: actual value is /100).\n" \
1884 "   :type step: int\n" \
1885
1886 #define BPY_PROPDEF_FLOAT_PREC_DOC \
1887 "   :arg precision: Maximum number of decimal digits to display, in [0, 6].\n" \
1888 "   :type precision: int\n" \
1889
1890 #define BPY_PROPDEF_UPDATE_DOC \
1891 "   :arg update: Function to be called when this value is modified,\n" \
1892 "      This function must take 2 values (self, context) and return None.\n" \
1893 "      *Warning* there are no safety checks to avoid infinite recursion.\n" \
1894 "   :type update: function\n" \
1895
1896 #define BPY_PROPDEF_GET_DOC \
1897 "   :arg get: Function to be called when this value is 'read',\n" \
1898 "      This function must take 1 value (self) and return the value of the property.\n" \
1899 "   :type get: function\n" \
1900
1901 #define BPY_PROPDEF_SET_DOC \
1902 "   :arg set: Function to be called when this value is 'written',\n" \
1903 "      This function must take 2 values (self, value) and return None.\n" \
1904 "   :type set: function\n" \
1905
1906 #define BPY_PROPDEF_TYPE_DOC \
1907 "   :arg type: A subclass of :class:`bpy.types.PropertyGroup`.\n" \
1908 "   :type type: class\n" \
1909
1910 #if 0
1911 static int bpy_struct_id_used(StructRNA *srna, char *identifier)
1912 {
1913         PointerRNA ptr;
1914         RNA_pointer_create(NULL, srna, NULL, &ptr);
1915         return (RNA_struct_find_property(&ptr, identifier) != NULL);
1916 }
1917 #endif
1918
1919
1920 /* Function that sets RNA, NOTE - self is NULL when called from python,
1921  * but being abused from C so we can pass the srna along.
1922  * This isn't incorrect since its a python object - but be careful */
1923 PyDoc_STRVAR(BPy_BoolProperty_doc,
1924 ".. function:: BoolProperty(name=\"\", "
1925                            "description=\"\", "
1926                            "default=False, "
1927                            "options={'ANIMATABLE'}, "
1928                            "subtype='NONE', "
1929                            "update=None, "
1930                            "get=None, "
1931                            "set=None)\n"
1932 "\n"
1933 "   Returns a new boolean property definition.\n"
1934 "\n"
1935 BPY_PROPDEF_NAME_DOC
1936 BPY_PROPDEF_DESC_DOC
1937 BPY_PROPDEF_OPTIONS_DOC
1938 BPY_PROPDEF_SUBTYPE_NUMBER_DOC
1939 BPY_PROPDEF_UPDATE_DOC
1940 BPY_PROPDEF_GET_DOC
1941 BPY_PROPDEF_SET_DOC
1942 );
1943 static PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
1944 {
1945         StructRNA *srna;
1946
1947         BPY_PROPDEF_HEAD(BoolProperty);
1948
1949         if (srna) {
1950                 static const char *kwlist[] = {"attr", "name", "description", "default",
1951                                                "options", "subtype", "update", "get", "set", NULL};
1952                 const char *id = NULL, *name = NULL, *description = "";
1953                 int id_len;
1954                 bool def = false;
1955                 PropertyRNA *prop;
1956                 PyObject *pyopts = NULL;
1957                 int opts = 0;
1958                 const char *pysubtype = NULL;
1959                 int subtype = PROP_NONE;
1960                 PyObject *update_cb = NULL;
1961                 PyObject *get_cb = NULL;
1962                 PyObject *set_cb = NULL;
1963
1964                 if (!PyArg_ParseTupleAndKeywords(args, kw,
1965                                                  "s#|ssO&O!sOOO:BoolProperty",
1966                                                  (char **)kwlist, &id, &id_len,
1967                                                  &name, &description, PyC_ParseBool, &def,
1968                                                  &PySet_Type, &pyopts, &pysubtype,
1969                                                  &update_cb, &get_cb, &set_cb))
1970                 {
1971                         return NULL;
1972                 }
1973
1974                 BPY_PROPDEF_SUBTYPE_CHECK(BoolProperty, property_flag_items, property_subtype_number_items);
1975
1976                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
1977                         return NULL;
1978                 }
1979                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
1980                         return NULL;
1981                 }
1982                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
1983                         return NULL;
1984                 }
1985
1986                 prop = RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
1987                 RNA_def_property_boolean_default(prop, def);
1988                 RNA_def_property_ui_text(prop, name ? name : id, description);
1989
1990                 if (pyopts) {
1991                         bpy_prop_assign_flag(prop, opts);
1992                 }
1993                 bpy_prop_callback_assign_update(prop, update_cb);
1994                 bpy_prop_callback_assign_boolean(prop, get_cb, set_cb);
1995                 RNA_def_property_duplicate_pointers(srna, prop);
1996         }
1997
1998         Py_RETURN_NONE;
1999 }
2000
2001 PyDoc_STRVAR(BPy_BoolVectorProperty_doc,
2002 ".. function:: BoolVectorProperty(name=\"\", "
2003                                  "description=\"\", "
2004                                  "default=(False, False, False), "
2005                                  "options={'ANIMATABLE'}, "
2006                                  "subtype='NONE', "
2007                                  "size=3, "
2008                                  "update=None, "
2009                                  "get=None, "
2010                                  "set=None)\n"
2011 "\n"
2012 "   Returns a new vector boolean property definition.\n"
2013 "\n"
2014 BPY_PROPDEF_NAME_DOC
2015 BPY_PROPDEF_DESC_DOC
2016 "   :arg default: sequence of booleans the length of *size*.\n"
2017 "   :type default: sequence\n"
2018 BPY_PROPDEF_OPTIONS_DOC
2019 BPY_PROPDEF_SUBTYPE_ARRAY_DOC
2020 BPY_PROPDEF_VECSIZE_DOC
2021 BPY_PROPDEF_UPDATE_DOC
2022 BPY_PROPDEF_GET_DOC
2023 BPY_PROPDEF_SET_DOC
2024 );
2025 static PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
2026 {
2027         StructRNA *srna;
2028
2029         BPY_PROPDEF_HEAD(BoolVectorProperty);
2030
2031         if (srna) {
2032                 static const char *kwlist[] = {"attr", "name", "description", "default",
2033                                                "options", "subtype", "size", "update", "get", "set", NULL};
2034                 const char *id = NULL, *name = NULL, *description = "";
2035                 int id_len;
2036                 int def[PYRNA_STACK_ARRAY] = {0};
2037                 int size = 3;
2038                 PropertyRNA *prop;
2039                 PyObject *pydef = NULL;
2040                 PyObject *pyopts = NULL;
2041                 int opts = 0;
2042                 const char *pysubtype = NULL;
2043                 int subtype = PROP_NONE;
2044                 PyObject *update_cb = NULL;
2045                 PyObject *get_cb = NULL;
2046                 PyObject *set_cb = NULL;
2047
2048                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2049                                                  "s#|ssOO!siOOO:BoolVectorProperty",
2050                                                  (char **)kwlist, &id, &id_len,
2051                                                  &name, &description, &pydef,
2052                                                  &PySet_Type, &pyopts, &pysubtype, &size,
2053                                                  &update_cb, &get_cb, &set_cb))
2054                 {
2055                         return NULL;
2056                 }
2057
2058                 BPY_PROPDEF_SUBTYPE_CHECK(BoolVectorProperty, property_flag_items, property_subtype_array_items);
2059
2060                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
2061                         PyErr_Format(PyExc_TypeError,
2062                                      "BoolVectorProperty(size=%d): size must be between 0 and "
2063                                      STRINGIFY(PYRNA_STACK_ARRAY), size);
2064                         return NULL;
2065                 }
2066
2067                 if (pydef && PyC_AsArray(def, pydef, size, &PyBool_Type, false, "BoolVectorProperty(default=sequence)") == -1)
2068                         return NULL;
2069
2070                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2071                         return NULL;
2072                 }
2073                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2074                         return NULL;
2075                 }
2076                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2077                         return NULL;
2078                 }
2079
2080                 // prop = RNA_def_boolean_array(srna, id, size, pydef ? def:NULL, name ? name : id, description);
2081                 prop = RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
2082                 RNA_def_property_array(prop, size);
2083                 if (pydef) RNA_def_property_boolean_array_default(prop, def);
2084                 RNA_def_property_ui_text(prop, name ? name : id, description);
2085
2086                 if (pyopts) {
2087                         bpy_prop_assign_flag(prop, opts);
2088                 }
2089                 bpy_prop_callback_assign_update(prop, update_cb);
2090                 bpy_prop_callback_assign_boolean_array(prop, get_cb, set_cb);
2091                 RNA_def_property_duplicate_pointers(srna, prop);
2092         }
2093         
2094         Py_RETURN_NONE;
2095 }
2096
2097 PyDoc_STRVAR(BPy_IntProperty_doc,
2098 ".. function:: IntProperty(name=\"\", "
2099                           "description=\"\", "
2100                           "default=0, "
2101                           "min=-2**31, max=2**31-1, "
2102                           "soft_min=-2**31, soft_max=2**31-1, "
2103                           "step=1, "
2104                           "options={'ANIMATABLE'}, "
2105                           "subtype='NONE', "
2106                           "update=None, "
2107                           "get=None, "
2108                           "set=None)\n"
2109 "\n"
2110 "   Returns a new int property definition.\n"
2111 "\n"
2112 BPY_PROPDEF_NAME_DOC
2113 BPY_PROPDEF_DESC_DOC
2114 BPY_PROPDEF_NUM_MIN_DOC
2115 "   :type min: int\n"
2116 BPY_PROPDEF_NUM_MAX_DOC
2117 "   :type max: int\n"
2118 BPY_PROPDEF_NUM_SOFTMAX_DOC
2119 "   :type soft_min: int\n"
2120 BPY_PROPDEF_NUM_SOFTMIN_DOC
2121 "   :type soft_max: int\n"
2122 BPY_PROPDEF_INT_STEP_DOC
2123 BPY_PROPDEF_OPTIONS_DOC
2124 BPY_PROPDEF_SUBTYPE_NUMBER_DOC
2125 BPY_PROPDEF_UPDATE_DOC
2126 BPY_PROPDEF_GET_DOC
2127 BPY_PROPDEF_SET_DOC
2128 );
2129 static PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
2130 {
2131         StructRNA *srna;
2132
2133         BPY_PROPDEF_HEAD(IntProperty);
2134
2135         if (srna) {
2136                 static const char *kwlist[] = {"attr", "name", "description", "default",
2137                                                "min", "max", "soft_min", "soft_max",
2138                                                "step", "options", "subtype", "update", "get", "set", NULL};
2139                 const char *id = NULL, *name = NULL, *description = "";
2140                 int id_len;
2141                 int min = INT_MIN, max = INT_MAX, soft_min = INT_MIN, soft_max = INT_MAX, step = 1, def = 0;
2142                 PropertyRNA *prop;
2143                 PyObject *pyopts = NULL;
2144                 int opts = 0;
2145                 const char *pysubtype = NULL;
2146                 int subtype = PROP_NONE;
2147                 PyObject *update_cb = NULL;
2148                 PyObject *get_cb = NULL;
2149                 PyObject *set_cb = NULL;
2150
2151                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2152                                                  "s#|ssiiiiiiO!sOOO:IntProperty",
2153                                                  (char **)kwlist, &id, &id_len,
2154                                                  &name, &description, &def,
2155                                                  &min, &max, &soft_min, &soft_max,
2156                                                  &step, &PySet_Type, &pyopts, &pysubtype,
2157                                                  &update_cb, &get_cb, &set_cb))
2158                 {
2159                         return NULL;
2160                 }
2161
2162                 BPY_PROPDEF_SUBTYPE_CHECK(IntProperty, property_flag_items, property_subtype_number_items);
2163
2164                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2165                         return NULL;
2166                 }
2167                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2168                         return NULL;
2169                 }
2170                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2171                         return NULL;
2172                 }
2173
2174                 prop = RNA_def_property(srna, id, PROP_INT, subtype);
2175                 RNA_def_property_int_default(prop, def);
2176                 RNA_def_property_ui_text(prop, name ? name : id, description);
2177                 RNA_def_property_range(prop, min, max);
2178                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
2179
2180                 if (pyopts) {
2181                         bpy_prop_assign_flag(prop, opts);
2182                 }
2183                 bpy_prop_callback_assign_update(prop, update_cb);
2184                 bpy_prop_callback_assign_int(prop, get_cb, set_cb);
2185                 RNA_def_property_duplicate_pointers(srna, prop);
2186         }
2187         Py_RETURN_NONE;
2188 }
2189
2190 PyDoc_STRVAR(BPy_IntVectorProperty_doc,
2191 ".. function:: IntVectorProperty(name=\"\", "
2192                                 "description=\"\", "
2193                                 "default=(0, 0, 0), min=-2**31, max=2**31-1, "
2194                                 "soft_min=-2**31, "
2195                                 "soft_max=2**31-1, "
2196                                 "step=1, "
2197                                 "options={'ANIMATABLE'}, "
2198                                 "subtype='NONE', "
2199                                 "size=3, "
2200                                 "update=None, "
2201                                 "get=None, "
2202                                 "set=None)\n"
2203 "\n"
2204 "   Returns a new vector int property definition.\n"
2205 "\n"
2206 BPY_PROPDEF_NAME_DOC
2207 BPY_PROPDEF_DESC_DOC
2208 "   :arg default: sequence of ints the length of *size*.\n"
2209 "   :type default: sequence\n"
2210 BPY_PROPDEF_NUM_MIN_DOC
2211 "   :type min: int\n"
2212 BPY_PROPDEF_NUM_MAX_DOC
2213 "   :type max: int\n"
2214 BPY_PROPDEF_NUM_SOFTMIN_DOC
2215 "   :type soft_min: int\n"
2216 BPY_PROPDEF_NUM_SOFTMAX_DOC
2217 "   :type soft_max: int\n"
2218 BPY_PROPDEF_INT_STEP_DOC
2219 BPY_PROPDEF_OPTIONS_DOC
2220 BPY_PROPDEF_SUBTYPE_ARRAY_DOC
2221 BPY_PROPDEF_VECSIZE_DOC
2222 BPY_PROPDEF_UPDATE_DOC
2223 BPY_PROPDEF_GET_DOC
2224 BPY_PROPDEF_SET_DOC
2225 );
2226 static PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
2227 {
2228         StructRNA *srna;
2229
2230         BPY_PROPDEF_HEAD(IntVectorProperty);
2231
2232         if (srna) {
2233                 static const char *kwlist[] = {"attr", "name", "description", "default",
2234                                                "min", "max", "soft_min", "soft_max",
2235                                                "step", "options", "subtype", "size", "update", "get", "set", NULL};
2236                 const char *id = NULL, *name = NULL, *description = "";
2237                 int id_len;
2238                 int min = INT_MIN, max = INT_MAX, soft_min = INT_MIN, soft_max = INT_MAX, step = 1;
2239                 int def[PYRNA_STACK_ARRAY] = {0};
2240                 int size = 3;
2241                 PropertyRNA *prop;
2242                 PyObject *pydef = NULL;
2243                 PyObject *pyopts = NULL;
2244                 int opts = 0;
2245                 const char *pysubtype = NULL;
2246                 int subtype = PROP_NONE;
2247                 PyObject *update_cb = NULL;
2248                 PyObject *get_cb = NULL;
2249                 PyObject *set_cb = NULL;
2250
2251                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2252                                                  "s#|ssOiiiiiO!siOOO:IntVectorProperty",
2253                                                  (char **)kwlist, &id, &id_len,
2254                                                  &name, &description, &pydef,
2255                                                  &min, &max, &soft_min, &soft_max,
2256                                                  &step, &PySet_Type, &pyopts,
2257                                                  &pysubtype, &size,
2258                                                  &update_cb, &get_cb, &set_cb))
2259                 {
2260                         return NULL;
2261                 }
2262
2263                 BPY_PROPDEF_SUBTYPE_CHECK(IntVectorProperty, property_flag_items, property_subtype_array_items);
2264
2265                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
2266                         PyErr_Format(PyExc_TypeError,
2267                                      "IntVectorProperty(size=%d): size must be between 0 and "
2268                                      STRINGIFY(PYRNA_STACK_ARRAY), size);
2269                         return NULL;
2270                 }
2271
2272                 if (pydef && PyC_AsArray(def, pydef, size, &PyLong_Type, false, "IntVectorProperty(default=sequence)") == -1)
2273                         return NULL;
2274
2275                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2276                         return NULL;
2277                 }
2278                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2279                         return NULL;
2280                 }
2281                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2282                         return NULL;
2283                 }
2284
2285                 prop = RNA_def_property(srna, id, PROP_INT, subtype);
2286                 RNA_def_property_array(prop, size);
2287                 if (pydef) RNA_def_property_int_array_default(prop, def);
2288                 RNA_def_property_range(prop, min, max);
2289                 RNA_def_property_ui_text(prop, name ? name : id, description);
2290                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
2291
2292                 if (pyopts) {
2293                         bpy_prop_assign_flag(prop, opts);
2294                 }
2295                 bpy_prop_callback_assign_update(prop, update_cb);
2296                 bpy_prop_callback_assign_int_array(prop, get_cb, set_cb);
2297                 RNA_def_property_duplicate_pointers(srna, prop);
2298         }
2299         Py_RETURN_NONE;
2300 }
2301
2302
2303 PyDoc_STRVAR(BPy_FloatProperty_doc,
2304 ".. function:: FloatProperty(name=\"\", "
2305                             "description=\"\", "
2306                             "default=0.0, "
2307                             "min=sys.float_info.min, max=sys.float_info.max, "
2308                             "soft_min=sys.float_info.min, soft_max=sys.float_info.max, "
2309                             "step=3, "
2310                             "precision=2, "
2311                             "options={'ANIMATABLE'}, "
2312                             "subtype='NONE', "
2313                             "unit='NONE', "
2314                             "update=None, "
2315                             "get=None, "
2316                             "set=None)\n"
2317 "\n"
2318 "   Returns a new float property definition.\n"
2319 "\n"
2320 BPY_PROPDEF_NAME_DOC
2321 BPY_PROPDEF_DESC_DOC
2322 BPY_PROPDEF_NUM_MIN_DOC
2323 "   :type min: float\n"
2324 BPY_PROPDEF_NUM_MAX_DOC
2325 "   :type max: float\n"
2326 BPY_PROPDEF_NUM_SOFTMIN_DOC
2327 "   :type soft_min: float\n"
2328 BPY_PROPDEF_NUM_SOFTMAX_DOC
2329 "   :type soft_max: float\n"
2330 BPY_PROPDEF_FLOAT_STEP_DOC
2331 BPY_PROPDEF_FLOAT_PREC_DOC
2332 BPY_PROPDEF_OPTIONS_DOC
2333 BPY_PROPDEF_SUBTYPE_NUMBER_DOC
2334 BPY_PROPDEF_UNIT_DOC
2335 BPY_PROPDEF_UPDATE_DOC
2336 BPY_PROPDEF_GET_DOC
2337 BPY_PROPDEF_SET_DOC
2338 );
2339 static PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
2340 {
2341         StructRNA *srna;
2342
2343         BPY_PROPDEF_HEAD(FloatProperty);
2344
2345         if (srna) {
2346                 static const char *kwlist[] = {"attr", "name", "description", "default",
2347                                                "min", "max", "soft_min", "soft_max",
2348                                                "step", "precision", "options", "subtype",
2349                                                "unit", "update", "get", "set", NULL};
2350                 const char *id = NULL, *name = NULL, *description = "";
2351                 int id_len;
2352                 float min = -FLT_MAX, max = FLT_MAX, soft_min = -FLT_MAX, soft_max = FLT_MAX, step = 3, def = 0.0f;
2353                 int precision = 2;
2354                 PropertyRNA *prop;
2355                 PyObject *pyopts = NULL;
2356                 int opts = 0;
2357                 const char *pysubtype = NULL;
2358                 int subtype = PROP_NONE;
2359                 const char *pyunit = NULL;
2360                 int unit = PROP_UNIT_NONE;
2361                 PyObject *update_cb = NULL;
2362                 PyObject *get_cb = NULL;
2363                 PyObject *set_cb = NULL;
2364
2365                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2366                                                  "s#|ssffffffiO!ssOOO:FloatProperty",
2367                                                  (char **)kwlist, &id, &id_len,
2368                                                  &name, &description, &def,
2369                                                  &min, &max, &soft_min, &soft_max,
2370                                                  &step, &precision, &PySet_Type,
2371                                                  &pyopts, &pysubtype, &pyunit,
2372                                                  &update_cb, &get_cb, &set_cb))
2373                 {
2374                         return NULL;
2375                 }
2376
2377                 BPY_PROPDEF_SUBTYPE_CHECK(FloatProperty, property_flag_items, property_subtype_number_items);
2378
2379                 if (pyunit && RNA_enum_value_from_id(rna_enum_property_unit_items, pyunit, &unit) == 0) {
2380                         PyErr_Format(PyExc_TypeError, "FloatProperty(unit='%s'): invalid unit", pyunit);
2381                         return NULL;
2382                 }
2383
2384                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2385                         return NULL;
2386                 }
2387                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2388                         return NULL;
2389                 }
2390                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2391                         return NULL;
2392                 }
2393
2394                 prop = RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
2395                 RNA_def_property_float_default(prop, def);
2396                 RNA_def_property_range(prop, min, max);
2397                 RNA_def_property_ui_text(prop, name ? name : id, description);
2398                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
2399
2400                 if (pyopts) {
2401                         bpy_prop_assign_flag(prop, opts);
2402                 }
2403                 bpy_prop_callback_assign_update(prop, update_cb);
2404                 bpy_prop_callback_assign_float(prop, get_cb, set_cb);
2405                 RNA_def_property_duplicate_pointers(srna, prop);
2406         }
2407         Py_RETURN_NONE;
2408 }
2409
2410 PyDoc_STRVAR(BPy_FloatVectorProperty_doc,
2411 ".. function:: FloatVectorProperty(name=\"\", "
2412                                   "description=\"\", "
2413                                   "default=(0.0, 0.0, 0.0), "
2414                                   "min=sys.float_info.min, max=sys.float_info.max, "
2415                                   "soft_min=sys.float_info.min, soft_max=sys.float_info.max, "
2416                                   "step=3, "
2417                                   "precision=2, "
2418                                   "options={'ANIMATABLE'}, "
2419                                   "subtype='NONE', "
2420                                   "unit='NONE', "
2421                                   "size=3, "
2422                                   "update=None, "
2423                                   "get=None, "
2424                                   "set=None)\n"
2425 "\n"
2426 "   Returns a new vector float property definition.\n"
2427 "\n"
2428 BPY_PROPDEF_NAME_DOC
2429 BPY_PROPDEF_DESC_DOC
2430 "   :arg default: sequence of floats the length of *size*.\n"
2431 "   :type default: sequence\n"
2432 BPY_PROPDEF_NUM_MIN_DOC
2433 "   :type min: float\n"
2434 BPY_PROPDEF_NUM_MAX_DOC
2435 "   :type max: float\n"
2436 BPY_PROPDEF_NUM_SOFTMIN_DOC
2437 "   :type soft_min: float\n"
2438 BPY_PROPDEF_NUM_SOFTMAX_DOC
2439 "   :type soft_max: float\n"
2440 BPY_PROPDEF_OPTIONS_DOC
2441 BPY_PROPDEF_FLOAT_STEP_DOC
2442 BPY_PROPDEF_FLOAT_PREC_DOC
2443 BPY_PROPDEF_SUBTYPE_ARRAY_DOC
2444 BPY_PROPDEF_UNIT_DOC
2445 BPY_PROPDEF_VECSIZE_DOC
2446 BPY_PROPDEF_UPDATE_DOC
2447 BPY_PROPDEF_GET_DOC
2448 BPY_PROPDEF_SET_DOC
2449 );
2450 static PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
2451 {
2452         StructRNA *srna;
2453
2454         BPY_PROPDEF_HEAD(FloatVectorProperty);
2455
2456         if (srna) {
2457                 static const char *kwlist[] = {"attr", "name", "description", "default",
2458                                                "min", "max", "soft_min", "soft_max",
2459                                                "step", "precision", "options", "subtype",
2460                                                "unit", "size", "update", "get", "set", NULL};
2461                 const char *id = NULL, *name = NULL, *description = "";
2462                 int id_len;
2463                 float min = -FLT_MAX, max = FLT_MAX, soft_min = -FLT_MAX, soft_max = FLT_MAX, step = 3;
2464                 float def[PYRNA_STACK_ARRAY] = {0.0f};
2465                 int precision = 2, size = 3;
2466                 PropertyRNA *prop;
2467                 PyObject *pydef = NULL;
2468                 PyObject *pyopts = NULL;
2469                 int opts = 0;
2470                 const char *pysubtype = NULL;
2471                 int subtype = PROP_NONE;
2472                 const char *pyunit = NULL;
2473                 int unit = PROP_UNIT_NONE;
2474                 PyObject *update_cb = NULL;
2475                 PyObject *get_cb = NULL;
2476                 PyObject *set_cb = NULL;
2477
2478                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2479                                                  "s#|ssOfffffiO!ssiOOO:FloatVectorProperty",
2480                                                  (char **)kwlist, &id, &id_len,
2481                                                  &name, &description, &pydef,
2482                                                  &min, &max, &soft_min, &soft_max,
2483                                                  &step, &precision, &PySet_Type,
2484                                                  &pyopts, &pysubtype, &pyunit, &size,
2485                                                  &update_cb, &get_cb, &set_cb))
2486                 {
2487                         return NULL;
2488                 }
2489
2490                 BPY_PROPDEF_SUBTYPE_CHECK(FloatVectorProperty, property_flag_items, property_subtype_array_items);
2491
2492                 if (pyunit && RNA_enum_value_from_id(rna_enum_property_unit_items, pyunit, &unit) == 0) {
2493                         PyErr_Format(PyExc_TypeError, "FloatVectorProperty(unit='%s'): invalid unit", pyunit);
2494                         return NULL;
2495                 }
2496
2497                 if (size < 1 || size > PYRNA_STACK_ARRAY) {
2498                         PyErr_Format(PyExc_TypeError,
2499                                      "FloatVectorProperty(size=%d): size must be between 0 and "
2500                                      STRINGIFY(PYRNA_STACK_ARRAY), size);
2501                         return NULL;
2502                 }
2503
2504                 if (pydef && PyC_AsArray(def, pydef, size, &PyFloat_Type, false, "FloatVectorProperty(default=sequence)") == -1)
2505                         return NULL;
2506
2507                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2508                         return NULL;
2509                 }
2510                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2511                         return NULL;
2512                 }
2513                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2514                         return NULL;
2515                 }
2516
2517                 prop = RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
2518                 RNA_def_property_array(prop, size);
2519                 if (pydef) RNA_def_property_float_array_default(prop, def);
2520                 RNA_def_property_range(prop, min, max);
2521                 RNA_def_property_ui_text(prop, name ? name : id, description);
2522                 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
2523
2524                 if (pyopts) {
2525                         bpy_prop_assign_flag(prop, opts);
2526                 }
2527                 bpy_prop_callback_assign_update(prop, update_cb);
2528                 bpy_prop_callback_assign_float_array(prop, get_cb, set_cb);
2529                 RNA_def_property_duplicate_pointers(srna, prop);
2530         }
2531         Py_RETURN_NONE;
2532 }
2533
2534 PyDoc_STRVAR(BPy_StringProperty_doc,
2535 ".. function:: StringProperty(name=\"\", "
2536                              "description=\"\", "
2537                              "default=\"\", "
2538                              "maxlen=0, "
2539                              "options={'ANIMATABLE'}, "
2540                              "subtype='NONE', "
2541                              "update=None, "
2542                              "get=None, "
2543                              "set=None)\n"
2544 "\n"
2545 "   Returns a new string property definition.\n"
2546 "\n"
2547 BPY_PROPDEF_NAME_DOC
2548 BPY_PROPDEF_DESC_DOC
2549 "   :arg default: initializer string.\n"
2550 "   :type default: string\n"
2551 "   :arg maxlen: maximum length of the string.\n"
2552 "   :type maxlen: int\n"
2553 BPY_PROPDEF_OPTIONS_DOC
2554 BPY_PROPDEF_SUBTYPE_STRING_DOC
2555 BPY_PROPDEF_UPDATE_DOC
2556 BPY_PROPDEF_GET_DOC
2557 BPY_PROPDEF_SET_DOC
2558 );
2559 static PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
2560 {
2561         StructRNA *srna;
2562
2563         BPY_PROPDEF_HEAD(StringProperty);
2564
2565         if (srna) {
2566                 static const char *kwlist[] = {"attr", "name", "description", "default",
2567                                                "maxlen", "options", "subtype", "update", "get", "set", NULL};
2568                 const char *id = NULL, *name = NULL, *description = "", *def = "";
2569                 int id_len;
2570                 int maxlen = 0;
2571                 PropertyRNA *prop;
2572                 PyObject *pyopts = NULL;
2573                 int opts = 0;
2574                 const char *pysubtype = NULL;
2575                 int subtype = PROP_NONE;
2576                 PyObject *update_cb = NULL;
2577                 PyObject *get_cb = NULL;
2578                 PyObject *set_cb = NULL;
2579
2580                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2581                                                  "s#|sssiO!sOOO:StringProperty",
2582                                                  (char **)kwlist, &id, &id_len,
2583                                                  &name, &description, &def,
2584                                                  &maxlen, &PySet_Type, &pyopts, &pysubtype,
2585                                                  &update_cb, &get_cb, &set_cb))
2586                 {
2587                         return NULL;
2588                 }
2589
2590                 BPY_PROPDEF_SUBTYPE_CHECK(StringProperty, property_flag_items, property_subtype_string_items);
2591
2592                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2593                         return NULL;
2594                 }
2595                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2596                         return NULL;
2597                 }
2598                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2599                         return NULL;
2600                 }
2601
2602                 prop = RNA_def_property(srna, id, PROP_STRING, subtype);
2603                 if (maxlen != 0) RNA_def_property_string_maxlength(prop, maxlen + 1);  /* +1 since it includes null terminator */
2604                 if (def && def[0]) RNA_def_property_string_default(prop, def);
2605                 RNA_def_property_ui_text(prop, name ? name : id, description);
2606
2607                 if (pyopts) {
2608                         bpy_prop_assign_flag(prop, opts);
2609                 }
2610                 bpy_prop_callback_assign_update(prop, update_cb);
2611                 bpy_prop_callback_assign_string(prop, get_cb, set_cb);
2612                 RNA_def_property_duplicate_pointers(srna, prop);
2613         }
2614         Py_RETURN_NONE;
2615 }
2616
2617 PyDoc_STRVAR(BPy_EnumProperty_doc,
2618 ".. function:: EnumProperty(items, "
2619                            "name=\"\", "
2620                            "description=\"\", "
2621                            "default=None, "
2622                            "options={'ANIMATABLE'}, "
2623                            "update=None, "
2624                            "get=None, "
2625                            "set=None)\n"
2626 "\n"
2627 "   Returns a new enumerator property definition.\n"
2628 "\n"
2629 "   :arg items: sequence of enum items formatted:\n"
2630 "      ``[(identifier, name, description, icon, number), ...]``.\n"
2631 "\n"
2632 "      The first three elements of the tuples are mandatory.\n"
2633 "\n"
2634 "      :identifier: The identifier is used for Python access.\n"
2635 "      :name: Name for the interace.\n"
2636 "      :description: Used for documentation and tooltips.\n"
2637 "      :icon: An icon string identifier or integer icon value\n"
2638 "         (e.g. returned by :class:`bpy.types.UILayout.icon`)\n"
2639 "      :number: Unique value used as the identifier for this item (stored in file data).\n"
2640 "         Use when the identifier may need to change.\n"
2641 "\n"
2642 "      When an item only contains 4 items they define ``(identifier, name, description, number)``.\n"
2643 "\n"
2644 "      For dynamic values a callback can be passed which returns a list in\n"
2645 "      the same format as the static list.\n"
2646 "      This function must take 2 arguments ``(self, context)``, **context may be None**.\n"
2647 "\n"
2648 "      .. warning::\n"
2649 "\n"
2650 "         There is a known bug with using a callback,\n"
2651 "         Python must keep a reference to the strings returned or Blender will misbehave\n"
2652 "         or even crash."
2653 "\n"
2654 "   :type items: sequence of string tuples or a function\n"
2655 BPY_PROPDEF_NAME_DOC
2656 BPY_PROPDEF_DESC_DOC
2657 "   :arg default: The default value for this enum, a string from the identifiers used in *items*.\n"
2658 "      If the *ENUM_FLAG* option is used this must be a set of such string identifiers instead.\n"
2659 "      WARNING: It shall not be specified (or specified to its default *None* value) for dynamic enums\n"
2660 "      (i.e. if a callback function is given as *items* parameter).\n"
2661 "   :type default: string or set\n"
2662 BPY_PROPDEF_OPTIONS_ENUM_DOC
2663 BPY_PROPDEF_UPDATE_DOC
2664 BPY_PROPDEF_GET_DOC
2665 BPY_PROPDEF_SET_DOC
2666 );
2667 static PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw)
2668 {
2669         StructRNA *srna;
2670
2671         BPY_PROPDEF_HEAD(EnumProperty);
2672         
2673         if (srna) {
2674                 static const char *kwlist[] = {"attr", "items", "name", "description", "default",
2675                                                "options", "update", "get", "set", NULL};
2676                 const char *id = NULL, *name = NULL, *description = "";
2677                 PyObject *def = NULL;
2678                 int id_len;
2679                 int defvalue = 0;
2680                 PyObject *items, *items_fast;
2681                 EnumPropertyItem *eitems;
2682                 PropertyRNA *prop;
2683                 PyObject *pyopts = NULL;
2684                 int opts = 0;
2685                 bool is_itemf = false;
2686                 PyObject *update_cb = NULL;
2687                 PyObject *get_cb = NULL;
2688                 PyObject *set_cb = NULL;
2689
2690                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2691                                                  "s#O|ssOO!OOO:EnumProperty",
2692                                                  (char **)kwlist, &id, &id_len,
2693                                                  &items, &name, &description,
2694                                                  &def, &PySet_Type, &pyopts,
2695                                                  &update_cb, &get_cb, &set_cb))
2696                 {
2697                         return NULL;
2698                 }
2699
2700                 BPY_PROPDEF_CHECK(EnumProperty, property_flag_enum_items);
2701
2702                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2703                         return NULL;
2704                 }
2705                 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2706                         return NULL;
2707                 }
2708                 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2709                         return NULL;
2710                 }
2711
2712                 if (def == Py_None) {
2713                         /* This allows to get same behavior when explicitly passing None as default value,
2714                          * and not defining a default value at all! */
2715                         def = NULL;
2716                 }
2717
2718                 /* items can be a list or a callable */
2719                 if (PyFunction_Check(items)) { /* don't use PyCallable_Check because we need the function code for errors */
2720                         PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(items);
2721                         if (f_code->co_argcount != 2) {
2722                                 PyErr_Format(PyExc_ValueError,
2723                                              "EnumProperty(...): expected 'items' function to take 2 arguments, not %d",
2724                                              f_code->co_argcount);
2725                                 return NULL;
2726                         }
2727
2728                         if (def) {
2729                                 /* note, using type error here is odd but python does this for invalid arguments */
2730                                 PyErr_SetString(PyExc_TypeError,
2731                                                 "EnumProperty(...): 'default' can't be set when 'items' is a function");
2732                                 return NULL;
2733                         }
2734
2735                         is_itemf = true;
2736                         eitems = DummyRNA_NULL_items;
2737                 }
2738                 else {
2739                         if (!(items_fast = PySequence_Fast(items, "EnumProperty(...): "
2740                                                            "expected a sequence of tuples for the enum items or a function")))
2741                         {
2742                                 return NULL;
2743                         }
2744
2745                         eitems = enum_items_from_py(items_fast, def, &defvalue,
2746                                                     (opts & PROP_ENUM_FLAG) != 0);
2747
2748                         if (!eitems) {
2749                                 Py_DECREF(items_fast);
2750                                 return NULL;
2751                         }
2752                 }
2753
2754                 if (opts & PROP_ENUM_FLAG)  prop = RNA_def_enum_flag(srna, id, eitems, defvalue, name ? name : id, description);
2755                 else                        prop = RNA_def_enum(srna, id, eitems, defvalue, name ? name : id, description);
2756
2757                 if (pyopts) {
2758                         bpy_prop_assign_flag(prop, opts);
2759                 }
2760                 bpy_prop_callback_assign_update(prop, update_cb);
2761                 bpy_prop_callback_assign_enum(prop, get_cb, set_cb, (is_itemf ? items : NULL));
2762                 RNA_def_property_duplicate_pointers(srna, prop);
2763
2764                 if (is_itemf == false) {
2765                         /* note: this must be postponed until after #RNA_def_property_duplicate_pointers
2766                          * otherwise if this is a generator it may free the strings before we copy them */
2767                         Py_DECREF(items_fast);
2768
2769                         MEM_freeN(eitems);
2770                 }
2771         }
2772         Py_RETURN_NONE;
2773 }
2774
2775 static StructRNA *pointer_type_from_py(PyObject *value, const char *error_prefix)
2776 {
2777         StructRNA *srna;
2778
2779         srna = srna_from_self(value, "");
2780         if (!srna) {
2781                 if (PyErr_Occurred()) {
2782                         PyObject *msg = PyC_ExceptionBuffer();
2783                         const char *msg_char = _PyUnicode_AsString(msg);
2784                         PyErr_Format(PyExc_TypeError,
2785                                      "%.200s expected an RNA type derived from PropertyGroup, failed with: %s",
2786                                      error_prefix, msg_char);
2787                         Py_DECREF(msg);
2788                 }
2789                 else {
2790                         PyErr_Format(PyExc_TypeError,
2791                                      "%.200s expected an RNA type derived from PropertyGroup, failed with type '%s'",
2792                                      error_prefix, Py_TYPE(value)->tp_name);
2793                 }
2794                 return NULL;
2795         }
2796
2797         if (!RNA_struct_is_a(srna, &RNA_PropertyGroup)) {
2798                 PyErr_Format(PyExc_TypeError,
2799                              "%.200s expected an RNA type derived from PropertyGroup",
2800                              error_prefix);
2801                 return NULL;
2802         }
2803
2804         return srna;
2805 }
2806
2807 PyDoc_STRVAR(BPy_PointerProperty_doc,
2808 ".. function:: PointerProperty(type=None, "
2809                               "name=\"\", "
2810                               "description=\"\", "
2811                               "options={'ANIMATABLE'}, "
2812                               "update=None)\n"
2813 "\n"
2814 "   Returns a new pointer property definition.\n"
2815 "\n"
2816 BPY_PROPDEF_TYPE_DOC
2817 BPY_PROPDEF_NAME_DOC
2818 BPY_PROPDEF_DESC_DOC
2819 BPY_PROPDEF_OPTIONS_DOC
2820 BPY_PROPDEF_UPDATE_DOC
2821 );
2822 static PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw)
2823 {
2824         StructRNA *srna;
2825
2826         BPY_PROPDEF_HEAD(PointerProperty);
2827
2828         if (srna) {
2829                 static const char *kwlist[] = {"attr", "type", "name", "description", "options", "update", NULL};
2830                 const char *id = NULL, *name = NULL, *description = "";
2831                 int id_len;
2832                 PropertyRNA *prop;
2833                 StructRNA *ptype;
2834                 PyObject *type = Py_None;
2835                 PyObject *pyopts = NULL;
2836                 int opts = 0;
2837                 PyObject *update_cb = NULL;
2838
2839                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2840                                                  "s#O|ssO!O:PointerProperty",
2841                                                  (char **)kwlist, &id, &id_len,
2842                                                  &type, &name, &description,
2843                                                  &PySet_Type, &pyopts,
2844                                                  &update_cb))
2845                 {
2846                         return NULL;
2847                 }
2848
2849                 BPY_PROPDEF_CHECK(PointerProperty, property_flag_items);
2850
2851                 ptype = pointer_type_from_py(type, "PointerProperty(...):");
2852                 if (!ptype)
2853                         return NULL;
2854
2855                 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2856                         return NULL;
2857                 }
2858
2859                 prop = RNA_def_pointer_runtime(srna, id, ptype, name ? name : id, description);
2860                 if (pyopts) {
2861                         bpy_prop_assign_flag(prop, opts);
2862                 }
2863                 bpy_prop_callback_assign_update(prop, update_cb);
2864                 RNA_def_property_duplicate_pointers(srna, prop);
2865         }
2866         Py_RETURN_NONE;
2867 }
2868
2869 PyDoc_STRVAR(BPy_CollectionProperty_doc,
2870 ".. function:: CollectionProperty(type=None, "
2871                                  "name=\"\", "
2872                                  "description=\"\", "
2873                                  "options={'ANIMATABLE'})\n"
2874 "\n"
2875 "   Returns a new collection property definition.\n"
2876 "\n"
2877 BPY_PROPDEF_TYPE_DOC
2878 BPY_PROPDEF_NAME_DOC
2879 BPY_PROPDEF_DESC_DOC
2880 BPY_PROPDEF_OPTIONS_DOC
2881 );
2882 static PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
2883 {
2884         StructRNA *srna;
2885
2886         BPY_PROPDEF_HEAD(CollectionProperty);
2887
2888         if (srna) {
2889                 static const char *kwlist[] = {"attr", "type", "name", "description", "options", NULL};
2890                 const char *id = NULL, *name = NULL, *description = "";
2891                 int id_len;
2892                 PropertyRNA *prop;
2893                 StructRNA *ptype;
2894                 PyObject *type = Py_None;
2895                 PyObject *pyopts = NULL;
2896                 int opts = 0;
2897
2898                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2899                                                  "s#O|ssO!:CollectionProperty",
2900                                                  (char **)kwlist, &id, &id_len,
2901                                                  &type, &name, &description,
2902                                                  &PySet_Type, &pyopts))
2903                 {
2904                         return NULL;
2905                 }
2906
2907                 BPY_PROPDEF_CHECK(CollectionProperty, property_flag_items);
2908
2909                 ptype = pointer_type_from_py(type, "CollectionProperty(...):");
2910                 if (!ptype)
2911                         return NULL;
2912
2913                 prop = RNA_def_collection_runtime(srna, id, ptype, name ? name : id, description);
2914                 if (pyopts) {
2915                         bpy_prop_assign_flag(prop, opts);
2916                 }
2917                 RNA_def_property_duplicate_pointers(srna, prop);
2918         }
2919         Py_RETURN_NONE;
2920 }
2921
2922 PyDoc_STRVAR(BPy_RemoveProperty_doc,
2923 ".. function:: RemoveProperty(cls, attr="")\n"
2924 "\n"
2925 "   Removes a dynamically defined property.\n"
2926 "\n"
2927 "   :arg cls: The class containing the property (must be a positional argument).\n"
2928 "   :type cls: type\n"
2929 "   :arg attr: Property name (must be passed as a keyword).\n"
2930 "   :type attr: string\n"
2931 "\n"
2932 ".. note:: Typically this function doesn't need to be accessed directly.\n"
2933 "   Instead use ``del cls.attr``\n"
2934 );
2935 static PyObject *BPy_RemoveProperty(PyObject *self, PyObject *args, PyObject *kw)
2936 {
2937         StructRNA *srna;
2938
2939         if (PyTuple_GET_SIZE(args) == 1) {
2940                 PyObject *ret;
2941                 self = PyTuple_GET_ITEM(args, 0);
2942                 args = PyTuple_New(0);
2943                 ret = BPy_RemoveProperty(self, args, kw);
2944                 Py_DECREF(args);
2945                 return ret;
2946         }
2947         else if (PyTuple_GET_SIZE(args) > 1) {
2948                 PyErr_SetString(PyExc_ValueError, "expected one positional arg, one keyword arg");
2949                 return NULL;
2950         }
2951
2952         srna = srna_from_self(self, "RemoveProperty(...):");
2953         if (srna == NULL && PyErr_Occurred()) {
2954                 return NULL; /* self's type was compatible but error getting the srna */
2955         }
2956         else if (srna == NULL) {
2957                 PyErr_SetString(PyExc_TypeError, "RemoveProperty(): struct rna not available for this type");
2958                 return NULL;
2959         }
2960         else {
2961                 static const char *kwlist[] = {"attr", NULL};
2962                 
2963                 const char *id = NULL;
2964
2965                 if (!PyArg_ParseTupleAndKeywords(args, kw,
2966                                                  "s:RemoveProperty",
2967                                                  (char **)kwlist, &id))
2968                 {
2969                         return NULL;
2970                 }
2971
2972                 if (RNA_def_property_free_identifier(srna, id) != 1) {
2973                         PyErr_Format(PyExc_TypeError, "RemoveProperty(): '%s' not a defined dynamic property", id);
2974                         return NULL;
2975                 }
2976         }
2977         Py_RETURN_NONE;
2978 }
2979
2980 static struct PyMethodDef props_methods[] = {
2981         {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS | METH_KEYWORDS, BPy_BoolProperty_doc},
2982         {"BoolVectorProperty", (PyCFunction)BPy_BoolVectorProperty, METH_VARARGS | METH_KEYWORDS, BPy_BoolVectorProperty_doc},
2983         {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS | METH_KEYWORDS, BPy_IntProperty_doc},
2984         {"IntVectorProperty", (PyCFunction)BPy_IntVectorProperty, METH_VARARGS | METH_KEYWORDS, BPy_IntVectorProperty_doc},
2985         {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS | METH_KEYWORDS, BPy_FloatProperty_doc},
2986         {"FloatVectorProperty", (PyCFunction)BPy_FloatVectorProperty, METH_VARARGS | METH_KEYWORDS, BPy_FloatVectorProperty_doc},
2987         {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS | METH_KEYWORDS, BPy_StringProperty_doc},
2988         {"EnumProperty", (PyCFunction)BPy_EnumProperty, METH_VARARGS | METH_KEYWORDS, BPy_EnumProperty_doc},
2989         {"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS | METH_KEYWORDS, BPy_PointerProperty_doc},
2990         {"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS | METH_KEYWORDS, BPy_CollectionProperty_doc},
2991
2992         {"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS | METH_KEYWORDS, BPy_RemoveProperty_doc},
2993         {NULL, NULL, 0, NULL}
2994 };
2995
2996 static struct PyModuleDef props_module = {
2997         PyModuleDef_HEAD_INIT,
2998         "bpy.props",
2999         "This module defines properties to extend Blender's internal data. The result of these functions"
3000         " is used to assign properties to classes registered with Blender and can't be used directly.\n"
3001         "\n"
3002         ".. note:: All parameters to these functions must be passed as keywords.\n",
3003         -1, /* multiple "initialization" just copies the module dict. */
3004         props_methods,
3005         NULL, NULL, NULL, NULL
3006 };
3007
3008 PyObject *BPY_rna_props(void)
3009 {
3010         PyObject *submodule;
3011         PyObject *submodule_dict;
3012         
3013         submodule = PyModule_Create(&props_module);
3014         PyDict_SetItemString(PyImport_GetModuleDict(), props_module.m_name, submodule);
3015
3016         /* INCREF since its its assumed that all these functions return the
3017          * module with a new ref like PyDict_New, since they are passed to
3018          * PyModule_AddObject which steals a ref */
3019         Py_INCREF(submodule);
3020         
3021         /* api needs the PyObjects internally */
3022         submodule_dict = PyModule_GetDict(submodule);
3023
3024 #define ASSIGN_STATIC(_name) pymeth_##_name = PyDict_GetItemString(submodule_dict, #_name)
3025
3026         ASSIGN_STATIC(BoolProperty);
3027         ASSIGN_STATIC(BoolVectorProperty);
3028         ASSIGN_STATIC(IntProperty);
3029         ASSIGN_STATIC(IntVectorProperty);
3030         ASSIGN_STATIC(FloatProperty);
3031         ASSIGN_STATIC(FloatVectorProperty);
3032         ASSIGN_STATIC(StringProperty);
3033         ASSIGN_STATIC(EnumProperty);
3034         ASSIGN_STATIC(PointerProperty);
3035         ASSIGN_STATIC(CollectionProperty);
3036         ASSIGN_STATIC(RemoveProperty);
3037         
3038         return submodule;
3039 }