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