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_rna.c
24 * \ingroup pythonintern
26 * This file is the main interface between python and blenders data api (RNA),
27 * exposing RNA to python so blender data can be accessed in a python like way.
29 * The two main types are 'BPy_StructRNA' and 'BPy_PropertyRNA' - the base
30 * classes for most of the data python accesses in blender.
36 #include <float.h> /* FLT_MIN/MAX */
38 #include "RNA_types.h"
40 #include "BLI_dynstr.h"
41 #include "BLI_string.h"
42 #include "BLI_listbase.h"
43 #include "BLI_math_rotation.h"
44 #include "BLI_utildefines.h"
46 #include "BPY_extern.h"
49 #include "bpy_rna_anim.h"
50 #include "bpy_props.h"
52 #include "bpy_rna_callback.h"
53 #include "bpy_intern_string.h"
55 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
56 # include "MEM_guardedalloc.h"
59 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
60 # include "BLI_ghash.h"
63 #include "RNA_enum_types.h"
64 #include "RNA_define.h" /* RNA_def_property_free_identifier */
65 #include "RNA_access.h"
67 #include "MEM_guardedalloc.h"
70 #include "BKE_idcode.h"
71 #include "BKE_context.h"
72 #include "BKE_global.h" /* evil G.* */
73 #include "BKE_report.h"
74 #include "BKE_idprop.h"
76 #include "BKE_animsys.h"
77 #include "BKE_fcurve.h"
79 #include "../generic/idprop_py_api.h" /* for IDprop lookups */
80 #include "../generic/py_capi_utils.h"
82 #ifdef WITH_INTERNATIONAL
83 # include "BLF_translation.h"
86 #define USE_PEDANTIC_WRITE
88 #define USE_STRING_COERCE
90 static PyObject *pyrna_struct_Subtype(PointerRNA *ptr);
91 static PyObject *pyrna_prop_collection_values(BPy_PropertyRNA *self);
93 #define BPY_DOC_ID_PROP_TYPE_NOTE \
96 " Only :class:`bpy.types.ID`, :class:`bpy.types.Bone` and \n" \
97 " :class:`bpy.types.PoseBone` classes support custom properties.\n"
100 int pyrna_struct_validity_check(BPy_StructRNA *pysrna)
102 if (pysrna->ptr.type) {
105 PyErr_Format(PyExc_ReferenceError,
106 "StructRNA of type %.200s has been removed",
107 Py_TYPE(pysrna)->tp_name);
111 int pyrna_prop_validity_check(BPy_PropertyRNA *self)
113 if (self->ptr.type) {
116 PyErr_Format(PyExc_ReferenceError,
117 "PropertyRNA of type %.200s.%.200s has been removed",
118 Py_TYPE(self)->tp_name, RNA_property_identifier(self->prop));
122 void pyrna_invalidate(BPy_DummyPointerRNA *self)
124 RNA_POINTER_INVALIDATE(&self->ptr);
127 #ifdef USE_PYRNA_INVALIDATE_GC
128 #define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g) + 1))
130 /* only for sizeof() */
131 struct gc_generation {
137 static void id_release_gc(struct ID *id)
140 // unsigned int i = 0;
141 for (j = 0; j < 3; j++) {
142 /* hack below to get the 2 other lists from _PyGC_generation0 that are normally not exposed */
143 PyGC_Head *gen = (PyGC_Head *)(((char *)_PyGC_generation0) + (sizeof(gc_generation) * j));
144 PyGC_Head *g = gen->gc.gc_next;
145 while ((g = g->gc.gc_next) != gen) {
146 PyObject *ob = FROM_GC(g);
147 if (PyType_IsSubtype(Py_TYPE(ob), &pyrna_struct_Type) || PyType_IsSubtype(Py_TYPE(ob), &pyrna_prop_Type)) {
148 BPy_DummyPointerRNA *ob_ptr = (BPy_DummyPointerRNA *)ob;
149 if (ob_ptr->ptr.id.data == id) {
150 pyrna_invalidate(ob_ptr);
151 // printf("freeing: %p %s, %.200s\n", (void *)ob, id->name, Py_TYPE(ob)->tp_name);
157 // printf("id_release_gc freed '%s': %d\n", id->name, i);
161 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
162 //#define DEBUG_RNA_WEAKREF
164 struct GHash *id_weakref_pool = NULL;
165 static PyObject *id_free_weakref_cb(PyObject *weakinfo_pair, PyObject *weakref);
166 static PyMethodDef id_free_weakref_cb_def = {"id_free_weakref_cb", (PyCFunction)id_free_weakref_cb, METH_O, NULL};
168 /* adds a reference to the list, remember to decref */
169 static GHash *id_weakref_pool_get(ID *id)
171 GHash *weakinfo_hash = NULL;
173 if (id_weakref_pool) {
174 weakinfo_hash = BLI_ghash_lookup(id_weakref_pool, (void *)id);
177 /* first time, allocate pool */
178 id_weakref_pool = BLI_ghash_ptr_new("rna_global_pool");
179 weakinfo_hash = NULL;
182 if (weakinfo_hash == NULL) {
183 /* we're using a ghash as a set, could use libHX's HXMAP_SINGULAR but would be an extra dep. */
184 weakinfo_hash = BLI_ghash_ptr_new("rna_id");
185 BLI_ghash_insert(id_weakref_pool, (void *)id, weakinfo_hash);
188 return weakinfo_hash;
191 /* called from pyrna_struct_CreatePyObject() and pyrna_prop_CreatePyObject() */
192 static void id_weakref_pool_add(ID *id, BPy_DummyPointerRNA *pyrna)
195 PyObject *weakref_capsule;
196 PyObject *weakref_cb_py;
198 /* create a new function instance and insert the list as 'self' so we can remove ourself from it */
199 GHash *weakinfo_hash = id_weakref_pool_get(id); /* new or existing */
201 weakref_capsule = PyCapsule_New(weakinfo_hash, NULL, NULL);
202 weakref_cb_py = PyCFunction_New(&id_free_weakref_cb_def, weakref_capsule);
203 Py_DECREF(weakref_capsule);
205 /* add weakref to weakinfo_hash list */
206 weakref = PyWeakref_NewRef((PyObject *)pyrna, weakref_cb_py);
208 Py_DECREF(weakref_cb_py); /* function owned by the weakref now */
210 /* important to add at the end, since first removal looks at the end */
211 BLI_ghash_insert(weakinfo_hash, (void *)weakref, id); /* using a hash table as a set, all 'id's are the same */
212 /* weakinfo_hash owns the weakref */
216 /* workaround to get the last id without a lookup */
217 static ID *_id_tmp_ptr;
218 static void value_id_set(void *id)
220 _id_tmp_ptr = (ID *)id;
223 static void id_release_weakref_list(struct ID *id, GHash *weakinfo_hash);
224 static PyObject *id_free_weakref_cb(PyObject *weakinfo_capsule, PyObject *weakref)
226 /* important to search backwards */
227 GHash *weakinfo_hash = PyCapsule_GetPointer(weakinfo_capsule, NULL);
230 if (BLI_ghash_size(weakinfo_hash) > 1) {
231 BLI_ghash_remove(weakinfo_hash, weakref, NULL, NULL);
233 else { /* get the last id and free it */
234 BLI_ghash_remove(weakinfo_hash, weakref, NULL, value_id_set);
235 id_release_weakref_list(_id_tmp_ptr, weakinfo_hash);
243 static void id_release_weakref_list(struct ID *id, GHash *weakinfo_hash)
245 GHashIterator weakinfo_hash_iter;
247 BLI_ghashIterator_init(&weakinfo_hash_iter, weakinfo_hash);
249 #ifdef DEBUG_RNA_WEAKREF
250 fprintf(stdout, "id_release_weakref: '%s', %d items\n", id->name, BLI_ghash_size(weakinfo_hash));
253 while (!BLI_ghashIterator_done(&weakinfo_hash_iter)) {
254 PyObject *weakref = (PyObject *)BLI_ghashIterator_getKey(&weakinfo_hash_iter);
255 PyObject *item = PyWeakref_GET_OBJECT(weakref);
256 if (item != Py_None) {
258 #ifdef DEBUG_RNA_WEAKREF
259 PyC_ObSpit("id_release_weakref item ", item);
262 pyrna_invalidate((BPy_DummyPointerRNA *)item);
267 BLI_ghashIterator_step(&weakinfo_hash_iter);
270 BLI_ghash_remove(id_weakref_pool, (void *)id, NULL, NULL);
271 BLI_ghash_free(weakinfo_hash, NULL, NULL);
273 if (BLI_ghash_size(id_weakref_pool) == 0) {
274 BLI_ghash_free(id_weakref_pool, NULL, NULL);
275 id_weakref_pool = NULL;
276 #ifdef DEBUG_RNA_WEAKREF
277 printf("id_release_weakref freeing pool\n");
282 static void id_release_weakref(struct ID *id)
284 GHash *weakinfo_hash = BLI_ghash_lookup(id_weakref_pool, (void *)id);
286 id_release_weakref_list(id, weakinfo_hash);
290 #endif /* USE_PYRNA_INVALIDATE_WEAKREF */
292 void BPY_id_release(struct ID *id)
294 #ifdef USE_PYRNA_INVALIDATE_GC
298 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
299 if (id_weakref_pool) {
300 PyGILState_STATE gilstate = PyGILState_Ensure();
302 id_release_weakref(id);
304 PyGILState_Release(gilstate);
306 #endif /* USE_PYRNA_INVALIDATE_WEAKREF */
311 #ifdef USE_PEDANTIC_WRITE
312 static bool rna_disallow_writes = false;
314 static bool rna_id_write_error(PointerRNA *ptr, PyObject *key)
316 ID *id = ptr->id.data;
318 const short idcode = GS(id->name);
319 if (!ELEM(idcode, ID_WM, ID_SCR)) { /* may need more added here */
320 const char *idtype = BKE_idcode_to_name(idcode);
322 if (key && PyUnicode_Check(key)) pyname = _PyUnicode_AsString(key);
323 else pyname = "<UNKNOWN>";
325 /* make a nice string error */
326 BLI_assert(idtype != NULL);
327 PyErr_Format(PyExc_AttributeError,
328 "Writing to ID classes in this context is not allowed: "
329 "%.200s, %.200s datablock, error setting %.200s.%.200s",
330 id->name + 2, idtype, RNA_struct_identifier(ptr->type), pyname);
337 #endif /* USE_PEDANTIC_WRITE */
340 #ifdef USE_PEDANTIC_WRITE
341 bool pyrna_write_check(void)
343 return !rna_disallow_writes;
346 void pyrna_write_set(bool val)
348 rna_disallow_writes = !val;
350 #else /* USE_PEDANTIC_WRITE */
351 bool pyrna_write_check(void)
355 void pyrna_write_set(bool UNUSED(val))
359 #endif /* USE_PEDANTIC_WRITE */
361 static Py_ssize_t pyrna_prop_collection_length(BPy_PropertyRNA *self);
362 static Py_ssize_t pyrna_prop_array_length(BPy_PropertyArrayRNA *self);
363 static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value, const char *error_prefix);
364 static int deferred_register_prop(StructRNA *srna, PyObject *key, PyObject *item);
367 #include "../mathutils/mathutils.h" /* so we can have mathutils callbacks */
369 static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop,
370 Py_ssize_t start, Py_ssize_t stop, Py_ssize_t length);
371 static short pyrna_rotation_euler_order_get(PointerRNA *ptr, PropertyRNA **prop_eul_order, const short order_fallback);
373 /* bpyrna vector/euler/quat callbacks */
374 static unsigned char mathutils_rna_array_cb_index = -1; /* index for our callbacks */
376 /* subtype not used much yet */
377 #define MATHUTILS_CB_SUBTYPE_EUL 0
378 #define MATHUTILS_CB_SUBTYPE_VEC 1
379 #define MATHUTILS_CB_SUBTYPE_QUAT 2
380 #define MATHUTILS_CB_SUBTYPE_COLOR 3
382 static int mathutils_rna_generic_check(BaseMathObject *bmo)
384 BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
386 PYRNA_PROP_CHECK_INT(self);
388 return self->prop ? 0 : -1;
391 static int mathutils_rna_vector_get(BaseMathObject *bmo, int subtype)
393 BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
395 PYRNA_PROP_CHECK_INT(self);
397 if (self->prop == NULL)
400 RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
402 /* Euler order exception */
403 if (subtype == MATHUTILS_CB_SUBTYPE_EUL) {
404 EulerObject *eul = (EulerObject *)bmo;
405 PropertyRNA *prop_eul_order = NULL;
406 eul->order = pyrna_rotation_euler_order_get(&self->ptr, &prop_eul_order, eul->order);
412 static int mathutils_rna_vector_set(BaseMathObject *bmo, int subtype)
414 BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
417 PYRNA_PROP_CHECK_INT(self);
419 if (self->prop == NULL)
422 #ifdef USE_PEDANTIC_WRITE
423 if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
426 #endif /* USE_PEDANTIC_WRITE */
428 if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
429 PyErr_Format(PyExc_AttributeError,
430 "bpy_prop \"%.200s.%.200s\" is read-only",
431 RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
435 RNA_property_float_range(&self->ptr, self->prop, &min, &max);
437 if (min != -FLT_MAX || max != FLT_MAX) {
438 int i, len = RNA_property_array_length(&self->ptr, self->prop);
439 for (i = 0; i < len; i++) {
440 CLAMP(bmo->data[i], min, max);
444 RNA_property_float_set_array(&self->ptr, self->prop, bmo->data);
445 if (RNA_property_update_check(self->prop)) {
446 RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
449 /* Euler order exception */
450 if (subtype == MATHUTILS_CB_SUBTYPE_EUL) {
451 EulerObject *eul = (EulerObject *)bmo;
452 PropertyRNA *prop_eul_order = NULL;
453 short order = pyrna_rotation_euler_order_get(&self->ptr, &prop_eul_order, eul->order);
454 if (order != eul->order) {
455 RNA_property_enum_set(&self->ptr, prop_eul_order, eul->order);
456 if (RNA_property_update_check(prop_eul_order)) {
457 RNA_property_update(BPy_GetContext(), &self->ptr, prop_eul_order);
464 static int mathutils_rna_vector_get_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
466 BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
468 PYRNA_PROP_CHECK_INT(self);
470 if (self->prop == NULL)
473 bmo->data[index] = RNA_property_float_get_index(&self->ptr, self->prop, index);
477 static int mathutils_rna_vector_set_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
479 BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
481 PYRNA_PROP_CHECK_INT(self);
483 if (self->prop == NULL)
486 #ifdef USE_PEDANTIC_WRITE
487 if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
490 #endif /* USE_PEDANTIC_WRITE */
492 if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
493 PyErr_Format(PyExc_AttributeError,
494 "bpy_prop \"%.200s.%.200s\" is read-only",
495 RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
499 RNA_property_float_clamp(&self->ptr, self->prop, &bmo->data[index]);
500 RNA_property_float_set_index(&self->ptr, self->prop, index, bmo->data[index]);
502 if (RNA_property_update_check(self->prop)) {
503 RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
509 static Mathutils_Callback mathutils_rna_array_cb = {
510 (BaseMathCheckFunc) mathutils_rna_generic_check,
511 (BaseMathGetFunc) mathutils_rna_vector_get,
512 (BaseMathSetFunc) mathutils_rna_vector_set,
513 (BaseMathGetIndexFunc) mathutils_rna_vector_get_index,
514 (BaseMathSetIndexFunc) mathutils_rna_vector_set_index
518 /* bpyrna matrix callbacks */
519 static unsigned char mathutils_rna_matrix_cb_index = -1; /* index for our callbacks */
521 static int mathutils_rna_matrix_get(BaseMathObject *bmo, int UNUSED(subtype))
523 BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
525 PYRNA_PROP_CHECK_INT(self);
527 if (self->prop == NULL)
530 RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
534 static int mathutils_rna_matrix_set(BaseMathObject *bmo, int UNUSED(subtype))
536 BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
538 PYRNA_PROP_CHECK_INT(self);
540 if (self->prop == NULL)
543 #ifdef USE_PEDANTIC_WRITE
544 if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
547 #endif /* USE_PEDANTIC_WRITE */
549 if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
550 PyErr_Format(PyExc_AttributeError,
551 "bpy_prop \"%.200s.%.200s\" is read-only",
552 RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
556 /* can ignore clamping here */
557 RNA_property_float_set_array(&self->ptr, self->prop, bmo->data);
559 if (RNA_property_update_check(self->prop)) {
560 RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
565 static Mathutils_Callback mathutils_rna_matrix_cb = {
566 mathutils_rna_generic_check,
567 mathutils_rna_matrix_get,
568 mathutils_rna_matrix_set,
573 static short pyrna_rotation_euler_order_get(PointerRNA *ptr, PropertyRNA **prop_eul_order, const short order_fallback)
575 /* attempt to get order */
576 if (*prop_eul_order == NULL)
577 *prop_eul_order = RNA_struct_find_property(ptr, "rotation_mode");
579 if (*prop_eul_order) {
580 short order = RNA_property_enum_get(ptr, *prop_eul_order);
581 if (order >= EULER_ORDER_XYZ && order <= EULER_ORDER_ZYX) /* could be quat or axisangle */
585 return order_fallback;
588 #endif /* USE_MATHUTILS */
590 /* note that PROP_NONE is included as a vector subtype. this is because its handy to
591 * have x/y access to fcurve keyframes and other fixed size float arrays of length 2-4. */
592 #define PROP_ALL_VECTOR_SUBTYPES \
594 case PROP_TRANSLATION: \
595 case PROP_DIRECTION: \
596 case PROP_VELOCITY: \
597 case PROP_ACCELERATION: \
599 case PROP_XYZ_LENGTH \
602 PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
604 PyObject *ret = NULL;
610 const int flag = RNA_property_flag(prop);
612 /* disallow dynamic sized arrays to be wrapped since the size could change
613 * to a size mathutils does not support */
614 if ((RNA_property_type(prop) != PROP_FLOAT) || (flag & PROP_DYNAMIC))
617 len = RNA_property_array_length(ptr, prop);
618 subtype = RNA_property_subtype(prop);
619 totdim = RNA_property_array_dimension(ptr, prop, NULL);
620 is_thick = (flag & PROP_THICK_WRAP) != 0;
622 if (totdim == 1 || (totdim == 2 && subtype == PROP_MATRIX)) {
624 ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the mathutils PyObject */
627 case PROP_ALL_VECTOR_SUBTYPES:
628 if (len >= 2 && len <= 4) {
630 ret = Vector_CreatePyObject(NULL, len, Py_NEW, NULL);
631 RNA_property_float_get_array(ptr, prop, ((VectorObject *)ret)->vec);
634 PyObject *vec_cb = Vector_CreatePyObject_cb(ret, len, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_VEC);
635 Py_DECREF(ret); /* the vector owns now */
636 ret = vec_cb; /* return the vector instead */
643 ret = Matrix_CreatePyObject(NULL, 4, 4, Py_NEW, NULL);
644 RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->matrix);
647 PyObject *mat_cb = Matrix_CreatePyObject_cb(ret, 4, 4, mathutils_rna_matrix_cb_index, 0);
648 Py_DECREF(ret); /* the matrix owns now */
649 ret = mat_cb; /* return the matrix instead */
654 ret = Matrix_CreatePyObject(NULL, 3, 3, Py_NEW, NULL);
655 RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->matrix);
658 PyObject *mat_cb = Matrix_CreatePyObject_cb(ret, 3, 3, mathutils_rna_matrix_cb_index, 0);
659 Py_DECREF(ret); /* the matrix owns now */
660 ret = mat_cb; /* return the matrix instead */
665 case PROP_QUATERNION:
666 if (len == 3) { /* euler */
668 /* attempt to get order, only needed for thick types since wrapped with update via callbacks */
669 PropertyRNA *prop_eul_order = NULL;
670 short order = pyrna_rotation_euler_order_get(ptr, &prop_eul_order, EULER_ORDER_XYZ);
672 ret = Euler_CreatePyObject(NULL, order, Py_NEW, NULL); /* TODO, get order from RNA */
673 RNA_property_float_get_array(ptr, prop, ((EulerObject *)ret)->eul);
676 /* order will be updated from callback on use */
677 PyObject *eul_cb = Euler_CreatePyObject_cb(ret, EULER_ORDER_XYZ, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_EUL); // TODO, get order from RNA
678 Py_DECREF(ret); /* the euler owns now */
679 ret = eul_cb; /* return the euler instead */
684 ret = Quaternion_CreatePyObject(NULL, Py_NEW, NULL);
685 RNA_property_float_get_array(ptr, prop, ((QuaternionObject *)ret)->quat);
688 PyObject *quat_cb = Quaternion_CreatePyObject_cb(ret, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_QUAT);
689 Py_DECREF(ret); /* the quat owns now */
690 ret = quat_cb; /* return the quat instead */
695 case PROP_COLOR_GAMMA:
696 if (len == 3) { /* color */
698 ret = Color_CreatePyObject(NULL, Py_NEW, NULL);
699 RNA_property_float_get_array(ptr, prop, ((ColorObject *)ret)->col);
702 PyObject *col_cb = Color_CreatePyObject_cb(ret, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_COLOR);
703 Py_DECREF(ret); /* the color owns now */
704 ret = col_cb; /* return the color instead */
715 /* this is an array we cant reference (since its not thin wrappable)
716 * and cannot be coerced into a mathutils type, so return as a list */
717 ret = pyrna_prop_array_subscript_slice(NULL, ptr, prop, 0, len, len);
720 ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the mathutils PyObject */
723 #else /* USE_MATHUTILS */
726 #endif /* USE_MATHUTILS */
731 /* same as RNA_enum_value_from_id but raises an exception */
732 int pyrna_enum_value_from_id(EnumPropertyItem *item, const char *identifier, int *value, const char *error_prefix)
734 if (RNA_enum_value_from_id(item, identifier, value) == 0) {
735 const char *enum_str = BPy_enum_as_string(item);
736 PyErr_Format(PyExc_ValueError,
737 "%s: '%.200s' not found in (%s)",
738 error_prefix, identifier, enum_str);
739 MEM_freeN((void *)enum_str);
747 * checking the 'ptr->data' matches works in almost all cases,
748 * however there are a few RNA properties that are fake sub-structs and
749 * share the pointer with the parent, in those cases this happens 'a.b == a'
750 * see: r43352 for example.
752 * So compare the 'ptr->type' as well to avoid this problem.
753 * It's highly unlikely this would happen that 'ptr->data' and 'ptr->prop' would match,
754 * but _not_ 'ptr->type' but include this check for completeness.
757 static int pyrna_struct_compare(BPy_StructRNA *a, BPy_StructRNA *b)
759 return (((a->ptr.data == b->ptr.data) &&
760 (a->ptr.type == b->ptr.type)) ? 0 : -1);
763 static int pyrna_prop_compare(BPy_PropertyRNA *a, BPy_PropertyRNA *b)
765 return (((a->prop == b->prop) &&
766 (a->ptr.data == b->ptr.data) &&
767 (a->ptr.type == b->ptr.type)) ? 0 : -1);
770 static PyObject *pyrna_struct_richcmp(PyObject *a, PyObject *b, int op)
773 int ok = -1; /* zero is true */
775 if (BPy_StructRNA_Check(a) && BPy_StructRNA_Check(b))
776 ok = pyrna_struct_compare((BPy_StructRNA *)a, (BPy_StructRNA *)b);
783 res = ok ? Py_False : Py_True;
790 res = Py_NotImplemented;
797 return Py_INCREF(res), res;
800 static PyObject *pyrna_prop_richcmp(PyObject *a, PyObject *b, int op)
803 int ok = -1; /* zero is true */
805 if (BPy_PropertyRNA_Check(a) && BPy_PropertyRNA_Check(b))
806 ok = pyrna_prop_compare((BPy_PropertyRNA *)a, (BPy_PropertyRNA *)b);
813 res = ok ? Py_False : Py_True;
820 res = Py_NotImplemented;
827 return Py_INCREF(res), res;
830 /*----------------------repr--------------------------------------------*/
831 static PyObject *pyrna_struct_str(BPy_StructRNA *self)
836 if (!PYRNA_STRUCT_IS_VALID(self)) {
837 return PyUnicode_FromFormat("<bpy_struct, %.200s invalid>",
838 Py_TYPE(self)->tp_name);
841 /* print name if available */
842 name = RNA_struct_name_get_alloc(&self->ptr, NULL, 0, NULL);
844 ret = PyUnicode_FromFormat("<bpy_struct, %.200s(\"%.200s\")>",
845 RNA_struct_identifier(self->ptr.type),
847 MEM_freeN((void *)name);
851 return PyUnicode_FromFormat("<bpy_struct, %.200s at %p>",
852 RNA_struct_identifier(self->ptr.type),
856 static PyObject *pyrna_struct_repr(BPy_StructRNA *self)
858 ID *id = self->ptr.id.data;
862 if (id == NULL || !PYRNA_STRUCT_IS_VALID(self))
863 return pyrna_struct_str(self); /* fallback */
865 tmp_str = PyUnicode_FromString(id->name + 2);
867 if (RNA_struct_is_ID(self->ptr.type)) {
868 ret = PyUnicode_FromFormat("bpy.data.%s[%R]",
869 BKE_idcode_to_name_plural(GS(id->name)),
874 path = RNA_path_from_ID_to_struct(&self->ptr);
876 if (GS(id->name) == ID_NT) { /* nodetree paths are not accurate */
877 ret = PyUnicode_FromFormat("bpy.data...%s",
881 ret = PyUnicode_FromFormat("bpy.data.%s[%R].%s",
882 BKE_idcode_to_name_plural(GS(id->name)),
887 MEM_freeN((void *)path);
889 else { /* cant find, print something sane */
890 ret = PyUnicode_FromFormat("bpy.data.%s[%R]...%s",
891 BKE_idcode_to_name_plural(GS(id->name)),
893 RNA_struct_identifier(self->ptr.type));
902 static PyObject *pyrna_prop_str(BPy_PropertyRNA *self)
907 const char *type_id = NULL;
908 char type_fmt[64] = "";
911 PYRNA_PROP_CHECK_OBJ(self);
913 type = RNA_property_type(self->prop);
915 if (RNA_enum_id_from_value(property_type_items, type, &type_id) == 0) {
916 PyErr_SetString(PyExc_RuntimeError, "could not use property type, internal error"); /* should never happen */
920 /* this should never fail */
924 while ((*c++ = tolower(*type_id++))) {}
926 if (type == PROP_COLLECTION) {
927 len = pyrna_prop_collection_length(self);
929 else if (RNA_property_array_check(self->prop)) {
930 len = pyrna_prop_array_length((BPy_PropertyArrayRNA *)self);
934 sprintf(--c, "[%d]", len);
937 /* if a pointer, try to print name of pointer target too */
938 if (type == PROP_POINTER) {
939 ptr = RNA_property_pointer_get(&self->ptr, self->prop);
940 name = RNA_struct_name_get_alloc(&ptr, NULL, 0, NULL);
943 ret = PyUnicode_FromFormat("<bpy_%.200s, %.200s.%.200s(\"%.200s\")>",
945 RNA_struct_identifier(self->ptr.type),
946 RNA_property_identifier(self->prop),
948 MEM_freeN((void *)name);
952 if (type == PROP_COLLECTION) {
954 if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
955 return PyUnicode_FromFormat("<bpy_%.200s, %.200s>",
957 RNA_struct_identifier(r_ptr.type));
961 return PyUnicode_FromFormat("<bpy_%.200s, %.200s.%.200s>",
963 RNA_struct_identifier(self->ptr.type),
964 RNA_property_identifier(self->prop));
967 static PyObject *pyrna_prop_repr(BPy_PropertyRNA *self)
969 ID *id = self->ptr.id.data;
974 PYRNA_PROP_CHECK_OBJ(self);
977 return pyrna_prop_str(self); /* fallback */
979 tmp_str = PyUnicode_FromString(id->name + 2);
981 path = RNA_path_from_ID_to_property(&self->ptr, self->prop);
983 if (GS(id->name) == ID_NT) { /* nodetree paths are not accurate */
984 ret = PyUnicode_FromFormat("bpy.data...%s",
988 ret = PyUnicode_FromFormat("bpy.data.%s[%R].%s",
989 BKE_idcode_to_name_plural(GS(id->name)),
994 MEM_freeN((void *)path);
996 else { /* cant find, print something sane */
997 ret = PyUnicode_FromFormat("bpy.data.%s[%R]...%s",
998 BKE_idcode_to_name_plural(GS(id->name)),
1000 RNA_property_identifier(self->prop));
1009 static PyObject *pyrna_func_repr(BPy_FunctionRNA *self)
1011 return PyUnicode_FromFormat("<%.200s %.200s.%.200s()>",
1012 Py_TYPE(self)->tp_name,
1013 RNA_struct_identifier(self->ptr.type),
1014 RNA_function_identifier(self->func));
1018 static Py_hash_t pyrna_struct_hash(BPy_StructRNA *self)
1020 return _Py_HashPointer(self->ptr.data);
1023 /* from python's meth_hash v3.1.2 */
1024 static long pyrna_prop_hash(BPy_PropertyRNA *self)
1027 if (self->ptr.data == NULL)
1030 x = _Py_HashPointer(self->ptr.data);
1034 y = _Py_HashPointer((void *)(self->prop));
1043 #ifdef USE_PYRNA_STRUCT_REFERENCE
1044 static int pyrna_struct_traverse(BPy_StructRNA *self, visitproc visit, void *arg)
1046 Py_VISIT(self->reference);
1050 static int pyrna_struct_clear(BPy_StructRNA *self)
1052 Py_CLEAR(self->reference);
1055 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
1057 /* use our own dealloc so we can free a property if we use one */
1058 static void pyrna_struct_dealloc(BPy_StructRNA *self)
1060 #ifdef PYRNA_FREE_SUPPORT
1061 if (self->freeptr && self->ptr.data) {
1062 IDP_FreeProperty(self->ptr.data);
1063 MEM_freeN(self->ptr.data);
1064 self->ptr.data = NULL;
1066 #endif /* PYRNA_FREE_SUPPORT */
1069 if (self->in_weakreflist != NULL) {
1070 PyObject_ClearWeakRefs((PyObject *)self);
1074 #ifdef USE_PYRNA_STRUCT_REFERENCE
1075 if (self->reference) {
1076 PyObject_GC_UnTrack(self);
1077 pyrna_struct_clear(self);
1079 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
1081 /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
1082 Py_TYPE(self)->tp_free(self);
1085 #ifdef USE_PYRNA_STRUCT_REFERENCE
1086 static void pyrna_struct_reference_set(BPy_StructRNA *self, PyObject *reference)
1088 if (self->reference) {
1089 // PyObject_GC_UnTrack(self); /* INITIALIZED TRACKED ? */
1090 pyrna_struct_clear(self);
1092 /* reference is now NULL */
1095 self->reference = reference;
1096 Py_INCREF(reference);
1097 // PyObject_GC_Track(self); /* INITIALIZED TRACKED ? */
1100 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
1102 /* use our own dealloc so we can free a property if we use one */
1103 static void pyrna_prop_dealloc(BPy_PropertyRNA *self)
1106 if (self->in_weakreflist != NULL) {
1107 PyObject_ClearWeakRefs((PyObject *)self);
1110 /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
1111 Py_TYPE(self)->tp_free(self);
1114 static void pyrna_prop_array_dealloc(BPy_PropertyRNA *self)
1117 if (self->in_weakreflist != NULL) {
1118 PyObject_ClearWeakRefs((PyObject *)self);
1121 /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
1122 Py_TYPE(self)->tp_free(self);
1125 static const char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
1127 EnumPropertyItem *item;
1131 RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
1133 result = BPy_enum_as_string(item);
1146 static int pyrna_string_to_enum(PyObject *item, PointerRNA *ptr, PropertyRNA *prop, int *val, const char *error_prefix)
1148 const char *param = _PyUnicode_AsString(item);
1150 if (param == NULL) {
1151 PyErr_Format(PyExc_TypeError,
1152 "%.200s expected a string enum, not %.200s",
1153 error_prefix, Py_TYPE(item)->tp_name);
1157 if (!RNA_property_enum_value(BPy_GetContext(), ptr, prop, param, val)) {
1158 const char *enum_str = pyrna_enum_as_string(ptr, prop);
1159 PyErr_Format(PyExc_TypeError,
1160 "%.200s enum \"%.200s\" not found in (%.200s)",
1161 error_prefix, param, enum_str);
1162 MEM_freeN((void *)enum_str);
1170 /* 'value' _must_ be a set type, error check before calling */
1171 int pyrna_set_to_enum_bitfield(EnumPropertyItem *items, PyObject *value, int *r_value, const char *error_prefix)
1173 /* set of enum items, concatenate all values with OR */
1178 Py_ssize_t hash = 0;
1183 while (_PySet_NextEntry(value, &pos, &key, &hash)) {
1184 const char *param = _PyUnicode_AsString(key);
1186 if (param == NULL) {
1187 PyErr_Format(PyExc_TypeError,
1188 "%.200s expected a string, not %.200s",
1189 error_prefix, Py_TYPE(key)->tp_name);
1193 if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) == -1) {
1204 static int pyrna_prop_to_enum_bitfield(PointerRNA *ptr, PropertyRNA *prop, PyObject *value, int *r_value, const char *error_prefix)
1206 EnumPropertyItem *item;
1212 if (!PyAnySet_Check(value)) {
1213 PyErr_Format(PyExc_TypeError,
1214 "%.200s, %.200s.%.200s expected a set, not a %.200s",
1215 error_prefix, RNA_struct_identifier(ptr->type),
1216 RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1220 RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
1223 ret = pyrna_set_to_enum_bitfield(item, value, r_value, error_prefix);
1226 if (PySet_GET_SIZE(value)) {
1227 PyErr_Format(PyExc_TypeError,
1228 "%.200s: empty enum \"%.200s\" could not have any values assigned",
1229 error_prefix, RNA_property_identifier(prop));
1243 PyObject *pyrna_enum_bitfield_to_py(EnumPropertyItem *items, int value)
1245 PyObject *ret = PySet_New(NULL);
1246 const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];
1248 if (RNA_enum_bitflag_identifiers(items, value, identifier)) {
1251 for (index = 0; identifier[index]; index++) {
1252 item = PyUnicode_FromString(identifier[index]);
1253 PySet_Add(ret, item);
1261 static PyObject *pyrna_enum_to_py(PointerRNA *ptr, PropertyRNA *prop, int val)
1263 PyObject *item, *ret = NULL;
1265 if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1266 const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];
1268 ret = PySet_New(NULL);
1270 if (RNA_property_enum_bitflag_identifiers(BPy_GetContext(), ptr, prop, val, identifier)) {
1273 for (index = 0; identifier[index]; index++) {
1274 item = PyUnicode_FromString(identifier[index]);
1275 PySet_Add(ret, item);
1282 const char *identifier;
1283 if (RNA_property_enum_identifier(BPy_GetContext(), ptr, prop, val, &identifier)) {
1284 ret = PyUnicode_FromString(identifier);
1287 EnumPropertyItem *enum_item;
1290 /* don't throw error here, can't trust blender 100% to give the
1291 * right values, python code should not generate error for that */
1292 RNA_property_enum_items(BPy_GetContext(), ptr, prop, &enum_item, NULL, &free);
1293 if (enum_item && enum_item->identifier) {
1294 ret = PyUnicode_FromString(enum_item->identifier);
1297 const char *ptr_name = RNA_struct_name_get_alloc(ptr, NULL, 0, NULL);
1299 /* prefer not fail silently in case of api errors, maybe disable it later */
1300 printf("RNA Warning: Current value \"%d\" "
1301 "matches no enum in '%s', '%s', '%s'\n",
1302 val, RNA_struct_identifier(ptr->type),
1303 ptr_name, RNA_property_identifier(prop));
1305 #if 0 /* gives python decoding errors while generating docs :( */
1306 char error_str[256];
1307 BLI_snprintf(error_str, sizeof(error_str),
1308 "RNA Warning: Current value \"%d\" "
1309 "matches no enum in '%s', '%s', '%s'",
1310 val, RNA_struct_identifier(ptr->type),
1311 ptr_name, RNA_property_identifier(prop));
1313 PyErr_Warn(PyExc_RuntimeWarning, error_str);
1317 MEM_freeN((void *)ptr_name);
1319 ret = PyUnicode_FromString("");
1323 MEM_freeN(enum_item);
1325 PyErr_Format(PyExc_AttributeError,
1326 "RNA Error: Current value \"%d\" matches no enum", val);
1335 PyObject *pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
1338 const int type = RNA_property_type(prop);
1340 if (RNA_property_array_check(prop)) {
1341 return pyrna_py_from_array(ptr, prop);
1344 /* see if we can coerce into a python type - PropertyType */
1347 ret = PyBool_FromLong(RNA_property_boolean_get(ptr, prop));
1350 ret = PyLong_FromLong(RNA_property_int_get(ptr, prop));
1353 ret = PyFloat_FromDouble(RNA_property_float_get(ptr, prop));
1357 const int subtype = RNA_property_subtype(prop);
1362 buf = RNA_property_string_get_alloc(ptr, prop, buf_fixed, sizeof(buf_fixed), &buf_len);
1363 #ifdef USE_STRING_COERCE
1364 /* only file paths get special treatment, they may contain non utf-8 chars */
1365 if (subtype == PROP_BYTESTRING) {
1366 ret = PyBytes_FromStringAndSize(buf, buf_len);
1368 else if (ELEM3(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
1369 ret = PyC_UnicodeFromByteAndSize(buf, buf_len);
1372 ret = PyUnicode_FromStringAndSize(buf, buf_len);
1374 #else /* USE_STRING_COERCE */
1375 if (subtype == PROP_BYTESTRING) {
1376 ret = PyBytes_FromStringAndSize(buf, buf_len);
1379 ret = PyUnicode_FromStringAndSize(buf, buf_len);
1381 #endif /* USE_STRING_COERCE */
1382 if (buf_fixed != buf) {
1383 MEM_freeN((void *)buf);
1389 ret = pyrna_enum_to_py(ptr, prop, RNA_property_enum_get(ptr, prop));
1395 newptr = RNA_property_pointer_get(ptr, prop);
1397 ret = pyrna_struct_CreatePyObject(&newptr);
1405 case PROP_COLLECTION:
1406 ret = pyrna_prop_CreatePyObject(ptr, prop);
1409 PyErr_Format(PyExc_TypeError,
1410 "bpy_struct internal error: unknown type '%d' (pyrna_prop_to_py)", type);
1418 /* This function is used by operators and converting dicts into collections.
1419 * Its takes keyword args and fills them with property values */
1420 int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, int all_args, const char *error_prefix)
1424 const char *arg_name = NULL;
1427 totkw = kw ? PyDict_Size(kw) : 0;
1429 RNA_STRUCT_BEGIN (ptr, prop)
1431 arg_name = RNA_property_identifier(prop);
1433 if (STREQ(arg_name, "rna_type")) {
1438 PyErr_Format(PyExc_TypeError,
1439 "%.200s: no keywords, expected \"%.200s\"",
1440 error_prefix, arg_name ? arg_name : "<UNKNOWN>");
1445 item = PyDict_GetItemString(kw, arg_name); /* wont set an error */
1449 PyErr_Format(PyExc_TypeError,
1450 "%.200s: keyword \"%.200s\" missing",
1451 error_prefix, arg_name ? arg_name : "<UNKNOWN>");
1452 error_val = -1; /* pyrna_py_to_prop sets the error */
1457 if (pyrna_py_to_prop(ptr, prop, NULL, item, error_prefix)) {
1466 if (error_val == 0 && totkw > 0) { /* some keywords were given that were not used :/ */
1467 PyObject *key, *value;
1470 while (PyDict_Next(kw, &pos, &key, &value)) {
1471 arg_name = _PyUnicode_AsString(key);
1472 if (RNA_struct_find_property(ptr, arg_name) == NULL) break;
1476 PyErr_Format(PyExc_TypeError,
1477 "%.200s: keyword \"%.200s\" unrecognized",
1478 error_prefix, arg_name ? arg_name : "<UNKNOWN>");
1486 static PyObject *pyrna_func_to_py(const PointerRNA *ptr, FunctionRNA *func)
1488 BPy_FunctionRNA *pyfunc = (BPy_FunctionRNA *) PyObject_NEW(BPy_FunctionRNA, &pyrna_func_Type);
1490 pyfunc->func = func;
1491 return (PyObject *)pyfunc;
1495 static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value, const char *error_prefix)
1497 /* XXX hard limits should be checked here */
1498 const int type = RNA_property_type(prop);
1501 if (RNA_property_array_check(prop)) {
1502 /* done getting the length */
1503 if (pyrna_py_to_array(ptr, prop, data, value, error_prefix) == -1) {
1508 /* Normal Property (not an array) */
1510 /* see if we can coerce into a python type - PropertyType */
1515 /* prefer not to have an exception here
1516 * however so many poll functions return None or a valid Object.
1517 * its a hassle to convert these into a bool before returning, */
1518 if (RNA_property_flag(prop) & PROP_OUTPUT) {
1519 param = PyObject_IsTrue(value);
1522 param = PyLong_AsLong(value);
1524 if (UNLIKELY(param & ~1)) { /* only accept 0/1 */
1525 param = -1; /* error out below */
1530 PyErr_Format(PyExc_TypeError,
1531 "%.200s %.200s.%.200s expected True/False or 0/1, not %.200s",
1532 error_prefix, RNA_struct_identifier(ptr->type),
1533 RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1537 if (data) *((int *)data) = param;
1538 else RNA_property_boolean_set(ptr, prop, param);
1545 long param = PyLong_AsLongAndOverflow(value, &overflow);
1546 if (overflow || (param > INT_MAX) || (param < INT_MIN)) {
1547 PyErr_Format(PyExc_ValueError,
1548 "%.200s %.200s.%.200s value not in 'int' range "
1549 "(" STRINGIFY(INT_MIN) ", " STRINGIFY(INT_MAX) ")",
1550 error_prefix, RNA_struct_identifier(ptr->type),
1551 RNA_property_identifier(prop));
1554 else if (param == -1 && PyErr_Occurred()) {
1555 PyErr_Format(PyExc_TypeError,
1556 "%.200s %.200s.%.200s expected an int type, not %.200s",
1557 error_prefix, RNA_struct_identifier(ptr->type),
1558 RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1562 int param_i = (int)param;
1564 RNA_property_int_clamp(ptr, prop, ¶m_i);
1565 *((int *)data) = param_i;
1568 RNA_property_int_set(ptr, prop, param_i);
1575 float param = PyFloat_AsDouble(value);
1576 if (PyErr_Occurred()) {
1577 PyErr_Format(PyExc_TypeError,
1578 "%.200s %.200s.%.200s expected a float type, not %.200s",
1579 error_prefix, RNA_struct_identifier(ptr->type),
1580 RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1585 RNA_property_float_clamp(ptr, prop, (float *)¶m);
1586 *((float *)data) = param;
1589 RNA_property_float_set(ptr, prop, param);
1596 const int subtype = RNA_property_subtype(prop);
1599 if (subtype == PROP_BYTESTRING) {
1603 param = PyBytes_AsString(value);
1605 if (param == NULL) {
1606 if (PyBytes_Check(value)) {
1607 /* there was an error assigning a string type,
1608 * rather than setting a new error, prefix the existing one
1610 PyC_Err_Format_Prefix(PyExc_TypeError,
1611 "%.200s %.200s.%.200s error assigning bytes",
1612 error_prefix, RNA_struct_identifier(ptr->type),
1613 RNA_property_identifier(prop));
1616 PyErr_Format(PyExc_TypeError,
1617 "%.200s %.200s.%.200s expected a bytes type, not %.200s",
1618 error_prefix, RNA_struct_identifier(ptr->type),
1619 RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1625 /* same as unicode */
1626 if (data) *((char **)data) = (char *)param; /*XXX, this is suspect but needed for function calls, need to see if theres a better way */
1627 else RNA_property_string_set(ptr, prop, param);
1631 /* Unicode String */
1632 #ifdef USE_STRING_COERCE
1633 PyObject *value_coerce = NULL;
1634 if (ELEM3(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
1635 /* TODO, get size */
1636 param = PyC_UnicodeAsByte(value, &value_coerce);
1639 param = _PyUnicode_AsString(value);
1641 #else /* USE_STRING_COERCE */
1642 param = _PyUnicode_AsString(value);
1643 #endif /* USE_STRING_COERCE */
1645 if (param == NULL) {
1646 if (PyUnicode_Check(value)) {
1647 /* there was an error assigning a string type,
1648 * rather than setting a new error, prefix the existing one
1650 PyC_Err_Format_Prefix(PyExc_TypeError,
1651 "%.200s %.200s.%.200s error assigning string",
1652 error_prefix, RNA_struct_identifier(ptr->type),
1653 RNA_property_identifier(prop));
1656 PyErr_Format(PyExc_TypeError,
1657 "%.200s %.200s.%.200s expected a string type, not %.200s",
1658 error_prefix, RNA_struct_identifier(ptr->type),
1659 RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1666 if (data) *((char **)data) = (char *)param; /*XXX, this is suspect but needed for function calls, need to see if theres a better way */
1667 else RNA_property_string_set(ptr, prop, param);
1669 #ifdef USE_STRING_COERCE
1670 Py_XDECREF(value_coerce);
1671 #endif /* USE_STRING_COERCE */
1679 /* type checkins is done by each function */
1680 if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1681 /* set of enum items, concatenate all values with OR */
1682 if (pyrna_prop_to_enum_bitfield(ptr, prop, value, &val, error_prefix) == -1) {
1687 /* simple enum string */
1688 if (pyrna_string_to_enum(value, ptr, prop, &val, error_prefix) == -1) {
1693 if (data) *((int *)data) = val;
1694 else RNA_property_enum_set(ptr, prop, val);
1700 PyObject *value_new = NULL;
1702 StructRNA *ptr_type = RNA_property_pointer_type(ptr, prop);
1703 int flag = RNA_property_flag(prop);
1705 /* this is really nasty!, so we can fake the operator having direct properties eg:
1706 * layout.prop(self, "filepath")
1707 * ... which in fact should be
1708 * layout.prop(self.properties, "filepath")
1710 * we need to do this trick.
1711 * if the prop is not an operator type and the pyobject is an operator,
1712 * use its properties in place of its self.
1714 * this is so bad that its almost a good reason to do away with fake 'self.properties -> self' class mixing
1715 * if this causes problems in the future it should be removed.
1717 if ((ptr_type == &RNA_AnyType) &&
1718 (BPy_StructRNA_Check(value)) &&
1719 (RNA_struct_is_a(((BPy_StructRNA *)value)->ptr.type, &RNA_Operator)))
1721 value = PyObject_GetAttrString(value, "properties");
1726 /* if property is an OperatorProperties pointer and value is a map,
1727 * forward back to pyrna_pydict_to_props */
1728 if (RNA_struct_is_a(ptr_type, &RNA_OperatorProperties) && PyDict_Check(value)) {
1729 PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
1730 return pyrna_pydict_to_props(&opptr, value, 0, error_prefix);
1733 /* another exception, allow to pass a collection as an RNA property */
1734 if (Py_TYPE(value) == &pyrna_prop_collection_Type) { /* ok to ignore idprop collections */
1736 BPy_PropertyRNA *value_prop = (BPy_PropertyRNA *)value;
1737 if (RNA_property_collection_type_get(&value_prop->ptr, value_prop->prop, &c_ptr)) {
1738 value = pyrna_struct_CreatePyObject(&c_ptr);
1742 PyErr_Format(PyExc_TypeError,
1743 "%.200s %.200s.%.200s collection has no type, "
1744 "cant be used as a %.200s type",
1745 error_prefix, RNA_struct_identifier(ptr->type),
1746 RNA_property_identifier(prop), RNA_struct_identifier(ptr_type));
1751 if (!BPy_StructRNA_Check(value) && value != Py_None) {
1752 PyErr_Format(PyExc_TypeError,
1753 "%.200s %.200s.%.200s expected a %.200s type, not %.200s",
1754 error_prefix, RNA_struct_identifier(ptr->type),
1755 RNA_property_identifier(prop), RNA_struct_identifier(ptr_type),
1756 Py_TYPE(value)->tp_name);
1757 Py_XDECREF(value_new); return -1;
1759 else if ((flag & PROP_NEVER_NULL) && value == Py_None) {
1760 PyErr_Format(PyExc_TypeError,
1761 "%.200s %.200s.%.200s does not support a 'None' assignment %.200s type",
1762 error_prefix, RNA_struct_identifier(ptr->type),
1763 RNA_property_identifier(prop), RNA_struct_identifier(ptr_type));
1764 Py_XDECREF(value_new); return -1;
1766 else if ((value != Py_None) &&
1767 ((flag & PROP_ID_SELF_CHECK) && ptr->id.data == ((BPy_StructRNA *)value)->ptr.id.data))
1769 PyErr_Format(PyExc_TypeError,
1770 "%.200s %.200s.%.200s ID type does not support assignment to its self",
1771 error_prefix, RNA_struct_identifier(ptr->type),
1772 RNA_property_identifier(prop));
1773 Py_XDECREF(value_new); return -1;
1776 BPy_StructRNA *param = (BPy_StructRNA *)value;
1777 bool raise_error = false;
1780 if (flag & PROP_RNAPTR) {
1781 if (flag & PROP_THICK_WRAP) {
1782 if (value == Py_None)
1783 memset(data, 0, sizeof(PointerRNA));
1784 else if (RNA_struct_is_a(param->ptr.type, ptr_type))
1785 *((PointerRNA *)data) = param->ptr;
1790 /* for function calls, we sometimes want to pass the 'ptr' directly,
1791 * watch out that it remains valid!, possibly we could support this later if needed */
1792 BLI_assert(value_new == NULL);
1793 if (value == Py_None)
1794 *((void **)data) = NULL;
1795 else if (RNA_struct_is_a(param->ptr.type, ptr_type))
1796 *((PointerRNA **)data) = ¶m->ptr;
1801 else if (value == Py_None) {
1802 *((void **)data) = NULL;
1804 else if (RNA_struct_is_a(param->ptr.type, ptr_type)) {
1805 *((void **)data) = param->ptr.data;
1812 /* data == NULL, assign to RNA */
1813 if (value == Py_None) {
1814 PointerRNA valueptr = {{NULL}};
1815 RNA_property_pointer_set(ptr, prop, valueptr);
1817 else if (RNA_struct_is_a(param->ptr.type, ptr_type)) {
1818 RNA_property_pointer_set(ptr, prop, param->ptr);
1822 RNA_pointer_create(NULL, ptr_type, NULL, &tmp);
1823 PyErr_Format(PyExc_TypeError,
1824 "%.200s %.200s.%.200s expected a %.200s type. not %.200s",
1825 error_prefix, RNA_struct_identifier(ptr->type),
1826 RNA_property_identifier(prop), RNA_struct_identifier(tmp.type),
1827 RNA_struct_identifier(param->ptr.type));
1828 Py_XDECREF(value_new); return -1;
1834 RNA_pointer_create(NULL, ptr_type, NULL, &tmp);
1835 PyErr_Format(PyExc_TypeError,
1836 "%.200s %.200s.%.200s expected a %.200s type, not %.200s",
1837 error_prefix, RNA_struct_identifier(ptr->type),
1838 RNA_property_identifier(prop), RNA_struct_identifier(tmp.type),
1839 RNA_struct_identifier(param->ptr.type));
1840 Py_XDECREF(value_new); return -1;
1844 Py_XDECREF(value_new);
1848 case PROP_COLLECTION:
1850 Py_ssize_t seq_len, i;
1854 CollectionPointerLink *link;
1856 lb = (data) ? (ListBase *)data : NULL;
1858 /* convert a sequence of dict's into a collection */
1859 if (!PySequence_Check(value)) {
1860 PyErr_Format(PyExc_TypeError,
1861 "%.200s %.200s.%.200s expected a sequence for an RNA collection, not %.200s",
1862 error_prefix, RNA_struct_identifier(ptr->type),
1863 RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1867 seq_len = PySequence_Size(value);
1868 for (i = 0; i < seq_len; i++) {
1869 item = PySequence_GetItem(value, i);
1872 PyErr_Format(PyExc_TypeError,
1873 "%.200s %.200s.%.200s failed to get sequence index '%d' for an RNA collection",
1874 error_prefix, RNA_struct_identifier(ptr->type),
1875 RNA_property_identifier(prop), i);
1880 if (PyDict_Check(item) == 0) {
1881 PyErr_Format(PyExc_TypeError,
1882 "%.200s %.200s.%.200s expected a each sequence "
1883 "member to be a dict for an RNA collection, not %.200s",
1884 error_prefix, RNA_struct_identifier(ptr->type),
1885 RNA_property_identifier(prop), Py_TYPE(item)->tp_name);
1891 link = MEM_callocN(sizeof(CollectionPointerLink), "PyCollectionPointerLink");
1892 link->ptr = itemptr;
1893 BLI_addtail(lb, link);
1896 RNA_property_collection_add(ptr, prop, &itemptr);
1898 if (pyrna_pydict_to_props(&itemptr, item, 1, "Converting a python list to an RNA collection") == -1) {
1899 PyObject *msg = PyC_ExceptionBuffer();
1900 const char *msg_char = _PyUnicode_AsString(msg);
1902 PyErr_Format(PyExc_TypeError,
1903 "%.200s %.200s.%.200s error converting a member of a collection "
1904 "from a dicts into an RNA collection, failed with: %s",
1905 error_prefix, RNA_struct_identifier(ptr->type),
1906 RNA_property_identifier(prop), msg_char);
1918 PyErr_Format(PyExc_AttributeError,
1919 "%.200s %.200s.%.200s unknown property type (pyrna_py_to_prop)",
1920 error_prefix, RNA_struct_identifier(ptr->type),
1921 RNA_property_identifier(prop));
1926 /* Run rna property functions */
1927 if (RNA_property_update_check(prop)) {
1928 RNA_property_update(BPy_GetContext(), ptr, prop);
1934 static PyObject *pyrna_prop_array_to_py_index(BPy_PropertyArrayRNA *self, int index)
1936 PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
1937 return pyrna_py_from_array_index(self, &self->ptr, self->prop, index);
1940 static int pyrna_py_to_prop_array_index(BPy_PropertyArrayRNA *self, int index, PyObject *value)
1943 PointerRNA *ptr = &self->ptr;
1944 PropertyRNA *prop = self->prop;
1946 const int totdim = RNA_property_array_dimension(ptr, prop, NULL);
1949 /* char error_str[512]; */
1950 if (pyrna_py_to_array_index(&self->ptr, self->prop, self->arraydim, self->arrayoffset, index, value, "") == -1) {
1956 /* see if we can coerce into a python type - PropertyType */
1957 switch (RNA_property_type(prop)) {
1960 int param = PyLong_AsLong(value);
1962 if (param < 0 || param > 1) {
1963 PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1");
1967 RNA_property_boolean_set_index(ptr, prop, index, param);
1973 int param = PyLong_AsLong(value);
1974 if (param == -1 && PyErr_Occurred()) {
1975 PyErr_SetString(PyExc_TypeError, "expected an int type");
1979 RNA_property_int_clamp(ptr, prop, ¶m);
1980 RNA_property_int_set_index(ptr, prop, index, param);
1986 float param = PyFloat_AsDouble(value);
1987 if (PyErr_Occurred()) {
1988 PyErr_SetString(PyExc_TypeError, "expected a float type");
1992 RNA_property_float_clamp(ptr, prop, ¶m);
1993 RNA_property_float_set_index(ptr, prop, index, param);
1998 PyErr_SetString(PyExc_AttributeError, "not an array type");
2004 /* Run rna property functions */
2005 if (RNA_property_update_check(prop)) {
2006 RNA_property_update(BPy_GetContext(), ptr, prop);
2012 /* ---------------sequence------------------------------------------- */
2013 static Py_ssize_t pyrna_prop_array_length(BPy_PropertyArrayRNA *self)
2015 PYRNA_PROP_CHECK_INT((BPy_PropertyRNA *)self);
2017 if (RNA_property_array_dimension(&self->ptr, self->prop, NULL) > 1)
2018 return RNA_property_multi_array_length(&self->ptr, self->prop, self->arraydim);
2020 return RNA_property_array_length(&self->ptr, self->prop);
2023 static Py_ssize_t pyrna_prop_collection_length(BPy_PropertyRNA *self)
2025 PYRNA_PROP_CHECK_INT(self);
2027 return RNA_property_collection_length(&self->ptr, self->prop);
2030 /* bool functions are for speed, so we can avoid getting the length
2031 * of 1000's of items in a linked list for eg. */
2032 static int pyrna_prop_array_bool(BPy_PropertyRNA *self)
2034 PYRNA_PROP_CHECK_INT(self);
2036 return RNA_property_array_length(&self->ptr, self->prop) ? 1 : 0;
2039 static int pyrna_prop_collection_bool(BPy_PropertyRNA *self)
2041 /* no callback defined, just iterate and find the nth item */
2042 CollectionPropertyIterator iter;
2045 PYRNA_PROP_CHECK_INT(self);
2047 RNA_property_collection_begin(&self->ptr, self->prop, &iter);
2049 RNA_property_collection_end(&iter);
2054 /* notice getting the length of the collection is avoided unless negative
2055 * index is used or to detect internal error with a valid index.
2056 * This is done for faster lookups. */
2057 #define PYRNA_PROP_COLLECTION_ABS_INDEX(ret_err) \
2059 keynum_abs += RNA_property_collection_length(&self->ptr, self->prop); \
2060 if (keynum_abs < 0) { \
2061 PyErr_Format(PyExc_IndexError, \
2062 "bpy_prop_collection[%d]: out of range.", keynum); \
2068 /* internal use only */
2069 static PyObject *pyrna_prop_collection_subscript_int(BPy_PropertyRNA *self, Py_ssize_t keynum)
2072 Py_ssize_t keynum_abs = keynum;
2074 PYRNA_PROP_CHECK_OBJ(self);
2076 PYRNA_PROP_COLLECTION_ABS_INDEX(NULL);
2078 if (RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum_abs, &newptr)) {
2079 return pyrna_struct_CreatePyObject(&newptr);
2082 const int len = RNA_property_collection_length(&self->ptr, self->prop);
2083 if (keynum_abs >= len) {
2084 PyErr_Format(PyExc_IndexError,
2085 "bpy_prop_collection[index]: "
2086 "index %d out of range, size %d", keynum, len);
2089 PyErr_Format(PyExc_RuntimeError,
2090 "bpy_prop_collection[index]: internal error, "
2091 "valid index %d given in %d sized collection but value not found",
2099 /* values type must have been already checked */
2100 static int pyrna_prop_collection_ass_subscript_int(BPy_PropertyRNA *self, Py_ssize_t keynum, PyObject *value)
2102 Py_ssize_t keynum_abs = keynum;
2103 const PointerRNA *ptr = (value == Py_None) ? (&PointerRNA_NULL) : &((BPy_StructRNA *)value)->ptr;
2105 PYRNA_PROP_CHECK_INT(self);
2107 PYRNA_PROP_COLLECTION_ABS_INDEX(-1);
2109 if (RNA_property_collection_assign_int(&self->ptr, self->prop, keynum_abs, ptr) == 0) {
2110 const int len = RNA_property_collection_length(&self->ptr, self->prop);
2111 if (keynum_abs >= len) {
2112 PyErr_Format(PyExc_IndexError,
2113 "bpy_prop_collection[index] = value: "
2114 "index %d out of range, size %d", keynum, len);
2118 PyErr_Format(PyExc_IndexError,
2119 "bpy_prop_collection[index] = value: "
2120 "failed assignment (unknown reason)", keynum);
2128 static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyArrayRNA *self, int keynum)
2132 PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
2134 len = pyrna_prop_array_length(self);
2136 if (keynum < 0) keynum += len;
2138 if (keynum >= 0 && keynum < len)
2139 return pyrna_prop_array_to_py_index(self, keynum);
2141 PyErr_Format(PyExc_IndexError,
2142 "bpy_prop_array[index]: index %d out of range", keynum);
2146 static PyObject *pyrna_prop_collection_subscript_str(BPy_PropertyRNA *self, const char *keyname)
2150 PYRNA_PROP_CHECK_OBJ(self);
2152 if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
2153 return pyrna_struct_CreatePyObject(&newptr);
2155 PyErr_Format(PyExc_KeyError, "bpy_prop_collection[key]: key \"%.200s\" not found", keyname);
2158 /* static PyObject *pyrna_prop_array_subscript_str(BPy_PropertyRNA *self, char *keyname) */
2160 /* special case: bpy.data.objects["some_id_name", "//some_lib_name.blend"]
2161 * also for: bpy.data.objects.get(("some_id_name", "//some_lib_name.blend"), fallback)
2164 * error codes since this is not to be called directly from python,
2165 * this matches pythons __contains__ values capi.
2169 static int pyrna_prop_collection_subscript_str_lib_pair_ptr(BPy_PropertyRNA *self, PyObject *key,
2170 const char *err_prefix, const short err_not_found,
2174 const char *keyname;
2176 /* first validate the args, all we know is that they are a tuple */
2177 if (PyTuple_GET_SIZE(key) != 2) {
2178 PyErr_Format(PyExc_KeyError,
2179 "%s: tuple key must be a pair, not size %d",
2180 err_prefix, PyTuple_GET_SIZE(key));
2183 else if (self->ptr.type != &RNA_BlendData) {
2184 PyErr_Format(PyExc_KeyError,
2185 "%s: is only valid for bpy.data collections, not %.200s",
2186 err_prefix, RNA_struct_identifier(self->ptr.type));
2189 else if ((keyname = _PyUnicode_AsString(PyTuple_GET_ITEM(key, 0))) == NULL) {
2190 PyErr_Format(PyExc_KeyError,
2191 "%s: id must be a string, not %.200s",
2192 err_prefix, Py_TYPE(PyTuple_GET_ITEM(key, 0))->tp_name);
2196 PyObject *keylib = PyTuple_GET_ITEM(key, 1);
2200 if (keylib == Py_None) {
2203 else if (PyUnicode_Check(keylib)) {
2204 Main *bmain = self->ptr.data;
2205 const char *keylib_str = _PyUnicode_AsString(keylib);
2206 lib = BLI_findstring(&bmain->library, keylib_str, offsetof(Library, name));
2208 if (err_not_found) {
2209 PyErr_Format(PyExc_KeyError,
2210 "%s: lib name '%.240s' "
2211 "does not reference a valid library",
2212 err_prefix, keylib_str);
2222 PyErr_Format(PyExc_KeyError,
2223 "%s: lib must be a sting or None, not %.200s",
2224 err_prefix, Py_TYPE(keylib)->tp_name);
2228 /* lib is either a valid pointer or NULL,
2229 * either way can do direct comparison with id.lib */
2231 RNA_PROP_BEGIN (&self->ptr, itemptr, self->prop)
2233 ID *id = itemptr.data; /* always an ID */
2234 if (id->lib == lib && (STREQLEN(keyname, id->name + 2, sizeof(id->name) - 2))) {
2244 /* we may want to fail silently as with collection.get() */
2245 if ((found == false) && err_not_found) {
2246 /* only runs for getitem access so use fixed string */
2247 PyErr_SetString(PyExc_KeyError,
2248 "bpy_prop_collection[key, lib]: not found");
2252 return found; /* 1 / 0, no exception */
2257 static PyObject *pyrna_prop_collection_subscript_str_lib_pair(BPy_PropertyRNA *self, PyObject *key,
2258 const char *err_prefix, const bool err_not_found)
2261 const int contains = pyrna_prop_collection_subscript_str_lib_pair_ptr(self, key, err_prefix, err_not_found, &ptr);
2263 if (contains == 1) {
2264 return pyrna_struct_CreatePyObject(&ptr);
2272 static PyObject *pyrna_prop_collection_subscript_slice(BPy_PropertyRNA *self, Py_ssize_t start, Py_ssize_t stop)
2274 CollectionPropertyIterator rna_macro_iter;
2280 PYRNA_PROP_CHECK_OBJ(self);
2282 list = PyList_New(0);
2285 RNA_property_collection_begin(&self->ptr, self->prop, &rna_macro_iter);
2286 RNA_property_collection_skip(&rna_macro_iter, start);
2288 /* add items until stop */
2289 for (count = start; rna_macro_iter.valid;
2290 RNA_property_collection_next(&rna_macro_iter))
2292 item = pyrna_struct_CreatePyObject(&rna_macro_iter.ptr);
2293 PyList_Append(list, item);
2297 if (count == stop) {
2302 RNA_property_collection_end(&rna_macro_iter);
2307 /* TODO - dimensions
2308 * note: could also use pyrna_prop_array_to_py_index(self, count) in a loop but its a lot slower
2309 * since at the moment it reads (and even allocates) the entire array for each index.
2311 static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop,
2312 Py_ssize_t start, Py_ssize_t stop, Py_ssize_t length)
2317 PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
2319 tuple = PyTuple_New(stop - start);
2321 /* PYRNA_PROP_CHECK_OBJ(self); isn't needed, internal use only */
2323 totdim = RNA_property_array_dimension(ptr, prop, NULL);
2326 for (count = start; count < stop; count++)
2327 PyTuple_SET_ITEM(tuple, count - start, pyrna_prop_array_to_py_index(self, count));
2330 switch (RNA_property_type(prop)) {
2333 float values_stack[PYRNA_STACK_ARRAY];
2335 if (length > PYRNA_STACK_ARRAY) { values = PyMem_MALLOC(sizeof(float) * length); }
2336 else { values = values_stack; }
2337 RNA_property_float_get_array(ptr, prop, values);
2339 for (count = start; count < stop; count++)
2340 PyTuple_SET_ITEM(tuple, count - start, PyFloat_FromDouble(values[count]));
2342 if (values != values_stack) {
2349 int values_stack[PYRNA_STACK_ARRAY];
2351 if (length > PYRNA_STACK_ARRAY) { values = PyMem_MALLOC(sizeof(int) * length); }
2352 else { values = values_stack; }
2354 RNA_property_boolean_get_array(ptr, prop, values);
2355 for (count = start; count < stop; count++)
2356 PyTuple_SET_ITEM(tuple, count - start, PyBool_FromLong(values[count]));
2358 if (values != values_stack) {
2365 int values_stack[PYRNA_STACK_ARRAY];
2367 if (length > PYRNA_STACK_ARRAY) { values = PyMem_MALLOC(sizeof(int) * length); }
2368 else { values = values_stack; }
2370 RNA_property_int_get_array(ptr, prop, values);
2371 for (count = start; count < stop; count++)
2372 PyTuple_SET_ITEM(tuple, count - start, PyLong_FromLong(values[count]));
2374 if (values != values_stack) {
2380 BLI_assert(!"Invalid array type");
2382 PyErr_SetString(PyExc_TypeError, "not an array type");
2391 static PyObject *pyrna_prop_collection_subscript(BPy_PropertyRNA *self, PyObject *key)
2393 PYRNA_PROP_CHECK_OBJ(self);
2395 if (PyUnicode_Check(key)) {
2396 return pyrna_prop_collection_subscript_str(self, _PyUnicode_AsString(key));
2398 else if (PyIndex_Check(key)) {
2399 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2400 if (i == -1 && PyErr_Occurred())
2403 return pyrna_prop_collection_subscript_int(self, i);
2405 else if (PySlice_Check(key)) {
2406 PySliceObject *key_slice = (PySliceObject *)key;
2407 Py_ssize_t step = 1;
2409 if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2412 else if (step != 1) {
2413 PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported");
2416 else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2417 return pyrna_prop_collection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
2420 Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
2422 /* avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
2423 if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) return NULL;
2424 if (key_slice->stop != Py_None && !_PyEval_SliceIndex(key_slice->stop, &stop)) return NULL;
2426 if (start < 0 || stop < 0) {
2427 /* only get the length for negative values */
2428 Py_ssize_t len = (Py_ssize_t)RNA_property_collection_length(&self->ptr, self->prop);
2429 if (start < 0) start += len;
2430 if (stop < 0) stop += len;
2433 if (stop - start <= 0) {
2434 return PyList_New(0);
2437 return pyrna_prop_collection_subscript_slice(self, start, stop);
2441 else if (PyTuple_Check(key)) {
2442 /* special case, for ID datablocks we */
2443 return pyrna_prop_collection_subscript_str_lib_pair(self, key,
2444 "bpy_prop_collection[id, lib]", true);
2447 PyErr_Format(PyExc_TypeError,
2448 "bpy_prop_collection[key]: invalid key, "
2449 "must be a string or an int, not %.200s",
2450 Py_TYPE(key)->tp_name);
2455 /* generic check to see if a PyObject is compatible with a collection
2456 * -1 on failure, 0 on success, sets the error */
2457 static int pyrna_prop_collection_type_check(BPy_PropertyRNA *self, PyObject *value)
2459 StructRNA *prop_srna;
2461 if (value == Py_None) {
2462 if (RNA_property_flag(self->prop) & PROP_NEVER_NULL) {
2463 PyErr_Format(PyExc_TypeError,
2464 "bpy_prop_collection[key] = value: invalid, "
2465 "this collection doesn't support None assignment");
2469 return 0; /* None is OK */
2472 else if (BPy_StructRNA_Check(value) == 0) {
2473 PyErr_Format(PyExc_TypeError,
2474 "bpy_prop_collection[key] = value: invalid, "
2475 "expected a StructRNA type or None, not a %.200s",
2476 Py_TYPE(value)->tp_name);
2479 else if ((prop_srna = RNA_property_pointer_type(&self->ptr, self->prop))) {
2480 StructRNA *value_srna = ((BPy_StructRNA *)value)->ptr.type;
2481 if (RNA_struct_is_a(value_srna, prop_srna) == 0) {
2482 PyErr_Format(PyExc_TypeError,
2483 "bpy_prop_collection[key] = value: invalid, "
2484 "expected a '%.200s' type or None, not a '%.200s'",
2485 RNA_struct_identifier(prop_srna),
2486 RNA_struct_identifier(value_srna)
2491 return 0; /* OK, this is the correct type!*/
2495 PyErr_Format(PyExc_TypeError,
2496 "bpy_prop_collection[key] = value: internal error, "
2497 "failed to get the collection type");
2501 /* note: currently this is a copy of 'pyrna_prop_collection_subscript' with
2502 * large blocks commented, we may support slice/key indices later */
2503 static int pyrna_prop_collection_ass_subscript(BPy_PropertyRNA *self, PyObject *key, PyObject *value)
2505 PYRNA_PROP_CHECK_INT(self);
2507 /* validate the assigned value */
2508 if (value == NULL) {
2509 PyErr_SetString(PyExc_TypeError,
2510 "del bpy_prop_collection[key]: not supported");
2513 else if (pyrna_prop_collection_type_check(self, value) == -1) {
2514 return -1; /* exception is set */
2518 if (PyUnicode_Check(key)) {
2519 return pyrna_prop_collection_subscript_str(self, _PyUnicode_AsString(key));
2523 if (PyIndex_Check(key)) {
2524 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2525 if (i == -1 && PyErr_Occurred())
2528 return pyrna_prop_collection_ass_subscript_int(self, i, value);
2530 #if 0 /* TODO, fake slice assignment */
2531 else if (PySlice_Check(key)) {
2532 PySliceObject *key_slice = (PySliceObject *)key;
2533 Py_ssize_t step = 1;
2535 if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2538 else if (step != 1) {
2539 PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported");
2542 else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2543 return pyrna_prop_collection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
2546 Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
2548 /* avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
2549 if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) return NULL;
2550 if (key_slice->stop != Py_None && !_PyEval_SliceIndex(key_slice->stop, &stop)) return NULL;
2552 if (start < 0 || stop < 0) {
2553 /* only get the length for negative values */
2554 Py_ssize_t len = (Py_ssize_t)RNA_property_collection_length(&self->ptr, self->prop);
2555 if (start < 0) start += len;
2556 if (stop < 0) stop += len;
2559 if (stop - start <= 0) {
2560 return PyList_New(0);
2563 return pyrna_prop_collection_subscript_slice(self, start, stop);
2569 PyErr_Format(PyExc_TypeError,
2570 "bpy_prop_collection[key]: invalid key, "
2571 "must be a string or an int, not %.200s",
2572 Py_TYPE(key)->tp_name);
2577 static PyObject *pyrna_prop_array_subscript(BPy_PropertyArrayRNA *self, PyObject *key)
2579 PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
2582 if (PyUnicode_Check(key)) {
2583 return pyrna_prop_array_subscript_str(self, _PyUnicode_AsString(key));
2587 if (PyIndex_Check(key)) {
2588 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2589 if (i == -1 && PyErr_Occurred())
2591 return pyrna_prop_array_subscript_int(self, PyLong_AsLong(key));
2593 else if (PySlice_Check(key)) {
2594 Py_ssize_t step = 1;
2595 PySliceObject *key_slice = (PySliceObject *)key;
2597 if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2600 else if (step != 1) {
2601 PyErr_SetString(PyExc_TypeError, "bpy_prop_array[slice]: slice steps not supported");
2604 else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2605 /* note, no significant advantage with optimizing [:] slice as with collections
2606 * but include here for consistency with collection slice func */
2607 Py_ssize_t len = (Py_ssize_t)pyrna_prop_array_length(self);
2608 return pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, 0, len, len);
2611 int len = pyrna_prop_array_length(self);
2612 Py_ssize_t start, stop, slicelength;
2614 if (PySlice_GetIndicesEx(key, len, &start, &stop, &step, &slicelength) < 0)
2617 if (slicelength <= 0) {
2618 return PyTuple_New(0);
2621 return pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, start, stop, len);
2626 PyErr_SetString(PyExc_AttributeError, "bpy_prop_array[key]: invalid key, key must be an int");
2631 /* could call (pyrna_py_to_prop_array_index(self, i, value) in a loop but it is slow */
2632 static int prop_subscript_ass_array_slice(PointerRNA *ptr, PropertyRNA *prop,
2633 int start, int stop, int length, PyObject *value_orig)
2637 void *values_alloc = NULL;
2640 if (value_orig == NULL) {
2641 PyErr_SetString(PyExc_TypeError,
2642 "bpy_prop_array[slice] = value: deleting with list types is not supported by bpy_struct");
2646 if (!(value = PySequence_Fast(value_orig, "bpy_prop_array[slice] = value: assignment is not a sequence type"))) {
2650 if (PySequence_Fast_GET_SIZE(value) != stop - start) {
2652 PyErr_SetString(PyExc_TypeError,
2653 "bpy_prop_array[slice] = value: re-sizing bpy_struct arrays isn't supported");
2657 switch (RNA_property_type(prop)) {
2660 float values_stack[PYRNA_STACK_ARRAY];
2661 float *values, fval;
2664 RNA_property_float_range(ptr, prop, &min, &max);
2666 if (length > PYRNA_STACK_ARRAY) { values = values_alloc = PyMem_MALLOC(sizeof(float) * length); }
2667 else { values = values_stack; }
2668 if (start != 0 || stop != length) /* partial assignment? - need to get the array */
2669 RNA_property_float_get_array(ptr, prop, values);
2671 for (count = start; count < stop; count++) {
2672 fval = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, count - start));
2673 CLAMP(fval, min, max);
2674 values[count] = fval;
2677 if (PyErr_Occurred()) ret = -1;
2678 else RNA_property_float_set_array(ptr, prop, values);
2683 int values_stack[PYRNA_STACK_ARRAY];
2685 if (length > PYRNA_STACK_ARRAY) { values = values_alloc = PyMem_MALLOC(sizeof(int) * length); }
2686 else { values = values_stack; }
2688 if (start != 0 || stop != length) /* partial assignment? - need to get the array */
2689 RNA_property_boolean_get_array(ptr, prop, values);
2691 for (count = start; count < stop; count++)
2692 values[count] = PyLong_AsLong(PySequence_Fast_GET_ITEM(value, count - start));
2694 if (PyErr_Occurred()) ret = -1;
2695 else RNA_property_boolean_set_array(ptr, prop, values);
2700 int values_stack[PYRNA_STACK_ARRAY];
2704 RNA_property_int_range(ptr, prop, &min, &max);
2706 if (length > PYRNA_STACK_ARRAY) { values = values_alloc = PyMem_MALLOC(sizeof(int) * length); }
2707 else { values = values_stack; }
2709 if (start != 0 || stop != length) /* partial assignment? - need to get the array */
2710 RNA_property_int_get_array(ptr, prop, values);
2712 for (count = start; count < stop; count++) {
2713 ival = PyLong_AsLong(PySequence_Fast_GET_ITEM(value, count - start));
2714 CLAMP(ival, min, max);
2715 values[count] = ival;
2718 if (PyErr_Occurred()) ret = -1;
2719 else RNA_property_int_set_array(ptr, prop, values);
2723 PyErr_SetString(PyExc_TypeError, "not an array type");
2731 PyMem_FREE(values_alloc);
2738 static int prop_subscript_ass_array_int(BPy_PropertyArrayRNA *self, Py_ssize_t keynum, PyObject *value)
2742 PYRNA_PROP_CHECK_INT((BPy_PropertyRNA *)self);
2744 len = pyrna_prop_array_length(self);
2746 if (keynum < 0) keynum += len;
2748 if (keynum >= 0 && keynum < len)
2749 return pyrna_py_to_prop_array_index(self, keynum, value);
2751 PyErr_SetString(PyExc_IndexError,
2752 "bpy_prop_array[index] = value: index out of range");
2756 static int pyrna_prop_array_ass_subscript(BPy_PropertyArrayRNA *self, PyObject *key, PyObject *value)
2758 /* char *keyname = NULL; */ /* not supported yet */
2761 PYRNA_PROP_CHECK_INT((BPy_PropertyRNA *)self);
2763 if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
2764 PyErr_Format(PyExc_AttributeError,
2765 "bpy_prop_collection: attribute \"%.200s\" from \"%.200s\" is read-only",
2766 RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type));
2770 else if (PyIndex_Check(key)) {
2771 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2772 if (i == -1 && PyErr_Occurred()) {
2776 ret = prop_subscript_ass_array_int(self, i, value);
2779 else if (PySlice_Check(key)) {
2780 int len = RNA_property_array_length(&self->ptr, self->prop);
2781 Py_ssize_t start, stop, step, slicelength;
2783 if (PySlice_GetIndicesEx(key, len, &start, &stop, &step, &slicelength) < 0) {
2786 else if (slicelength <= 0) {
2787 ret = 0; /* do nothing */
2789 else if (step == 1) {
2790 ret = prop_subscript_ass_array_slice(&self->ptr, self->prop, start, stop, len, value);
2793 PyErr_SetString(PyExc_TypeError, "slice steps not supported with rna");
2798 PyErr_SetString(PyExc_AttributeError, "invalid key, key must be an int");
2803 if (RNA_property_update_check(self->prop)) {
2804 RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
2811 /* for slice only */
2812 static PyMappingMethods pyrna_prop_array_as_mapping = {
2813 (lenfunc) pyrna_prop_array_length, /* mp_length */
2814 (binaryfunc) pyrna_prop_array_subscript, /* mp_subscript */
2815 (objobjargproc) pyrna_prop_array_ass_subscript, /* mp_ass_subscript */
2818 static PyMappingMethods pyrna_prop_collection_as_mapping = {
2819 (lenfunc) pyrna_prop_collection_length, /* mp_length */
2820 (binaryfunc) pyrna_prop_collection_subscript, /* mp_subscript */
2821 (objobjargproc) pyrna_prop_collection_ass_subscript, /* mp_ass_subscript */
2824 /* only for fast bool's, large structs, assign nb_bool on init */
2825 static PyNumberMethods pyrna_prop_array_as_number = {
2827 NULL, /* nb_subtract */
2828 NULL, /* nb_multiply */
2829 NULL, /* nb_remainder */
2830 NULL, /* nb_divmod */
2831 NULL, /* nb_power */
2832 NULL, /* nb_negative */
2833 NULL, /* nb_positive */
2834 NULL, /* nb_absolute */
2835 (inquiry) pyrna_prop_array_bool, /* nb_bool */
2837 static PyNumberMethods pyrna_prop_collection_as_number = {
2839 NULL, /* nb_subtract */
2840 NULL, /* nb_multiply */
2841 NULL, /* nb_remainder */
2842 NULL, /* nb_divmod */
2843 NULL, /* nb_power */
2844 NULL, /* nb_negative */
2845 NULL, /* nb_positive */
2846 NULL, /* nb_absolute */
2847 (inquiry) pyrna_prop_collection_bool, /* nb_bool */
2850 static int pyrna_prop_array_contains(BPy_PropertyRNA *self, PyObject *value)
2852 return pyrna_array_contains_py(&self->ptr, self->prop, value);
2855 static int pyrna_prop_collection_contains(BPy_PropertyRNA *self, PyObject *key)
2857 PointerRNA newptr; /* not used, just so RNA_property_collection_lookup_string runs */
2859 if (PyTuple_Check(key)) {
2860 /* special case, for ID datablocks we */
2861 return pyrna_prop_collection_subscript_str_lib_pair_ptr(self, key,
2862 "(id, lib) in bpy_prop_collection", false, NULL);
2866 /* key in dict style check */
2867 const char *keyname = _PyUnicode_AsString(key);
2869 if (keyname == NULL) {
2870 PyErr_SetString(PyExc_TypeError,
2871 "bpy_prop_collection.__contains__: expected a string or a tuple of strings");
2875 if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
2882 static int pyrna_struct_contains(BPy_StructRNA *self, PyObject *value)
2885 const char *name = _PyUnicode_AsString(value);
2887 PYRNA_STRUCT_CHECK_INT(self);
2890 PyErr_SetString(PyExc_TypeError, "bpy_struct.__contains__: expected a string");
2894 if (RNA_struct_idprops_check(self->ptr.type) == 0) {
2895 PyErr_SetString(PyExc_TypeError, "bpy_struct: this type doesn't support IDProperties");
2899 group = RNA_struct_idprops(&self->ptr, 0);
2904 return IDP_GetPropertyFromGroup(group, name) ? 1 : 0;
2907 static PySequenceMethods pyrna_prop_array_as_sequence = {
2908 (lenfunc)pyrna_prop_array_length, /* Cant set the len otherwise it can evaluate as false */
2909 NULL, /* sq_concat */
2910 NULL, /* sq_repeat */
2911 (ssizeargfunc)pyrna_prop_array_subscript_int, /* sq_item */ /* Only set this so PySequence_Check() returns True */
2912 NULL, /* sq_slice */
2913 (ssizeobjargproc)prop_subscript_ass_array_int, /* sq_ass_item */
2914 NULL, /* *was* sq_ass_slice */
2915 (objobjproc)pyrna_prop_array_contains, /* sq_contains */
2916 (binaryfunc) NULL, /* sq_inplace_concat */
2917 (ssizeargfunc) NULL, /* sq_inplace_repeat */
2920 static PySequenceMethods pyrna_prop_collection_as_sequence = {
2921 (lenfunc)pyrna_prop_collection_length, /* Cant set the len otherwise it can evaluate as false */
2922 NULL, /* sq_concat */
2923 NULL, /* sq_repeat */
2924 (ssizeargfunc)pyrna_prop_collection_subscript_int, /* sq_item */ /* Only set this so PySequence_Check() returns True */
2925 NULL, /* *was* sq_slice */
2926 (ssizeobjargproc)/* pyrna_prop_collection_ass_subscript_int */ NULL /* let mapping take this one */, /* sq_ass_item */
2927 NULL, /* *was* sq_ass_slice */
2928 (objobjproc)pyrna_prop_collection_contains, /* sq_contains */
2929 (binaryfunc) NULL, /* sq_inplace_concat */
2930 (ssizeargfunc) NULL, /* sq_inplace_repeat */
2933 static PySequenceMethods pyrna_struct_as_sequence = {
2934 NULL, /* Cant set the len otherwise it can evaluate as false */
2935 NULL, /* sq_concat */
2936 NULL, /* sq_repeat */
2937 NULL, /* sq_item */ /* Only set this so PySequence_Check() returns True */
2938 NULL, /* *was* sq_slice */
2939 NULL, /* sq_ass_item */
2940 NULL, /* *was* sq_ass_slice */
2941 (objobjproc)pyrna_struct_contains, /* sq_contains */
2942 (binaryfunc) NULL, /* sq_inplace_concat */
2943 (ssizeargfunc) NULL, /* sq_inplace_repeat */
2946 static PyObject *pyrna_struct_subscript(BPy_StructRNA *self, PyObject *key)
2948 /* mostly copied from BPy_IDGroup_Map_GetItem */
2949 IDProperty *group, *idprop;
2950 const char *name = _PyUnicode_AsString(key);
2952 PYRNA_STRUCT_CHECK_OBJ(self);
2954 if (RNA_struct_idprops_check(self->ptr.type) == 0) {
2955 PyErr_SetString(PyExc_TypeError, "this type doesn't support IDProperties");
2960 PyErr_SetString(PyExc_TypeError, "bpy_struct[key]: only strings are allowed as keys of ID properties");
2964 group = RNA_struct_idprops(&self->ptr, 0);