2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * Contributor(s): Campbell Barton
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file blender/python/intern/bpy_props.c
24 * \ingroup pythonintern
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.
34 #include "RNA_types.h"
36 #include "BLI_utildefines.h"
38 #include "bpy_props.h"
42 #include "BKE_idprop.h"
44 #include "RNA_access.h"
45 #include "RNA_define.h" /* for defining our own rna */
46 #include "RNA_enum_types.h"
48 #include "MEM_guardedalloc.h"
50 #include "../generic/py_capi_utils.h"
52 /* initial definition of callback slots we'll probably have more than 1 */
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,
61 extern BPy_StructRNA *bpy_context_module;
63 static 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}};
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 " :arg poll: function to be called to determine whether an item is valid for this property.\n" \
77 " The function must take 2 values (self,object) and return Bool.\n" \
78 " :type poll: function\n" \
80 static EnumPropertyItem property_flag_enum_items[] = {
81 {PROP_HIDDEN, "HIDDEN", 0, "Hidden", ""},
82 {PROP_SKIP_SAVE, "SKIP_SAVE", 0, "Skip Save", ""},
83 {PROP_ANIMATABLE, "ANIMATABLE", 0, "Animatable", ""},
84 {PROP_LIB_EXCEPTION, "LIBRARY_EDITABLE", 0, "Library Editable", ""},
85 {PROP_ENUM_FLAG, "ENUM_FLAG", 0, "Enum Flag", ""},
86 {0, NULL, 0, NULL, NULL}};
88 #define BPY_PROPDEF_OPTIONS_ENUM_DOC \
89 " :arg options: Enumerator in ['HIDDEN', 'SKIP_SAVE', 'ANIMATABLE', 'ENUM_FLAG', 'LIBRARY_EDITABLE'].\n" \
90 " :type options: set\n" \
93 /* XXX Keep in sync with rna_rna.c's rna_enum_property_subtype_items ???
94 * Currently it is not...
96 static EnumPropertyItem property_subtype_string_items[] = {
97 {PROP_FILEPATH, "FILE_PATH", 0, "File Path", ""},
98 {PROP_DIRPATH, "DIR_PATH", 0, "Directory Path", ""},
99 {PROP_FILENAME, "FILE_NAME", 0, "Filename", ""},
100 {PROP_BYTESTRING, "BYTE_STRING", 0, "Byte String", ""},
101 {PROP_PASSWORD, "PASSWORD", 0, "Password", "A string that is displayed hidden ('********')"},
103 {PROP_NONE, "NONE", 0, "None", ""},
104 {0, NULL, 0, NULL, NULL}};
106 #define BPY_PROPDEF_SUBTYPE_STRING_DOC \
107 " :arg subtype: Enumerator in ['FILE_PATH', 'DIR_PATH', 'FILE_NAME', 'BYTE_STRING', 'PASSWORD', 'NONE'].\n" \
108 " :type subtype: string\n" \
110 static EnumPropertyItem property_subtype_number_items[] = {
111 {PROP_PIXEL, "PIXEL", 0, "Pixel", ""},
112 {PROP_UNSIGNED, "UNSIGNED", 0, "Unsigned", ""},
113 {PROP_PERCENTAGE, "PERCENTAGE", 0, "Percentage", ""},
114 {PROP_FACTOR, "FACTOR", 0, "Factor", ""},
115 {PROP_ANGLE, "ANGLE", 0, "Angle", ""},
116 {PROP_TIME, "TIME", 0, "Time", ""},
117 {PROP_DISTANCE, "DISTANCE", 0, "Distance", ""},
119 {PROP_NONE, "NONE", 0, "None", ""},
120 {0, NULL, 0, NULL, NULL}};
122 #define BPY_PROPDEF_SUBTYPE_NUMBER_DOC \
123 " :arg subtype: Enumerator in ['PIXEL', 'UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n" \
124 " :type subtype: string\n" \
126 static EnumPropertyItem property_subtype_array_items[] = {
127 {PROP_COLOR, "COLOR", 0, "Color", ""},
128 {PROP_TRANSLATION, "TRANSLATION", 0, "Translation", ""},
129 {PROP_DIRECTION, "DIRECTION", 0, "Direction", ""},
130 {PROP_VELOCITY, "VELOCITY", 0, "Velocity", ""},
131 {PROP_ACCELERATION, "ACCELERATION", 0, "Acceleration", ""},
132 {PROP_MATRIX, "MATRIX", 0, "Matrix", ""},
133 {PROP_EULER, "EULER", 0, "Euler", ""},
134 {PROP_QUATERNION, "QUATERNION", 0, "Quaternion", ""},
135 {PROP_AXISANGLE, "AXISANGLE", 0, "Axis Angle", ""},
136 {PROP_XYZ, "XYZ", 0, "XYZ", ""},
137 {PROP_COLOR_GAMMA, "COLOR_GAMMA", 0, "Color Gamma", ""},
138 {PROP_LAYER, "LAYER", 0, "Layer", ""},
140 {PROP_NONE, "NONE", 0, "None", ""},
141 {0, NULL, 0, NULL, NULL}};
143 #define BPY_PROPDEF_SUBTYPE_ARRAY_DOC \
144 " :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', " \
145 "'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', " \
146 "'XYZ', 'COLOR_GAMMA', 'LAYER', 'NONE'].\n" \
147 " :type subtype: string\n"
150 static PyObject *pymeth_BoolProperty = NULL;
151 static PyObject *pymeth_BoolVectorProperty = NULL;
152 static PyObject *pymeth_IntProperty = NULL;
153 static PyObject *pymeth_IntVectorProperty = NULL;
154 static PyObject *pymeth_FloatProperty = NULL;
155 static PyObject *pymeth_FloatVectorProperty = NULL;
156 static PyObject *pymeth_StringProperty = NULL;
157 static PyObject *pymeth_EnumProperty = NULL;
158 static PyObject *pymeth_PointerProperty = NULL;
159 static PyObject *pymeth_CollectionProperty = NULL;
160 static PyObject *pymeth_RemoveProperty = NULL;
162 static PyObject *pyrna_struct_as_instance(PointerRNA *ptr)
164 PyObject *self = NULL;
166 /* operators can store their own instance for later use */
168 void **instance = RNA_struct_instance(ptr);
178 /* in most cases this will run */
180 self = pyrna_struct_CreatePyObject(ptr);
186 /* could be moved into bpy_utils */
187 static void printf_func_error(PyObject *py_func)
189 /* since we return to C code we can't leave the error */
190 PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(py_func);
194 /* use py style error */
195 fprintf(stderr, "File \"%s\", line %d, in %s\n",
196 _PyUnicode_AsString(f_code->co_filename),
197 f_code->co_firstlineno,
198 _PyUnicode_AsString(((PyFunctionObject *)py_func)->func_name)
202 static void bpy_prop_assign_flag(PropertyRNA *prop, const int flag)
204 const int flag_mask = ((PROP_ANIMATABLE) & ~flag);
207 RNA_def_property_flag(prop, flag);
211 RNA_def_property_clear_flag(prop, flag_mask);
215 /* operators and classes use this so it can store the args given but defer
216 * running it until the operator runs where these values are used to setup
217 * the default args for that operator instance */
218 static PyObject *bpy_prop_deferred_return(PyObject *func, PyObject *kw)
220 PyObject *ret = PyTuple_New(2);
221 PyTuple_SET_ITEM(ret, 0, func);
229 PyTuple_SET_ITEM(ret, 1, kw);
235 static void bpy_prop_update_cb(struct bContext *C, struct PointerRNA *ptr, struct PropertyRNA *prop)
237 PyGILState_STATE gilstate;
238 PyObject **py_data = RNA_property_py_data_get(prop);
243 const bool is_write_ok = pyrna_write_check();
245 BLI_assert(py_data != NULL);
248 pyrna_write_set(true);
251 bpy_context_set(C, &gilstate);
253 py_func = py_data[BPY_DATA_CB_SLOT_UPDATE];
255 args = PyTuple_New(2);
256 self = pyrna_struct_as_instance(ptr);
257 PyTuple_SET_ITEM(args, 0, self);
259 PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
260 Py_INCREF(bpy_context_module);
262 ret = PyObject_CallObject(py_func, args);
267 printf_func_error(py_func);
270 if (ret != Py_None) {
271 PyErr_SetString(PyExc_ValueError, "the return value must be None");
272 printf_func_error(py_func);
278 bpy_context_clear(C, &gilstate);
281 pyrna_write_set(false);
285 static int bpy_prop_boolean_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop)
287 PyObject **py_data = RNA_property_py_data_get(prop);
292 PyGILState_STATE gilstate;
294 const bool is_write_ok = pyrna_write_check();
297 BLI_assert(py_data != NULL);
300 pyrna_write_set(true);
303 use_gil = true; /* !PyC_IsInterpreterActive(); */
306 gilstate = PyGILState_Ensure();
308 py_func = py_data[BPY_DATA_CB_SLOT_GET];
310 args = PyTuple_New(1);
311 self = pyrna_struct_as_instance(ptr);
312 PyTuple_SET_ITEM(args, 0, self);
314 ret = PyObject_CallObject(py_func, args);
319 printf_func_error(py_func);
323 value = PyLong_AsLong(ret);
325 if (value == -1 && PyErr_Occurred()) {
326 printf_func_error(py_func);
334 PyGILState_Release(gilstate);
337 pyrna_write_set(false);
343 static void bpy_prop_boolean_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, int value)
345 PyObject **py_data = RNA_property_py_data_get(prop);
350 PyGILState_STATE gilstate;
352 const bool is_write_ok = pyrna_write_check();
354 BLI_assert(py_data != NULL);
357 pyrna_write_set(true);
360 use_gil = true; /* !PyC_IsInterpreterActive(); */
363 gilstate = PyGILState_Ensure();
365 py_func = py_data[BPY_DATA_CB_SLOT_SET];
367 args = PyTuple_New(2);
368 self = pyrna_struct_as_instance(ptr);
369 PyTuple_SET_ITEM(args, 0, self);
371 PyTuple_SET_ITEM(args, 1, PyBool_FromLong(value));
373 ret = PyObject_CallObject(py_func, args);
378 printf_func_error(py_func);
381 if (ret != Py_None) {
382 PyErr_SetString(PyExc_ValueError, "the return value must be None");
383 printf_func_error(py_func);
390 PyGILState_Release(gilstate);
393 pyrna_write_set(false);
397 static int bpy_prop_poll_cb(struct PointerRNA *self, PointerRNA candidate, struct PropertyRNA *prop)
400 PyObject *py_candidate;
402 PyObject **py_data = RNA_property_py_data_get(prop);
406 const int is_write_ok = pyrna_write_check();
407 PyGILState_STATE gilstate = PyGILState_Ensure();
409 BLI_assert(self != NULL);
411 py_self = pyrna_struct_as_instance(self);
412 py_candidate = pyrna_struct_as_instance(&candidate);
413 py_func = py_data[BPY_DATA_CB_SLOT_POLL];
416 pyrna_write_set(true);
418 args = PyTuple_New(2);
419 PyTuple_SET_ITEM(args, 0, py_self);
420 PyTuple_SET_ITEM(args, 1, py_candidate);
422 ret = PyObject_CallObject(py_func, args);
427 printf_func_error(py_func);
431 result = PyObject_IsTrue(ret);
435 PyGILState_Release(gilstate);
437 pyrna_write_set(false);
442 static void bpy_prop_boolean_array_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, int *values)
444 PyObject **py_data = RNA_property_py_data_get(prop);
449 PyGILState_STATE gilstate;
451 const bool is_write_ok = pyrna_write_check();
452 int i, len = RNA_property_array_length(ptr, prop);
454 BLI_assert(py_data != NULL);
457 pyrna_write_set(true);
460 use_gil = true; /* !PyC_IsInterpreterActive(); */
463 gilstate = PyGILState_Ensure();
465 py_func = py_data[BPY_DATA_CB_SLOT_GET];
467 args = PyTuple_New(1);
468 self = pyrna_struct_as_instance(ptr);
469 PyTuple_SET_ITEM(args, 0, self);
471 ret = PyObject_CallObject(py_func, args);
476 printf_func_error(py_func);
478 for (i = 0; i < len; ++i)
482 if (PyC_AsArray(values, ret, len, &PyBool_Type, false, "BoolVectorProperty get") == -1) {
483 printf_func_error(py_func);
485 for (i = 0; i < len; ++i)
488 /* PyC_AsArray decrements refcount internally on error */
496 PyGILState_Release(gilstate);
499 pyrna_write_set(false);
503 static void bpy_prop_boolean_array_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, const int *values)
505 PyObject **py_data = RNA_property_py_data_get(prop);
511 PyGILState_STATE gilstate;
513 const bool is_write_ok = pyrna_write_check();
514 int len = RNA_property_array_length(ptr, prop);
516 BLI_assert(py_data != NULL);
519 pyrna_write_set(true);
522 use_gil = true; /* !PyC_IsInterpreterActive(); */
525 gilstate = PyGILState_Ensure();
527 py_func = py_data[BPY_DATA_CB_SLOT_SET];
529 args = PyTuple_New(2);
530 self = pyrna_struct_as_instance(ptr);
531 PyTuple_SET_ITEM(args, 0, self);
533 py_values = PyC_FromArray(values, len, &PyBool_Type, false, "BoolVectorProperty set");
535 printf_func_error(py_func);
538 PyTuple_SET_ITEM(args, 1, py_values);
540 ret = PyObject_CallObject(py_func, args);
545 printf_func_error(py_func);
548 if (ret != Py_None) {
549 PyErr_SetString(PyExc_ValueError, "the return value must be None");
550 printf_func_error(py_func);
557 PyGILState_Release(gilstate);
560 pyrna_write_set(false);
564 static int bpy_prop_int_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop)
566 PyObject **py_data = RNA_property_py_data_get(prop);
571 PyGILState_STATE gilstate;
573 const bool is_write_ok = pyrna_write_check();
576 BLI_assert(py_data != NULL);
579 pyrna_write_set(true);
582 use_gil = true; /* !PyC_IsInterpreterActive(); */
585 gilstate = PyGILState_Ensure();
587 py_func = py_data[BPY_DATA_CB_SLOT_GET];
589 args = PyTuple_New(1);
590 self = pyrna_struct_as_instance(ptr);
591 PyTuple_SET_ITEM(args, 0, self);
593 ret = PyObject_CallObject(py_func, args);
598 printf_func_error(py_func);
602 value = PyLong_AsLong(ret);
604 if (value == -1 && PyErr_Occurred()) {
605 printf_func_error(py_func);
613 PyGILState_Release(gilstate);
616 pyrna_write_set(false);
622 static void bpy_prop_int_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, int value)
624 PyObject **py_data = RNA_property_py_data_get(prop);
629 PyGILState_STATE gilstate;
631 const bool is_write_ok = pyrna_write_check();
633 BLI_assert(py_data != NULL);
636 pyrna_write_set(true);
639 use_gil = true; /* !PyC_IsInterpreterActive(); */
642 gilstate = PyGILState_Ensure();
644 py_func = py_data[BPY_DATA_CB_SLOT_SET];
646 args = PyTuple_New(2);
647 self = pyrna_struct_as_instance(ptr);
648 PyTuple_SET_ITEM(args, 0, self);
650 PyTuple_SET_ITEM(args, 1, PyLong_FromLong(value));
652 ret = PyObject_CallObject(py_func, args);
657 printf_func_error(py_func);
660 if (ret != Py_None) {
661 PyErr_SetString(PyExc_ValueError, "the return value must be None");
662 printf_func_error(py_func);
669 PyGILState_Release(gilstate);
672 pyrna_write_set(false);
676 static void bpy_prop_int_array_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, int *values)
678 PyObject **py_data = RNA_property_py_data_get(prop);
683 PyGILState_STATE gilstate;
685 const bool is_write_ok = pyrna_write_check();
686 int i, len = RNA_property_array_length(ptr, prop);
688 BLI_assert(py_data != NULL);
691 pyrna_write_set(true);
694 use_gil = true; /* !PyC_IsInterpreterActive(); */
697 gilstate = PyGILState_Ensure();
699 py_func = py_data[BPY_DATA_CB_SLOT_GET];
701 args = PyTuple_New(1);
702 self = pyrna_struct_as_instance(ptr);
703 PyTuple_SET_ITEM(args, 0, self);
705 ret = PyObject_CallObject(py_func, args);
710 printf_func_error(py_func);
712 for (i = 0; i < len; ++i)
716 if (PyC_AsArray(values, ret, len, &PyLong_Type, false, "IntVectorProperty get") == -1) {
717 printf_func_error(py_func);
719 for (i = 0; i < len; ++i)
722 /* PyC_AsArray decrements refcount internally on error */
730 PyGILState_Release(gilstate);
733 pyrna_write_set(false);
737 static void bpy_prop_int_array_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, const int *values)
739 PyObject **py_data = RNA_property_py_data_get(prop);
745 PyGILState_STATE gilstate;
747 const bool is_write_ok = pyrna_write_check();
748 int len = RNA_property_array_length(ptr, prop);
750 BLI_assert(py_data != NULL);
753 pyrna_write_set(true);
756 use_gil = true; /* !PyC_IsInterpreterActive(); */
759 gilstate = PyGILState_Ensure();
761 py_func = py_data[BPY_DATA_CB_SLOT_SET];
763 args = PyTuple_New(2);
764 self = pyrna_struct_as_instance(ptr);
765 PyTuple_SET_ITEM(args, 0, self);
767 py_values = PyC_FromArray(values, len, &PyLong_Type, false, "IntVectorProperty set");
769 printf_func_error(py_func);
772 PyTuple_SET_ITEM(args, 1, py_values);
774 ret = PyObject_CallObject(py_func, args);
779 printf_func_error(py_func);
782 if (ret != Py_None) {
783 PyErr_SetString(PyExc_ValueError, "the return value must be None");
784 printf_func_error(py_func);
791 PyGILState_Release(gilstate);
794 pyrna_write_set(false);
798 static float bpy_prop_float_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop)
800 PyObject **py_data = RNA_property_py_data_get(prop);
805 PyGILState_STATE gilstate;
807 const bool is_write_ok = pyrna_write_check();
810 BLI_assert(py_data != NULL);
813 pyrna_write_set(true);
816 use_gil = true; /* !PyC_IsInterpreterActive(); */
819 gilstate = PyGILState_Ensure();
821 py_func = py_data[BPY_DATA_CB_SLOT_GET];
823 args = PyTuple_New(1);
824 self = pyrna_struct_as_instance(ptr);
825 PyTuple_SET_ITEM(args, 0, self);
827 ret = PyObject_CallObject(py_func, args);
832 printf_func_error(py_func);
836 value = PyFloat_AsDouble(ret);
838 if (value == -1.0f && PyErr_Occurred()) {
839 printf_func_error(py_func);
847 PyGILState_Release(gilstate);
850 pyrna_write_set(false);
856 static void bpy_prop_float_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, float value)
858 PyObject **py_data = RNA_property_py_data_get(prop);
863 PyGILState_STATE gilstate;
865 const bool is_write_ok = pyrna_write_check();
867 BLI_assert(py_data != NULL);
870 pyrna_write_set(true);
873 use_gil = true; /* !PyC_IsInterpreterActive(); */
876 gilstate = PyGILState_Ensure();
878 py_func = py_data[BPY_DATA_CB_SLOT_SET];
880 args = PyTuple_New(2);
881 self = pyrna_struct_as_instance(ptr);
882 PyTuple_SET_ITEM(args, 0, self);
884 PyTuple_SET_ITEM(args, 1, PyFloat_FromDouble(value));
886 ret = PyObject_CallObject(py_func, args);
891 printf_func_error(py_func);
894 if (ret != Py_None) {
895 PyErr_SetString(PyExc_ValueError, "the return value must be None");
896 printf_func_error(py_func);
903 PyGILState_Release(gilstate);
906 pyrna_write_set(false);
910 static void bpy_prop_float_array_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, float *values)
912 PyObject **py_data = RNA_property_py_data_get(prop);
917 PyGILState_STATE gilstate;
919 const bool is_write_ok = pyrna_write_check();
920 int i, len = RNA_property_array_length(ptr, prop);
922 BLI_assert(py_data != NULL);
925 pyrna_write_set(true);
928 use_gil = true; /* !PyC_IsInterpreterActive(); */
931 gilstate = PyGILState_Ensure();
933 py_func = py_data[BPY_DATA_CB_SLOT_GET];
935 args = PyTuple_New(1);
936 self = pyrna_struct_as_instance(ptr);
937 PyTuple_SET_ITEM(args, 0, self);
939 ret = PyObject_CallObject(py_func, args);
944 printf_func_error(py_func);
946 for (i = 0; i < len; ++i)
950 if (PyC_AsArray(values, ret, len, &PyFloat_Type, false, "FloatVectorProperty get") == -1) {
951 printf_func_error(py_func);
953 for (i = 0; i < len; ++i)
956 /* PyC_AsArray decrements refcount internally on error */
964 PyGILState_Release(gilstate);
967 pyrna_write_set(false);
971 static void bpy_prop_float_array_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, const float *values)
973 PyObject **py_data = RNA_property_py_data_get(prop);
979 PyGILState_STATE gilstate;
981 const bool is_write_ok = pyrna_write_check();
982 int len = RNA_property_array_length(ptr, prop);
984 BLI_assert(py_data != NULL);
987 pyrna_write_set(true);
990 use_gil = true; /* !PyC_IsInterpreterActive(); */
993 gilstate = PyGILState_Ensure();
995 py_func = py_data[BPY_DATA_CB_SLOT_SET];
997 args = PyTuple_New(2);
998 self = pyrna_struct_as_instance(ptr);
999 PyTuple_SET_ITEM(args, 0, self);
1001 py_values = PyC_FromArray(values, len, &PyFloat_Type, false, "FloatVectorProperty set");
1003 printf_func_error(py_func);
1006 PyTuple_SET_ITEM(args, 1, py_values);
1008 ret = PyObject_CallObject(py_func, args);
1013 printf_func_error(py_func);
1016 if (ret != Py_None) {
1017 PyErr_SetString(PyExc_ValueError, "the return value must be None");
1018 printf_func_error(py_func);
1025 PyGILState_Release(gilstate);
1028 pyrna_write_set(false);
1032 static void bpy_prop_string_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, char *value)
1034 PyObject **py_data = RNA_property_py_data_get(prop);
1039 PyGILState_STATE gilstate;
1041 const bool is_write_ok = pyrna_write_check();
1043 BLI_assert(py_data != NULL);
1046 pyrna_write_set(true);
1049 use_gil = true; /* !PyC_IsInterpreterActive(); */
1052 gilstate = PyGILState_Ensure();
1054 py_func = py_data[BPY_DATA_CB_SLOT_GET];
1056 args = PyTuple_New(1);
1057 self = pyrna_struct_as_instance(ptr);
1058 PyTuple_SET_ITEM(args, 0, self);
1060 ret = PyObject_CallObject(py_func, args);
1065 printf_func_error(py_func);
1068 else if (!PyUnicode_Check(ret)) {
1069 PyErr_Format(PyExc_TypeError,
1070 "return value must be a string, not %.200s",
1071 Py_TYPE(ret)->tp_name);
1072 printf_func_error(py_func);
1078 const char *buffer = _PyUnicode_AsStringAndSize(ret, &length);
1079 memcpy(value, buffer, length + 1);
1084 PyGILState_Release(gilstate);
1087 pyrna_write_set(false);
1091 static int bpy_prop_string_length_cb(struct PointerRNA *ptr, struct PropertyRNA *prop)
1093 PyObject **py_data = RNA_property_py_data_get(prop);
1098 PyGILState_STATE gilstate;
1100 const bool is_write_ok = pyrna_write_check();
1103 BLI_assert(py_data != NULL);
1106 pyrna_write_set(true);
1109 use_gil = true; /* !PyC_IsInterpreterActive(); */
1112 gilstate = PyGILState_Ensure();
1114 py_func = py_data[BPY_DATA_CB_SLOT_GET];
1116 args = PyTuple_New(1);
1117 self = pyrna_struct_as_instance(ptr);
1118 PyTuple_SET_ITEM(args, 0, self);
1120 ret = PyObject_CallObject(py_func, args);
1125 printf_func_error(py_func);
1128 else if (!PyUnicode_Check(ret)) {
1129 PyErr_Format(PyExc_TypeError,
1130 "return value must be a string, not %.200s",
1131 Py_TYPE(ret)->tp_name);
1132 printf_func_error(py_func);
1137 Py_ssize_t length_ssize_t = 0;
1138 _PyUnicode_AsStringAndSize(ret, &length_ssize_t);
1139 length = length_ssize_t;
1144 PyGILState_Release(gilstate);
1147 pyrna_write_set(false);
1153 static void bpy_prop_string_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, const char *value)
1155 PyObject **py_data = RNA_property_py_data_get(prop);
1160 PyGILState_STATE gilstate;
1162 const bool is_write_ok = pyrna_write_check();
1165 BLI_assert(py_data != NULL);
1168 pyrna_write_set(true);
1171 use_gil = true; /* !PyC_IsInterpreterActive(); */
1174 gilstate = PyGILState_Ensure();
1176 py_func = py_data[BPY_DATA_CB_SLOT_SET];
1178 args = PyTuple_New(2);
1179 self = pyrna_struct_as_instance(ptr);
1180 PyTuple_SET_ITEM(args, 0, self);
1182 py_value = PyUnicode_FromString(value);
1184 PyErr_SetString(PyExc_ValueError, "the return value must be a string");
1185 printf_func_error(py_func);
1188 PyTuple_SET_ITEM(args, 1, py_value);
1190 ret = PyObject_CallObject(py_func, args);
1195 printf_func_error(py_func);
1198 if (ret != Py_None) {
1199 PyErr_SetString(PyExc_ValueError, "the return value must be None");
1200 printf_func_error(py_func);
1207 PyGILState_Release(gilstate);
1210 pyrna_write_set(false);
1214 static int bpy_prop_enum_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop)
1216 PyObject **py_data = RNA_property_py_data_get(prop);
1221 PyGILState_STATE gilstate;
1223 const bool is_write_ok = pyrna_write_check();
1226 BLI_assert(py_data != NULL);
1229 pyrna_write_set(true);
1232 use_gil = true; /* !PyC_IsInterpreterActive(); */
1235 gilstate = PyGILState_Ensure();
1237 py_func = py_data[BPY_DATA_CB_SLOT_GET];
1239 args = PyTuple_New(1);
1240 self = pyrna_struct_as_instance(ptr);
1241 PyTuple_SET_ITEM(args, 0, self);
1243 ret = PyObject_CallObject(py_func, args);
1248 printf_func_error(py_func);
1249 value = RNA_property_enum_get_default(ptr, prop);
1252 value = PyLong_AsLong(ret);
1254 if (value == -1 && PyErr_Occurred()) {
1255 printf_func_error(py_func);
1256 value = RNA_property_enum_get_default(ptr, prop);
1263 PyGILState_Release(gilstate);
1266 pyrna_write_set(false);
1272 static void bpy_prop_enum_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop, int value)
1274 PyObject **py_data = RNA_property_py_data_get(prop);
1279 PyGILState_STATE gilstate;
1281 const bool is_write_ok = pyrna_write_check();
1283 BLI_assert(py_data != NULL);
1286 pyrna_write_set(true);
1289 use_gil = true; /* !PyC_IsInterpreterActive(); */
1292 gilstate = PyGILState_Ensure();
1294 py_func = py_data[BPY_DATA_CB_SLOT_SET];
1296 args = PyTuple_New(2);
1297 self = pyrna_struct_as_instance(ptr);
1298 PyTuple_SET_ITEM(args, 0, self);
1300 PyTuple_SET_ITEM(args, 1, PyLong_FromLong(value));
1302 ret = PyObject_CallObject(py_func, args);
1307 printf_func_error(py_func);
1310 if (ret != Py_None) {
1311 PyErr_SetString(PyExc_ValueError, "the return value must be None");
1312 printf_func_error(py_func);
1319 PyGILState_Release(gilstate);
1322 pyrna_write_set(false);
1326 /* utility function we need for parsing int's in an if statement */
1327 static int py_long_as_int(PyObject *py_long, int *r_int)
1329 if (PyLong_CheckExact(py_long)) {
1330 *r_int = (int)PyLong_AS_LONG(py_long);
1339 /* copies orig to buf, then sets orig to buf, returns copy length */
1340 static size_t strswapbufcpy(char *buf, const char **orig)
1342 const char *src = *orig;
1346 while ((*dst = *src)) { dst++; src++; i++; }
1347 return i + 1; /* include '\0' */
1351 static int icon_id_from_name(const char *name)
1353 EnumPropertyItem *item;
1357 for (item = rna_enum_icon_items, id = 0; item->identifier; item++, id++) {
1358 if (STREQ(item->name, name)) {
1367 static EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, PyObject *def, int *defvalue, const bool is_enum_flag)
1369 EnumPropertyItem *items;
1371 const Py_ssize_t seq_len = PySequence_Fast_GET_SIZE(seq_fast);
1372 PyObject **seq_fast_items = PySequence_Fast_ITEMS(seq_fast);
1373 Py_ssize_t totbuf = 0;
1376 const char *def_cmp = NULL;
1379 if (seq_len > RNA_ENUM_BITFLAG_SIZE) {
1380 PyErr_SetString(PyExc_TypeError,
1381 "EnumProperty(...): maximum "
1382 STRINGIFY(RNA_ENUM_BITFLAG_SIZE)
1383 " members for a ENUM_FLAG type property");
1386 if (def && !PySet_Check(def)) {
1387 PyErr_Format(PyExc_TypeError,
1388 "EnumProperty(...): default option must be a 'set' "
1389 "type when ENUM_FLAG is enabled, not a '%.200s'",
1390 Py_TYPE(def)->tp_name);
1396 def_cmp = _PyUnicode_AsString(def);
1397 if (def_cmp == NULL) {
1398 PyErr_Format(PyExc_TypeError,
1399 "EnumProperty(...): default option must be a 'str' "
1400 "type when ENUM_FLAG is disabled, not a '%.200s'",
1401 Py_TYPE(def)->tp_name);
1410 items = MEM_callocN(sizeof(EnumPropertyItem) * (seq_len + 1), "enum_items_from_py1");
1412 for (i = 0; i < seq_len; i++) {
1413 EnumPropertyItem tmp = {0, "", 0, "", ""};
1414 const char *tmp_icon = NULL;
1415 Py_ssize_t item_size;
1416 Py_ssize_t id_str_size;
1417 Py_ssize_t name_str_size;
1418 Py_ssize_t desc_str_size;
1420 item = seq_fast_items[i];
1422 if ((PyTuple_CheckExact(item)) &&
1423 (item_size = PyTuple_GET_SIZE(item)) &&
1424 (item_size >= 3 && item_size <= 5) &&
1425 (tmp.identifier = _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 0), &id_str_size)) &&
1426 (tmp.name = _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 1), &name_str_size)) &&
1427 (tmp.description = _PyUnicode_AsStringAndSize(PyTuple_GET_ITEM(item, 2), &desc_str_size)) &&
1428 /* TODO, number isn't ensured to be unique from the script author */
1429 (item_size != 4 || py_long_as_int(PyTuple_GET_ITEM(item, 3), &tmp.value) != -1) &&
1430 (item_size != 5 || ((py_long_as_int(PyTuple_GET_ITEM(item, 3), &tmp.icon) != -1 ||
1431 (tmp_icon = _PyUnicode_AsString(PyTuple_GET_ITEM(item, 3)))) &&
1432 py_long_as_int(PyTuple_GET_ITEM(item, 4), &tmp.value) != -1)))
1435 if (item_size < 4) {
1439 if (def && PySet_Contains(def, PyTuple_GET_ITEM(item, 0))) {
1440 *defvalue |= tmp.value;
1445 if (item_size < 4) {
1449 if (def && def_used == 0 && STREQ(def_cmp, tmp.identifier)) {
1450 *defvalue = tmp.value;
1451 def_used++; /* only ever 1 */
1456 tmp.icon = icon_id_from_name(tmp_icon);
1460 /* calculate combine string length */
1461 totbuf += id_str_size + name_str_size + desc_str_size + 3; /* 3 is for '\0's */
1465 PyErr_SetString(PyExc_TypeError,
1466 "EnumProperty(...): expected a tuple containing "
1467 "(identifier, name, description) and optionally an "
1468 "icon name and unique number");
1475 /* strict check that all set members were used */
1476 if (def && def_used != PySet_GET_SIZE(def)) {
1479 PyErr_Format(PyExc_TypeError,
1480 "EnumProperty(..., default={...}): set has %d unused member(s)",
1481 PySet_GET_SIZE(def) - def_used);
1486 if (def && def_used == 0) {
1489 PyErr_Format(PyExc_TypeError,
1490 "EnumProperty(..., default=\'%s\'): not found in enum members",
1496 /* disabled duplicating strings because the array can still be freed and
1497 * the strings from it referenced, for now we can't support dynamically
1498 * created strings from python. */
1500 /* this would all work perfectly _but_ the python strings may be freed
1501 * immediately after use, so we need to duplicate them, ugh.
1502 * annoying because it works most of the time without this. */
1504 EnumPropertyItem *items_dup = MEM_mallocN((sizeof(EnumPropertyItem) * (seq_len + 1)) + (sizeof(char) * totbuf),
1505 "enum_items_from_py2");
1506 EnumPropertyItem *items_ptr = items_dup;
1507 char *buf = ((char *)items_dup) + (sizeof(EnumPropertyItem) * (seq_len + 1));
1508 memcpy(items_dup, items, sizeof(EnumPropertyItem) * (seq_len + 1));
1509 for (i = 0; i < seq_len; i++, items_ptr++) {
1510 buf += strswapbufcpy(buf, &items_ptr->identifier);
1511 buf += strswapbufcpy(buf, &items_ptr->name);
1512 buf += strswapbufcpy(buf, &items_ptr->description);
1517 /* end string duplication */
1523 static EnumPropertyItem *bpy_prop_enum_itemf_cb(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, bool *r_free)
1525 PyGILState_STATE gilstate;
1527 PyObject *py_func = RNA_property_enum_py_data_get(prop);
1528 PyObject *self = NULL;
1530 PyObject *items; /* returned from the function call */
1532 EnumPropertyItem *eitems = NULL;
1536 bpy_context_set(C, &gilstate);
1539 gilstate = PyGILState_Ensure();
1542 args = PyTuple_New(2);
1543 self = pyrna_struct_as_instance(ptr);
1544 PyTuple_SET_ITEM(args, 0, self);
1546 /* now get the context */
1548 PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
1549 Py_INCREF(bpy_context_module);
1552 PyTuple_SET_ITEM(args, 1, Py_None);
1556 items = PyObject_CallObject(py_func, args);
1560 if (items == NULL) {
1564 PyObject *items_fast;
1565 int defvalue_dummy = 0;
1567 if (!(items_fast = PySequence_Fast(items, "EnumProperty(...): "
1568 "return value from the callback was not a sequence")))
1573 eitems = enum_items_from_py(items_fast, NULL, &defvalue_dummy,
1574 (RNA_property_flag(prop) & PROP_ENUM_FLAG) != 0);
1576 Py_DECREF(items_fast);
1586 if (err != -1) { /* worked */
1590 printf_func_error(py_func);
1592 eitems = DummyRNA_NULL_items;
1596 bpy_context_clear(C, &gilstate);
1599 PyGILState_Release(gilstate);
1605 static int bpy_prop_callback_check(PyObject *py_func, const char *keyword, int argcount)
1607 if (py_func && py_func != Py_None) {
1608 if (!PyFunction_Check(py_func)) {
1609 PyErr_Format(PyExc_TypeError,
1610 "%s keyword: expected a function type, not a %.200s",
1611 keyword, Py_TYPE(py_func)->tp_name);
1615 PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(py_func);
1616 if (f_code->co_argcount != argcount) {
1617 PyErr_Format(PyExc_TypeError,
1618 "%s keyword: expected a function taking %d arguments, not %d",
1619 keyword, argcount, f_code->co_argcount);
1628 static PyObject **bpy_prop_py_data_get(struct PropertyRNA *prop)
1630 PyObject **py_data = RNA_property_py_data_get(prop);
1632 py_data = MEM_callocN(sizeof(PyObject *) * BPY_DATA_CB_SLOT_SIZE, __func__);
1633 RNA_def_py_data(prop, py_data);
1638 static void bpy_prop_callback_assign_update(struct PropertyRNA *prop, PyObject *update_cb)
1640 /* assume this is already checked for type and arg length */
1641 if (update_cb && update_cb != Py_None) {
1642 PyObject **py_data = bpy_prop_py_data_get(prop);
1644 RNA_def_property_update_runtime(prop, (void *)bpy_prop_update_cb);
1645 py_data[BPY_DATA_CB_SLOT_UPDATE] = update_cb;
1647 RNA_def_property_flag(prop, PROP_CONTEXT_PROPERTY_UPDATE);
1651 static void bpy_prop_callback_assign_pointer(struct PropertyRNA *prop, PyObject *poll_cb)
1653 if (poll_cb && poll_cb != Py_None) {
1654 PyObject **py_data = bpy_prop_py_data_get(prop);
1656 RNA_def_property_poll_runtime(prop, (void *) bpy_prop_poll_cb);
1657 py_data[BPY_DATA_CB_SLOT_POLL] = poll_cb;
1661 static void bpy_prop_callback_assign_boolean(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1663 BooleanPropertyGetFunc rna_get_cb = NULL;
1664 BooleanPropertySetFunc rna_set_cb = NULL;
1666 if (get_cb && get_cb != Py_None) {
1667 PyObject **py_data = bpy_prop_py_data_get(prop);
1669 rna_get_cb = bpy_prop_boolean_get_cb;
1670 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1673 if (set_cb && set_cb != Py_None) {
1674 PyObject **py_data = bpy_prop_py_data_get(prop);
1676 rna_set_cb = bpy_prop_boolean_set_cb;
1677 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1680 RNA_def_property_boolean_funcs_runtime(prop, rna_get_cb, rna_set_cb);
1683 static void bpy_prop_callback_assign_boolean_array(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1685 BooleanArrayPropertyGetFunc rna_get_cb = NULL;
1686 BooleanArrayPropertySetFunc rna_set_cb = NULL;
1688 if (get_cb && get_cb != Py_None) {
1689 PyObject **py_data = bpy_prop_py_data_get(prop);
1691 rna_get_cb = bpy_prop_boolean_array_get_cb;
1692 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1695 if (set_cb && set_cb != Py_None) {
1696 PyObject **py_data = bpy_prop_py_data_get(prop);
1698 rna_set_cb = bpy_prop_boolean_array_set_cb;
1699 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1702 RNA_def_property_boolean_array_funcs_runtime(prop, rna_get_cb, rna_set_cb);
1705 static void bpy_prop_callback_assign_int(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1707 IntPropertyGetFunc rna_get_cb = NULL;
1708 IntPropertySetFunc rna_set_cb = NULL;
1710 if (get_cb && get_cb != Py_None) {
1711 PyObject **py_data = bpy_prop_py_data_get(prop);
1713 rna_get_cb = bpy_prop_int_get_cb;
1714 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1717 if (set_cb && set_cb != Py_None) {
1718 PyObject **py_data = bpy_prop_py_data_get(prop);
1720 rna_set_cb = bpy_prop_int_set_cb;
1721 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1724 RNA_def_property_int_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1727 static void bpy_prop_callback_assign_int_array(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1729 IntArrayPropertyGetFunc rna_get_cb = NULL;
1730 IntArrayPropertySetFunc rna_set_cb = NULL;
1732 if (get_cb && get_cb != Py_None) {
1733 PyObject **py_data = bpy_prop_py_data_get(prop);
1735 rna_get_cb = bpy_prop_int_array_get_cb;
1736 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1739 if (set_cb && set_cb != Py_None) {
1740 PyObject **py_data = bpy_prop_py_data_get(prop);
1742 rna_set_cb = bpy_prop_int_array_set_cb;
1743 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1746 RNA_def_property_int_array_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1749 static void bpy_prop_callback_assign_float(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1751 FloatPropertyGetFunc rna_get_cb = NULL;
1752 FloatPropertySetFunc rna_set_cb = NULL;
1754 if (get_cb && get_cb != Py_None) {
1755 PyObject **py_data = bpy_prop_py_data_get(prop);
1757 rna_get_cb = bpy_prop_float_get_cb;
1758 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1761 if (set_cb && set_cb != Py_None) {
1762 PyObject **py_data = bpy_prop_py_data_get(prop);
1764 rna_set_cb = bpy_prop_float_set_cb;
1765 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1768 RNA_def_property_float_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1771 static void bpy_prop_callback_assign_float_array(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1773 FloatArrayPropertyGetFunc rna_get_cb = NULL;
1774 FloatArrayPropertySetFunc rna_set_cb = NULL;
1776 if (get_cb && get_cb != Py_None) {
1777 PyObject **py_data = bpy_prop_py_data_get(prop);
1779 rna_get_cb = bpy_prop_float_array_get_cb;
1780 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1783 if (set_cb && set_cb != Py_None) {
1784 PyObject **py_data = bpy_prop_py_data_get(prop);
1786 rna_set_cb = bpy_prop_float_array_set_cb;
1787 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1790 RNA_def_property_float_array_funcs_runtime(prop, rna_get_cb, rna_set_cb, NULL);
1793 static void bpy_prop_callback_assign_string(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb)
1795 StringPropertyGetFunc rna_get_cb = NULL;
1796 StringPropertyLengthFunc rna_length_cb = NULL;
1797 StringPropertySetFunc rna_set_cb = NULL;
1799 if (get_cb && get_cb != Py_None) {
1800 PyObject **py_data = bpy_prop_py_data_get(prop);
1802 rna_get_cb = bpy_prop_string_get_cb;
1803 rna_length_cb = bpy_prop_string_length_cb;
1804 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1807 if (set_cb && set_cb != Py_None) {
1808 PyObject **py_data = bpy_prop_py_data_get(prop);
1810 rna_set_cb = bpy_prop_string_set_cb;
1811 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1814 RNA_def_property_string_funcs_runtime(prop, rna_get_cb, rna_length_cb, rna_set_cb);
1817 static void bpy_prop_callback_assign_enum(struct PropertyRNA *prop, PyObject *get_cb, PyObject *set_cb, PyObject *itemf_cb)
1819 EnumPropertyGetFunc rna_get_cb = NULL;
1820 EnumPropertyItemFunc rna_itemf_cb = NULL;
1821 EnumPropertySetFunc rna_set_cb = NULL;
1823 if (get_cb && get_cb != Py_None) {
1824 PyObject **py_data = bpy_prop_py_data_get(prop);
1826 rna_get_cb = bpy_prop_enum_get_cb;
1827 py_data[BPY_DATA_CB_SLOT_GET] = get_cb;
1830 if (set_cb && set_cb != Py_None) {
1831 PyObject **py_data = bpy_prop_py_data_get(prop);
1833 rna_set_cb = bpy_prop_enum_set_cb;
1834 py_data[BPY_DATA_CB_SLOT_SET] = set_cb;
1837 if (itemf_cb && itemf_cb != Py_None) {
1838 rna_itemf_cb = bpy_prop_enum_itemf_cb;
1839 RNA_def_property_enum_py_data(prop, (void *)itemf_cb);
1841 /* watch out!, if a user is tricky they can probably crash blender
1842 * if they manage to free the callback, take care! */
1843 /* Py_INCREF(itemf_cb); */
1846 RNA_def_property_enum_funcs_runtime(prop, rna_get_cb, rna_set_cb, rna_itemf_cb);
1849 /* this define runs at the start of each function and deals with
1850 * returning a deferred property (to be registered later) */
1851 #define BPY_PROPDEF_HEAD(_func) \
1852 if (PyTuple_GET_SIZE(args) == 1) { \
1854 self = PyTuple_GET_ITEM(args, 0); \
1855 args = PyTuple_New(0); \
1856 ret = BPy_##_func(self, args, kw); \
1860 else if (PyTuple_GET_SIZE(args) > 1) { \
1861 PyErr_SetString(PyExc_ValueError, "all args must be keywords"); \
1864 srna = srna_from_self(self, #_func"(...):"); \
1865 if (srna == NULL) { \
1866 if (PyErr_Occurred()) \
1868 return bpy_prop_deferred_return(pymeth_##_func, kw); \
1871 /* terse macros for error checks shared between all funcs cant use function
1872 * calls because of static strings passed to pyrna_set_to_enum_bitfield */
1873 #define BPY_PROPDEF_CHECK(_func, _property_flag_items) \
1874 if (UNLIKELY(id_len >= MAX_IDPROP_NAME)) { \
1875 PyErr_Format(PyExc_TypeError, \
1876 #_func"(): '%.200s' too long, max length is %d", \
1877 id, MAX_IDPROP_NAME - 1); \
1880 if (UNLIKELY(RNA_def_property_free_identifier(srna, id) == -1)) { \
1881 PyErr_Format(PyExc_TypeError, \
1882 #_func"(): '%s' is defined as a non-dynamic type", \
1886 if (UNLIKELY(pyopts && pyrna_set_to_enum_bitfield(_property_flag_items, \
1889 #_func"(options={ ...}):"))) \
1894 #define BPY_PROPDEF_SUBTYPE_CHECK(_func, _property_flag_items, _subtype) \
1895 BPY_PROPDEF_CHECK(_func, _property_flag_items); \
1896 if (UNLIKELY(pysubtype && RNA_enum_value_from_id(_subtype, \
1900 const char *enum_str = BPy_enum_as_string(_subtype); \
1901 PyErr_Format(PyExc_TypeError, \
1902 #_func"(subtype='%s'): " \
1903 "subtype not found in (%s)", \
1904 pysubtype, enum_str); \
1905 MEM_freeN((void *)enum_str); \
1910 #define BPY_PROPDEF_NAME_DOC \
1911 " :arg name: Name used in the user interface.\n" \
1912 " :type name: string\n" \
1914 #define BPY_PROPDEF_DESC_DOC \
1915 " :arg description: Text used for the tooltip and api documentation.\n" \
1916 " :type description: string\n" \
1918 #define BPY_PROPDEF_UNIT_DOC \
1919 " :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION'].\n" \
1920 " :type unit: string\n" \
1922 #define BPY_PROPDEF_NUM_MIN_DOC \
1923 " :arg min: Hard minimum, trying to assign a value below will silently assign this minimum instead.\n" \
1925 #define BPY_PROPDEF_NUM_MAX_DOC \
1926 " :arg max: Hard maximum, trying to assign a value above will silently assign this maximum instead.\n" \
1928 #define BPY_PROPDEF_NUM_SOFTMIN_DOC \
1929 " :arg soft_min: Soft minimum (>= *min*), user won't be able to drag the widget below this value in the UI.\n" \
1931 #define BPY_PROPDEF_NUM_SOFTMAX_DOC \
1932 " :arg soft_max: Soft maximum (<= *max*), user won't be able to drag the widget above this value in the UI.\n" \
1934 #define BPY_PROPDEF_VECSIZE_DOC \
1935 " :arg size: Vector dimensions in [1, " STRINGIFY(PYRNA_STACK_ARRAY) "].\n" \
1936 " :type size: int\n" \
1938 #define BPY_PROPDEF_INT_STEP_DOC \
1939 " :arg step: Step of increment/decrement in UI, in [1, 100], defaults to 1 (WARNING: unused currently!).\n" \
1940 " :type step: int\n" \
1942 #define BPY_PROPDEF_FLOAT_STEP_DOC \
1943 " :arg step: Step of increment/decrement in UI, in [1, 100], defaults to 3 (WARNING: actual value is /100).\n" \
1944 " :type step: int\n" \
1946 #define BPY_PROPDEF_FLOAT_PREC_DOC \
1947 " :arg precision: Maximum number of decimal digits to display, in [0, 6].\n" \
1948 " :type precision: int\n" \
1950 #define BPY_PROPDEF_UPDATE_DOC \
1951 " :arg update: Function to be called when this value is modified,\n" \
1952 " This function must take 2 values (self, context) and return None.\n" \
1953 " *Warning* there are no safety checks to avoid infinite recursion.\n" \
1954 " :type update: function\n" \
1956 #define BPY_PROPDEF_GET_DOC \
1957 " :arg get: Function to be called when this value is 'read',\n" \
1958 " This function must take 1 value (self) and return the value of the property.\n" \
1959 " :type get: function\n" \
1961 #define BPY_PROPDEF_SET_DOC \
1962 " :arg set: Function to be called when this value is 'written',\n" \
1963 " This function must take 2 values (self, value) and return None.\n" \
1964 " :type set: function\n" \
1966 #define BPY_PROPDEF_TYPE_DOC \
1967 " :arg type: A subclass of :class:`bpy.types.PropertyGroup` or :class:`bpy.types.ID`.\n" \
1968 " :type type: class\n" \
1971 static int bpy_struct_id_used(StructRNA *srna, char *identifier)
1974 RNA_pointer_create(NULL, srna, NULL, &ptr);
1975 return (RNA_struct_find_property(&ptr, identifier) != NULL);
1980 /* Function that sets RNA, NOTE - self is NULL when called from python,
1981 * but being abused from C so we can pass the srna along.
1982 * This isn't incorrect since its a python object - but be careful */
1983 PyDoc_STRVAR(BPy_BoolProperty_doc,
1984 ".. function:: BoolProperty(name=\"\", "
1985 "description=\"\", "
1987 "options={'ANIMATABLE'}, "
1993 " Returns a new boolean property definition.\n"
1995 BPY_PROPDEF_NAME_DOC
1996 BPY_PROPDEF_DESC_DOC
1997 BPY_PROPDEF_OPTIONS_DOC
1998 BPY_PROPDEF_SUBTYPE_NUMBER_DOC
1999 BPY_PROPDEF_UPDATE_DOC
2003 static PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
2007 BPY_PROPDEF_HEAD(BoolProperty);
2010 static const char *kwlist[] = {"attr", "name", "description", "default",
2011 "options", "subtype", "update", "get", "set", NULL};
2012 const char *id = NULL, *name = NULL, *description = "";
2016 PyObject *pyopts = NULL;
2018 const char *pysubtype = NULL;
2019 int subtype = PROP_NONE;
2020 PyObject *update_cb = NULL;
2021 PyObject *get_cb = NULL;
2022 PyObject *set_cb = NULL;
2024 if (!PyArg_ParseTupleAndKeywords(args, kw,
2025 "s#|ssO&O!sOOO:BoolProperty",
2026 (char **)kwlist, &id, &id_len,
2027 &name, &description, PyC_ParseBool, &def,
2028 &PySet_Type, &pyopts, &pysubtype,
2029 &update_cb, &get_cb, &set_cb))
2034 BPY_PROPDEF_SUBTYPE_CHECK(BoolProperty, property_flag_items, property_subtype_number_items);
2036 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2039 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2042 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2046 prop = RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
2047 RNA_def_property_boolean_default(prop, def);
2048 RNA_def_property_ui_text(prop, name ? name : id, description);
2051 bpy_prop_assign_flag(prop, opts);
2053 bpy_prop_callback_assign_update(prop, update_cb);
2054 bpy_prop_callback_assign_boolean(prop, get_cb, set_cb);
2055 RNA_def_property_duplicate_pointers(srna, prop);
2061 PyDoc_STRVAR(BPy_BoolVectorProperty_doc,
2062 ".. function:: BoolVectorProperty(name=\"\", "
2063 "description=\"\", "
2064 "default=(False, False, False), "
2065 "options={'ANIMATABLE'}, "
2072 " Returns a new vector boolean property definition.\n"
2074 BPY_PROPDEF_NAME_DOC
2075 BPY_PROPDEF_DESC_DOC
2076 " :arg default: sequence of booleans the length of *size*.\n"
2077 " :type default: sequence\n"
2078 BPY_PROPDEF_OPTIONS_DOC
2079 BPY_PROPDEF_SUBTYPE_ARRAY_DOC
2080 BPY_PROPDEF_VECSIZE_DOC
2081 BPY_PROPDEF_UPDATE_DOC
2085 static PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
2089 BPY_PROPDEF_HEAD(BoolVectorProperty);
2092 static const char *kwlist[] = {"attr", "name", "description", "default",
2093 "options", "subtype", "size", "update", "get", "set", NULL};
2094 const char *id = NULL, *name = NULL, *description = "";
2096 int def[PYRNA_STACK_ARRAY] = {0};
2099 PyObject *pydef = NULL;
2100 PyObject *pyopts = NULL;
2102 const char *pysubtype = NULL;
2103 int subtype = PROP_NONE;
2104 PyObject *update_cb = NULL;
2105 PyObject *get_cb = NULL;
2106 PyObject *set_cb = NULL;
2108 if (!PyArg_ParseTupleAndKeywords(args, kw,
2109 "s#|ssOO!siOOO:BoolVectorProperty",
2110 (char **)kwlist, &id, &id_len,
2111 &name, &description, &pydef,
2112 &PySet_Type, &pyopts, &pysubtype, &size,
2113 &update_cb, &get_cb, &set_cb))
2118 BPY_PROPDEF_SUBTYPE_CHECK(BoolVectorProperty, property_flag_items, property_subtype_array_items);
2120 if (size < 1 || size > PYRNA_STACK_ARRAY) {
2121 PyErr_Format(PyExc_TypeError,
2122 "BoolVectorProperty(size=%d): size must be between 0 and "
2123 STRINGIFY(PYRNA_STACK_ARRAY), size);
2127 if (pydef && PyC_AsArray(def, pydef, size, &PyBool_Type, false, "BoolVectorProperty(default=sequence)") == -1)
2130 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2133 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2136 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2140 // prop = RNA_def_boolean_array(srna, id, size, pydef ? def:NULL, name ? name : id, description);
2141 prop = RNA_def_property(srna, id, PROP_BOOLEAN, subtype);
2142 RNA_def_property_array(prop, size);
2143 if (pydef) RNA_def_property_boolean_array_default(prop, def);
2144 RNA_def_property_ui_text(prop, name ? name : id, description);
2147 bpy_prop_assign_flag(prop, opts);
2149 bpy_prop_callback_assign_update(prop, update_cb);
2150 bpy_prop_callback_assign_boolean_array(prop, get_cb, set_cb);
2151 RNA_def_property_duplicate_pointers(srna, prop);
2157 PyDoc_STRVAR(BPy_IntProperty_doc,
2158 ".. function:: IntProperty(name=\"\", "
2159 "description=\"\", "
2161 "min=-2**31, max=2**31-1, "
2162 "soft_min=-2**31, soft_max=2**31-1, "
2164 "options={'ANIMATABLE'}, "
2170 " Returns a new int property definition.\n"
2172 BPY_PROPDEF_NAME_DOC
2173 BPY_PROPDEF_DESC_DOC
2174 BPY_PROPDEF_NUM_MIN_DOC
2176 BPY_PROPDEF_NUM_MAX_DOC
2178 BPY_PROPDEF_NUM_SOFTMAX_DOC
2179 " :type soft_min: int\n"
2180 BPY_PROPDEF_NUM_SOFTMIN_DOC
2181 " :type soft_max: int\n"
2182 BPY_PROPDEF_INT_STEP_DOC
2183 BPY_PROPDEF_OPTIONS_DOC
2184 BPY_PROPDEF_SUBTYPE_NUMBER_DOC
2185 BPY_PROPDEF_UPDATE_DOC
2189 static PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
2193 BPY_PROPDEF_HEAD(IntProperty);
2196 static const char *kwlist[] = {"attr", "name", "description", "default",
2197 "min", "max", "soft_min", "soft_max",
2198 "step", "options", "subtype", "update", "get", "set", NULL};
2199 const char *id = NULL, *name = NULL, *description = "";
2201 int min = INT_MIN, max = INT_MAX, soft_min = INT_MIN, soft_max = INT_MAX, step = 1, def = 0;
2203 PyObject *pyopts = NULL;
2205 const char *pysubtype = NULL;
2206 int subtype = PROP_NONE;
2207 PyObject *update_cb = NULL;
2208 PyObject *get_cb = NULL;
2209 PyObject *set_cb = NULL;
2211 if (!PyArg_ParseTupleAndKeywords(args, kw,
2212 "s#|ssiiiiiiO!sOOO:IntProperty",
2213 (char **)kwlist, &id, &id_len,
2214 &name, &description, &def,
2215 &min, &max, &soft_min, &soft_max,
2216 &step, &PySet_Type, &pyopts, &pysubtype,
2217 &update_cb, &get_cb, &set_cb))
2222 BPY_PROPDEF_SUBTYPE_CHECK(IntProperty, property_flag_items, property_subtype_number_items);
2224 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2227 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2230 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2234 prop = RNA_def_property(srna, id, PROP_INT, subtype);
2235 RNA_def_property_int_default(prop, def);
2236 RNA_def_property_ui_text(prop, name ? name : id, description);
2237 RNA_def_property_range(prop, min, max);
2238 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
2241 bpy_prop_assign_flag(prop, opts);
2243 bpy_prop_callback_assign_update(prop, update_cb);
2244 bpy_prop_callback_assign_int(prop, get_cb, set_cb);
2245 RNA_def_property_duplicate_pointers(srna, prop);
2250 PyDoc_STRVAR(BPy_IntVectorProperty_doc,
2251 ".. function:: IntVectorProperty(name=\"\", "
2252 "description=\"\", "
2253 "default=(0, 0, 0), min=-2**31, max=2**31-1, "
2255 "soft_max=2**31-1, "
2257 "options={'ANIMATABLE'}, "
2264 " Returns a new vector int property definition.\n"
2266 BPY_PROPDEF_NAME_DOC
2267 BPY_PROPDEF_DESC_DOC
2268 " :arg default: sequence of ints the length of *size*.\n"
2269 " :type default: sequence\n"
2270 BPY_PROPDEF_NUM_MIN_DOC
2272 BPY_PROPDEF_NUM_MAX_DOC
2274 BPY_PROPDEF_NUM_SOFTMIN_DOC
2275 " :type soft_min: int\n"
2276 BPY_PROPDEF_NUM_SOFTMAX_DOC
2277 " :type soft_max: int\n"
2278 BPY_PROPDEF_INT_STEP_DOC
2279 BPY_PROPDEF_OPTIONS_DOC
2280 BPY_PROPDEF_SUBTYPE_ARRAY_DOC
2281 BPY_PROPDEF_VECSIZE_DOC
2282 BPY_PROPDEF_UPDATE_DOC
2286 static PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
2290 BPY_PROPDEF_HEAD(IntVectorProperty);
2293 static const char *kwlist[] = {"attr", "name", "description", "default",
2294 "min", "max", "soft_min", "soft_max",
2295 "step", "options", "subtype", "size", "update", "get", "set", NULL};
2296 const char *id = NULL, *name = NULL, *description = "";
2298 int min = INT_MIN, max = INT_MAX, soft_min = INT_MIN, soft_max = INT_MAX, step = 1;
2299 int def[PYRNA_STACK_ARRAY] = {0};
2302 PyObject *pydef = NULL;
2303 PyObject *pyopts = NULL;
2305 const char *pysubtype = NULL;
2306 int subtype = PROP_NONE;
2307 PyObject *update_cb = NULL;
2308 PyObject *get_cb = NULL;
2309 PyObject *set_cb = NULL;
2311 if (!PyArg_ParseTupleAndKeywords(args, kw,
2312 "s#|ssOiiiiiO!siOOO:IntVectorProperty",
2313 (char **)kwlist, &id, &id_len,
2314 &name, &description, &pydef,
2315 &min, &max, &soft_min, &soft_max,
2316 &step, &PySet_Type, &pyopts,
2318 &update_cb, &get_cb, &set_cb))
2323 BPY_PROPDEF_SUBTYPE_CHECK(IntVectorProperty, property_flag_items, property_subtype_array_items);
2325 if (size < 1 || size > PYRNA_STACK_ARRAY) {
2326 PyErr_Format(PyExc_TypeError,
2327 "IntVectorProperty(size=%d): size must be between 0 and "
2328 STRINGIFY(PYRNA_STACK_ARRAY), size);
2332 if (pydef && PyC_AsArray(def, pydef, size, &PyLong_Type, false, "IntVectorProperty(default=sequence)") == -1)
2335 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2338 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2341 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2345 prop = RNA_def_property(srna, id, PROP_INT, subtype);
2346 RNA_def_property_array(prop, size);
2347 if (pydef) RNA_def_property_int_array_default(prop, def);
2348 RNA_def_property_range(prop, min, max);
2349 RNA_def_property_ui_text(prop, name ? name : id, description);
2350 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, 3);
2353 bpy_prop_assign_flag(prop, opts);
2355 bpy_prop_callback_assign_update(prop, update_cb);
2356 bpy_prop_callback_assign_int_array(prop, get_cb, set_cb);
2357 RNA_def_property_duplicate_pointers(srna, prop);
2363 PyDoc_STRVAR(BPy_FloatProperty_doc,
2364 ".. function:: FloatProperty(name=\"\", "
2365 "description=\"\", "
2367 "min=sys.float_info.min, max=sys.float_info.max, "
2368 "soft_min=sys.float_info.min, soft_max=sys.float_info.max, "
2371 "options={'ANIMATABLE'}, "
2378 " Returns a new float property definition.\n"
2380 BPY_PROPDEF_NAME_DOC
2381 BPY_PROPDEF_DESC_DOC
2382 BPY_PROPDEF_NUM_MIN_DOC
2383 " :type min: float\n"
2384 BPY_PROPDEF_NUM_MAX_DOC
2385 " :type max: float\n"
2386 BPY_PROPDEF_NUM_SOFTMIN_DOC
2387 " :type soft_min: float\n"
2388 BPY_PROPDEF_NUM_SOFTMAX_DOC
2389 " :type soft_max: float\n"
2390 BPY_PROPDEF_FLOAT_STEP_DOC
2391 BPY_PROPDEF_FLOAT_PREC_DOC
2392 BPY_PROPDEF_OPTIONS_DOC
2393 BPY_PROPDEF_SUBTYPE_NUMBER_DOC
2394 BPY_PROPDEF_UNIT_DOC
2395 BPY_PROPDEF_UPDATE_DOC
2399 static PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
2403 BPY_PROPDEF_HEAD(FloatProperty);
2406 static const char *kwlist[] = {"attr", "name", "description", "default",
2407 "min", "max", "soft_min", "soft_max",
2408 "step", "precision", "options", "subtype",
2409 "unit", "update", "get", "set", NULL};
2410 const char *id = NULL, *name = NULL, *description = "";
2412 float min = -FLT_MAX, max = FLT_MAX, soft_min = -FLT_MAX, soft_max = FLT_MAX, step = 3, def = 0.0f;
2415 PyObject *pyopts = NULL;
2417 const char *pysubtype = NULL;
2418 int subtype = PROP_NONE;
2419 const char *pyunit = NULL;
2420 int unit = PROP_UNIT_NONE;
2421 PyObject *update_cb = NULL;
2422 PyObject *get_cb = NULL;
2423 PyObject *set_cb = NULL;
2425 if (!PyArg_ParseTupleAndKeywords(args, kw,
2426 "s#|ssffffffiO!ssOOO:FloatProperty",
2427 (char **)kwlist, &id, &id_len,
2428 &name, &description, &def,
2429 &min, &max, &soft_min, &soft_max,
2430 &step, &precision, &PySet_Type,
2431 &pyopts, &pysubtype, &pyunit,
2432 &update_cb, &get_cb, &set_cb))
2437 BPY_PROPDEF_SUBTYPE_CHECK(FloatProperty, property_flag_items, property_subtype_number_items);
2439 if (pyunit && RNA_enum_value_from_id(rna_enum_property_unit_items, pyunit, &unit) == 0) {
2440 PyErr_Format(PyExc_TypeError, "FloatProperty(unit='%s'): invalid unit", pyunit);
2444 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2447 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2450 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2454 prop = RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
2455 RNA_def_property_float_default(prop, def);
2456 RNA_def_property_range(prop, min, max);
2457 RNA_def_property_ui_text(prop, name ? name : id, description);
2458 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
2461 bpy_prop_assign_flag(prop, opts);
2463 bpy_prop_callback_assign_update(prop, update_cb);
2464 bpy_prop_callback_assign_float(prop, get_cb, set_cb);
2465 RNA_def_property_duplicate_pointers(srna, prop);
2470 PyDoc_STRVAR(BPy_FloatVectorProperty_doc,
2471 ".. function:: FloatVectorProperty(name=\"\", "
2472 "description=\"\", "
2473 "default=(0.0, 0.0, 0.0), "
2474 "min=sys.float_info.min, max=sys.float_info.max, "
2475 "soft_min=sys.float_info.min, soft_max=sys.float_info.max, "
2478 "options={'ANIMATABLE'}, "
2486 " Returns a new vector float property definition.\n"
2488 BPY_PROPDEF_NAME_DOC
2489 BPY_PROPDEF_DESC_DOC
2490 " :arg default: sequence of floats the length of *size*.\n"
2491 " :type default: sequence\n"
2492 BPY_PROPDEF_NUM_MIN_DOC
2493 " :type min: float\n"
2494 BPY_PROPDEF_NUM_MAX_DOC
2495 " :type max: float\n"
2496 BPY_PROPDEF_NUM_SOFTMIN_DOC
2497 " :type soft_min: float\n"
2498 BPY_PROPDEF_NUM_SOFTMAX_DOC
2499 " :type soft_max: float\n"
2500 BPY_PROPDEF_OPTIONS_DOC
2501 BPY_PROPDEF_FLOAT_STEP_DOC
2502 BPY_PROPDEF_FLOAT_PREC_DOC
2503 BPY_PROPDEF_SUBTYPE_ARRAY_DOC
2504 BPY_PROPDEF_UNIT_DOC
2505 BPY_PROPDEF_VECSIZE_DOC
2506 BPY_PROPDEF_UPDATE_DOC
2510 static PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
2514 BPY_PROPDEF_HEAD(FloatVectorProperty);
2517 static const char *kwlist[] = {"attr", "name", "description", "default",
2518 "min", "max", "soft_min", "soft_max",
2519 "step", "precision", "options", "subtype",
2520 "unit", "size", "update", "get", "set", NULL};
2521 const char *id = NULL, *name = NULL, *description = "";
2523 float min = -FLT_MAX, max = FLT_MAX, soft_min = -FLT_MAX, soft_max = FLT_MAX, step = 3;
2524 float def[PYRNA_STACK_ARRAY] = {0.0f};
2525 int precision = 2, size = 3;
2527 PyObject *pydef = NULL;
2528 PyObject *pyopts = NULL;
2530 const char *pysubtype = NULL;
2531 int subtype = PROP_NONE;
2532 const char *pyunit = NULL;
2533 int unit = PROP_UNIT_NONE;
2534 PyObject *update_cb = NULL;
2535 PyObject *get_cb = NULL;
2536 PyObject *set_cb = NULL;
2538 if (!PyArg_ParseTupleAndKeywords(args, kw,
2539 "s#|ssOfffffiO!ssiOOO:FloatVectorProperty",
2540 (char **)kwlist, &id, &id_len,
2541 &name, &description, &pydef,
2542 &min, &max, &soft_min, &soft_max,
2543 &step, &precision, &PySet_Type,
2544 &pyopts, &pysubtype, &pyunit, &size,
2545 &update_cb, &get_cb, &set_cb))
2550 BPY_PROPDEF_SUBTYPE_CHECK(FloatVectorProperty, property_flag_items, property_subtype_array_items);
2552 if (pyunit && RNA_enum_value_from_id(rna_enum_property_unit_items, pyunit, &unit) == 0) {
2553 PyErr_Format(PyExc_TypeError, "FloatVectorProperty(unit='%s'): invalid unit", pyunit);
2557 if (size < 1 || size > PYRNA_STACK_ARRAY) {
2558 PyErr_Format(PyExc_TypeError,
2559 "FloatVectorProperty(size=%d): size must be between 0 and "
2560 STRINGIFY(PYRNA_STACK_ARRAY), size);
2564 if (pydef && PyC_AsArray(def, pydef, size, &PyFloat_Type, false, "FloatVectorProperty(default=sequence)") == -1)
2567 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2570 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2573 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2577 prop = RNA_def_property(srna, id, PROP_FLOAT, subtype | unit);
2578 RNA_def_property_array(prop, size);
2579 if (pydef) RNA_def_property_float_array_default(prop, def);
2580 RNA_def_property_range(prop, min, max);
2581 RNA_def_property_ui_text(prop, name ? name : id, description);
2582 RNA_def_property_ui_range(prop, MAX2(soft_min, min), MIN2(soft_max, max), step, precision);
2585 bpy_prop_assign_flag(prop, opts);
2587 bpy_prop_callback_assign_update(prop, update_cb);
2588 bpy_prop_callback_assign_float_array(prop, get_cb, set_cb);
2589 RNA_def_property_duplicate_pointers(srna, prop);
2594 PyDoc_STRVAR(BPy_StringProperty_doc,
2595 ".. function:: StringProperty(name=\"\", "
2596 "description=\"\", "
2599 "options={'ANIMATABLE'}, "
2605 " Returns a new string property definition.\n"
2607 BPY_PROPDEF_NAME_DOC
2608 BPY_PROPDEF_DESC_DOC
2609 " :arg default: initializer string.\n"
2610 " :type default: string\n"
2611 " :arg maxlen: maximum length of the string.\n"
2612 " :type maxlen: int\n"
2613 BPY_PROPDEF_OPTIONS_DOC
2614 BPY_PROPDEF_SUBTYPE_STRING_DOC
2615 BPY_PROPDEF_UPDATE_DOC
2619 static PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
2623 BPY_PROPDEF_HEAD(StringProperty);
2626 static const char *kwlist[] = {"attr", "name", "description", "default",
2627 "maxlen", "options", "subtype", "update", "get", "set", NULL};
2628 const char *id = NULL, *name = NULL, *description = "", *def = "";
2632 PyObject *pyopts = NULL;
2634 const char *pysubtype = NULL;
2635 int subtype = PROP_NONE;
2636 PyObject *update_cb = NULL;
2637 PyObject *get_cb = NULL;
2638 PyObject *set_cb = NULL;
2640 if (!PyArg_ParseTupleAndKeywords(args, kw,
2641 "s#|sssiO!sOOO:StringProperty",
2642 (char **)kwlist, &id, &id_len,
2643 &name, &description, &def,
2644 &maxlen, &PySet_Type, &pyopts, &pysubtype,
2645 &update_cb, &get_cb, &set_cb))
2650 BPY_PROPDEF_SUBTYPE_CHECK(StringProperty, property_flag_items, property_subtype_string_items);
2652 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2655 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2658 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2662 prop = RNA_def_property(srna, id, PROP_STRING, subtype);
2663 if (maxlen != 0) RNA_def_property_string_maxlength(prop, maxlen + 1); /* +1 since it includes null terminator */
2664 if (def && def[0]) RNA_def_property_string_default(prop, def);
2665 RNA_def_property_ui_text(prop, name ? name : id, description);
2668 bpy_prop_assign_flag(prop, opts);
2670 bpy_prop_callback_assign_update(prop, update_cb);
2671 bpy_prop_callback_assign_string(prop, get_cb, set_cb);
2672 RNA_def_property_duplicate_pointers(srna, prop);
2677 PyDoc_STRVAR(BPy_EnumProperty_doc,
2678 ".. function:: EnumProperty(items, "
2680 "description=\"\", "
2682 "options={'ANIMATABLE'}, "
2687 " Returns a new enumerator property definition.\n"
2689 " :arg items: sequence of enum items formatted:\n"
2690 " ``[(identifier, name, description, icon, number), ...]``.\n"
2692 " The first three elements of the tuples are mandatory.\n"
2694 " :identifier: The identifier is used for Python access.\n"
2695 " :name: Name for the interace.\n"
2696 " :description: Used for documentation and tooltips.\n"
2697 " :icon: An icon string identifier or integer icon value\n"
2698 " (e.g. returned by :class:`bpy.types.UILayout.icon`)\n"
2699 " :number: Unique value used as the identifier for this item (stored in file data).\n"
2700 " Use when the identifier may need to change. If the *ENUM_FLAG* option is used,\n"
2701 " the values are bitmasks and should be powers of two.\n"
2703 " When an item only contains 4 items they define ``(identifier, name, description, number)``.\n"
2705 " For dynamic values a callback can be passed which returns a list in\n"
2706 " the same format as the static list.\n"
2707 " This function must take 2 arguments ``(self, context)``, **context may be None**.\n"
2711 " There is a known bug with using a callback,\n"
2712 " Python must keep a reference to the strings returned or Blender will misbehave\n"
2715 " :type items: sequence of string tuples or a function\n"
2716 BPY_PROPDEF_NAME_DOC
2717 BPY_PROPDEF_DESC_DOC
2718 " :arg default: The default value for this enum, a string from the identifiers used in *items*.\n"
2719 " If the *ENUM_FLAG* option is used this must be a set of such string identifiers instead.\n"
2720 " WARNING: It shall not be specified (or specified to its default *None* value) for dynamic enums\n"
2721 " (i.e. if a callback function is given as *items* parameter).\n"
2722 " :type default: string or set\n"
2723 BPY_PROPDEF_OPTIONS_ENUM_DOC
2724 BPY_PROPDEF_UPDATE_DOC
2728 static PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw)
2732 BPY_PROPDEF_HEAD(EnumProperty);
2735 static const char *kwlist[] = {"attr", "items", "name", "description", "default",
2736 "options", "update", "get", "set", NULL};
2737 const char *id = NULL, *name = NULL, *description = "";
2738 PyObject *def = NULL;
2741 PyObject *items, *items_fast;
2742 EnumPropertyItem *eitems;
2744 PyObject *pyopts = NULL;
2746 bool is_itemf = false;
2747 PyObject *update_cb = NULL;
2748 PyObject *get_cb = NULL;
2749 PyObject *set_cb = NULL;
2751 if (!PyArg_ParseTupleAndKeywords(args, kw,
2752 "s#O|ssOO!OOO:EnumProperty",
2753 (char **)kwlist, &id, &id_len,
2754 &items, &name, &description,
2755 &def, &PySet_Type, &pyopts,
2756 &update_cb, &get_cb, &set_cb))
2761 BPY_PROPDEF_CHECK(EnumProperty, property_flag_enum_items);
2763 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2766 if (bpy_prop_callback_check(get_cb, "get", 1) == -1) {
2769 if (bpy_prop_callback_check(set_cb, "set", 2) == -1) {
2773 if (def == Py_None) {
2774 /* This allows to get same behavior when explicitly passing None as default value,
2775 * and not defining a default value at all! */
2779 /* items can be a list or a callable */
2780 if (PyFunction_Check(items)) { /* don't use PyCallable_Check because we need the function code for errors */
2781 PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(items);
2782 if (f_code->co_argcount != 2) {
2783 PyErr_Format(PyExc_ValueError,
2784 "EnumProperty(...): expected 'items' function to take 2 arguments, not %d",
2785 f_code->co_argcount);
2790 /* note, using type error here is odd but python does this for invalid arguments */
2791 PyErr_SetString(PyExc_TypeError,
2792 "EnumProperty(...): 'default' can't be set when 'items' is a function");
2797 eitems = DummyRNA_NULL_items;
2800 if (!(items_fast = PySequence_Fast(items, "EnumProperty(...): "
2801 "expected a sequence of tuples for the enum items or a function")))
2806 eitems = enum_items_from_py(items_fast, def, &defvalue,
2807 (opts & PROP_ENUM_FLAG) != 0);
2810 Py_DECREF(items_fast);
2815 if (opts & PROP_ENUM_FLAG) prop = RNA_def_enum_flag(srna, id, eitems, defvalue, name ? name : id, description);
2816 else prop = RNA_def_enum(srna, id, eitems, defvalue, name ? name : id, description);
2819 bpy_prop_assign_flag(prop, opts);
2821 bpy_prop_callback_assign_update(prop, update_cb);
2822 bpy_prop_callback_assign_enum(prop, get_cb, set_cb, (is_itemf ? items : NULL));
2823 RNA_def_property_duplicate_pointers(srna, prop);
2825 if (is_itemf == false) {
2826 /* note: this must be postponed until after #RNA_def_property_duplicate_pointers
2827 * otherwise if this is a generator it may free the strings before we copy them */
2828 Py_DECREF(items_fast);
2836 StructRNA *pointer_type_from_py(PyObject *value, const char *error_prefix)
2840 srna = srna_from_self(value, "");
2842 if (PyErr_Occurred()) {
2843 PyObject *msg = PyC_ExceptionBuffer();
2844 const char *msg_char = _PyUnicode_AsString(msg);
2845 PyErr_Format(PyExc_TypeError,
2846 "%.200s expected an RNA type, failed with: %s",
2847 error_prefix, msg_char);
2851 PyErr_Format(PyExc_TypeError,
2852 "%.200s expected an RNA type, failed with type '%s'",
2853 error_prefix, Py_TYPE(value)->tp_name);
2861 PyDoc_STRVAR(BPy_PointerProperty_doc,
2862 ".. function:: PointerProperty(type=None, "
2864 "description=\"\", "
2865 "options={'ANIMATABLE'}, "
2869 " Returns a new pointer property definition.\n"
2871 BPY_PROPDEF_TYPE_DOC
2872 BPY_PROPDEF_NAME_DOC
2873 BPY_PROPDEF_DESC_DOC
2874 BPY_PROPDEF_OPTIONS_DOC
2875 BPY_PROPDEF_UPDATE_DOC
2877 PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw)
2881 BPY_PROPDEF_HEAD(PointerProperty);
2884 static const char *kwlist[] = {"attr", "type", "name", "description", "options", "poll", "update", NULL};
2885 const char *id = NULL, *name = NULL, *description = "";
2889 PyObject *type = Py_None;
2890 PyObject *pyopts = NULL;
2892 PyObject *update_cb = NULL, *poll_cb = NULL;
2894 if (!PyArg_ParseTupleAndKeywords(args, kw,
2895 "s#O|ssO!OOO:PointerProperty",
2896 (char **)kwlist, &id, &id_len,
2897 &type, &name, &description,
2898 &PySet_Type, &pyopts,
2899 &poll_cb, &update_cb))
2904 BPY_PROPDEF_CHECK(PointerProperty, property_flag_items);
2906 ptype = pointer_type_from_py(type, "PointerProperty(...)");
2909 if (!RNA_struct_is_a(ptype, &RNA_PropertyGroup) && !RNA_struct_is_ID(ptype)) {
2910 PyErr_Format(PyExc_TypeError,
2911 "PointerProperty(...) expected an RNA type derived from %.200s or %.200s",
2912 RNA_struct_ui_name(&RNA_ID), RNA_struct_ui_name(&RNA_PropertyGroup));
2915 if (bpy_prop_callback_check(update_cb, "update", 2) == -1) {
2918 if (bpy_prop_callback_check(poll_cb, "poll", 2) == -1) {
2921 prop = RNA_def_pointer_runtime(srna, id, ptype, name ? name : id, description);
2923 bpy_prop_assign_flag(prop, opts);
2926 if (RNA_struct_idprops_contains_datablock(ptype)) {
2927 if (RNA_struct_is_a(srna, &RNA_PropertyGroup)) {
2928 RNA_def_struct_flag(srna, STRUCT_CONTAINS_DATABLOCK_IDPROPERTIES);
2931 bpy_prop_callback_assign_update(prop, update_cb);
2932 bpy_prop_callback_assign_pointer(prop, poll_cb);
2933 RNA_def_property_duplicate_pointers(srna, prop);
2938 PyDoc_STRVAR(BPy_CollectionProperty_doc,
2939 ".. function:: CollectionProperty(type=None, "
2941 "description=\"\", "
2942 "options={'ANIMATABLE'})\n"
2944 " Returns a new collection property definition.\n"
2946 BPY_PROPDEF_TYPE_DOC
2947 BPY_PROPDEF_NAME_DOC
2948 BPY_PROPDEF_DESC_DOC
2949 BPY_PROPDEF_OPTIONS_DOC
2951 PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
2955 BPY_PROPDEF_HEAD(CollectionProperty);
2958 static const char *kwlist[] = {"attr", "type", "name", "description", "options", NULL};
2959 const char *id = NULL, *name = NULL, *description = "";
2963 PyObject *type = Py_None;
2964 PyObject *pyopts = NULL;
2967 if (!PyArg_ParseTupleAndKeywords(args, kw,
2968 "s#O|ssO!:CollectionProperty",
2969 (char **)kwlist, &id, &id_len,
2970 &type, &name, &description,
2971 &PySet_Type, &pyopts))
2976 BPY_PROPDEF_CHECK(CollectionProperty, property_flag_items);
2978 ptype = pointer_type_from_py(type, "CollectionProperty(...):");
2982 if (!RNA_struct_is_a(ptype, &RNA_PropertyGroup)) {
2983 PyErr_Format(PyExc_TypeError,
2984 "CollectionProperty(...) expected an RNA type derived from %.200s",
2985 RNA_struct_ui_name(&RNA_ID), RNA_struct_ui_name(&RNA_PropertyGroup));
2989 prop = RNA_def_collection_runtime(srna, id, ptype, name ? name : id, description);
2991 bpy_prop_assign_flag(prop, opts);
2994 if (RNA_struct_idprops_contains_datablock(ptype)) {
2995 if (RNA_struct_is_a(srna, &RNA_PropertyGroup)) {
2996 RNA_def_struct_flag(srna, STRUCT_CONTAINS_DATABLOCK_IDPROPERTIES);
2999 RNA_def_property_duplicate_pointers(srna, prop);
3004 PyDoc_STRVAR(BPy_RemoveProperty_doc,
3005 ".. function:: RemoveProperty(cls, attr)\n"
3007 " Removes a dynamically defined property.\n"
3009 " :arg cls: The class containing the property (must be a positional argument).\n"
3010 " :type cls: type\n"
3011 " :arg attr: Property name (must be passed as a keyword).\n"
3012 " :type attr: string\n"
3014 ".. note:: Typically this function doesn't need to be accessed directly.\n"
3015 " Instead use ``del cls.attr``\n"
3017 static PyObject *BPy_RemoveProperty(PyObject *self, PyObject *args, PyObject *kw)
3021 if (PyTuple_GET_SIZE(args) == 1) {
3023 self = PyTuple_GET_ITEM(args, 0);
3024 args = PyTuple_New(0);
3025 ret = BPy_RemoveProperty(self, args, kw);
3029 else if (PyTuple_GET_SIZE(args) > 1) {
3030 PyErr_SetString(PyExc_ValueError, "expected one positional arg, one keyword arg");
3034 srna = srna_from_self(self, "RemoveProperty(...):");
3035 if (srna == NULL && PyErr_Occurred()) {
3036 return NULL; /* self's type was compatible but error getting the srna */
3038 else if (srna == NULL) {
3039 PyErr_SetString(PyExc_TypeError, "RemoveProperty(): struct rna not available for this type");
3043 static const char *kwlist[] = {"attr", NULL};
3045 const char *id = NULL;
3047 if (!PyArg_ParseTupleAndKeywords(args, kw,
3049 (char **)kwlist, &id))
3054 if (RNA_def_property_free_identifier(srna, id) != 1) {
3055 PyErr_Format(PyExc_TypeError, "RemoveProperty(): '%s' not a defined dynamic property", id);
3062 static struct PyMethodDef props_methods[] = {
3063 {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS | METH_KEYWORDS, BPy_BoolProperty_doc},
3064 {"BoolVectorProperty", (PyCFunction)BPy_BoolVectorProperty, METH_VARARGS | METH_KEYWORDS, BPy_BoolVectorProperty_doc},
3065 {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS | METH_KEYWORDS, BPy_IntProperty_doc},
3066 {"IntVectorProperty", (PyCFunction)BPy_IntVectorProperty, METH_VARARGS | METH_KEYWORDS, BPy_IntVectorProperty_doc},
3067 {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS | METH_KEYWORDS, BPy_FloatProperty_doc},
3068 {"FloatVectorProperty", (PyCFunction)BPy_FloatVectorProperty, METH_VARARGS | METH_KEYWORDS, BPy_FloatVectorProperty_doc},
3069 {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS | METH_KEYWORDS, BPy_StringProperty_doc},
3070 {"EnumProperty", (PyCFunction)BPy_EnumProperty, METH_VARARGS | METH_KEYWORDS, BPy_EnumProperty_doc},
3071 {"PointerProperty", (PyCFunction)BPy_PointerProperty, METH_VARARGS | METH_KEYWORDS, BPy_PointerProperty_doc},
3072 {"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS | METH_KEYWORDS, BPy_CollectionProperty_doc},
3074 {"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS | METH_KEYWORDS, BPy_RemoveProperty_doc},
3075 {NULL, NULL, 0, NULL}
3078 static struct PyModuleDef props_module = {
3079 PyModuleDef_HEAD_INIT,
3081 "This module defines properties to extend Blender's internal data. The result of these functions"
3082 " is used to assign properties to classes registered with Blender and can't be used directly.\n"
3084 ".. note:: All parameters to these functions must be passed as keywords.\n",
3085 -1, /* multiple "initialization" just copies the module dict. */
3087 NULL, NULL, NULL, NULL
3090 PyObject *BPY_rna_props(void)
3092 PyObject *submodule;
3093 PyObject *submodule_dict;
3095 submodule = PyModule_Create(&props_module);
3096 PyDict_SetItemString(PyImport_GetModuleDict(), props_module.m_name, submodule);
3098 /* INCREF since its its assumed that all these functions return the
3099 * module with a new ref like PyDict_New, since they are passed to
3100 * PyModule_AddObject which steals a ref */
3101 Py_INCREF(submodule);
3103 /* api needs the PyObjects internally */
3104 submodule_dict = PyModule_GetDict(submodule);
3106 #define ASSIGN_STATIC(_name) pymeth_##_name = PyDict_GetItemString(submodule_dict, #_name)
3108 ASSIGN_STATIC(BoolProperty);
3109 ASSIGN_STATIC(BoolVectorProperty);
3110 ASSIGN_STATIC(IntProperty);
3111 ASSIGN_STATIC(IntVectorProperty);
3112 ASSIGN_STATIC(FloatProperty);
3113 ASSIGN_STATIC(FloatVectorProperty);
3114 ASSIGN_STATIC(StringProperty);
3115 ASSIGN_STATIC(EnumProperty);
3116 ASSIGN_STATIC(PointerProperty);
3117 ASSIGN_STATIC(CollectionProperty);
3118 ASSIGN_STATIC(RemoveProperty);