Blender V5.3
idprop_py_api.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include <Python.h>
12
13#include "python_compat.hh" /* IWYU pragma: keep. */
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_utildefines.hh"
18
19#include "idprop_py_api.hh"
20#include "idprop_py_ui_api.hh"
21
22#include "BKE_idprop.hh"
23
24#include "DNA_ID.h" /* ID property definitions. */
25
26#define USE_STRING_COERCE
27
28#ifdef USE_STRING_COERCE
29# include "py_capi_utils.hh"
30#endif
31
32#include "python_utildefines.hh"
33
34namespace blender {
35
36extern bool pyrna_id_FromPyObject(PyObject *obj, ID **id);
37extern PyObject *pyrna_id_CreatePyObject(ID *id);
38extern bool pyrna_id_CheckPyObject(PyObject *obj);
39
40/* Currently there is no need to expose this publicly. */
41static PyObject *BPy_IDGroup_IterKeys_CreatePyObject(BPy_IDProperty *group, const bool reversed);
42static PyObject *BPy_IDGroup_IterValues_CreatePyObject(BPy_IDProperty *group, const bool reversed);
43static PyObject *BPy_IDGroup_IterItems_CreatePyObject(BPy_IDProperty *group, const bool reversed);
44
45static PyObject *BPy_IDGroup_ViewKeys_CreatePyObject(BPy_IDProperty *group);
46static PyObject *BPy_IDGroup_ViewValues_CreatePyObject(BPy_IDProperty *group);
47static PyObject *BPy_IDGroup_ViewItems_CreatePyObject(BPy_IDProperty *group);
48
49static BPy_IDGroup_View *IDGroup_View_New_WithType(BPy_IDProperty *group, PyTypeObject *type);
50static int BPy_IDGroup_Contains(BPy_IDProperty *self, PyObject *value);
51
52/* -------------------------------------------------------------------- */
57
58static PyObject *idprop_py_from_idp_string(const IDProperty *prop)
59{
60 if (prop->subtype == IDP_STRING_SUB_BYTE) {
61 return PyBytes_FromStringAndSize(IDP_string_get(prop), prop->len);
62 }
63
64#ifdef USE_STRING_COERCE
65 return PyC_UnicodeFromBytesAndSize(IDP_string_get(prop), prop->len - 1);
66#else
67 return PyUnicode_FromStringAndSize(IDP_string_get(prop), prop->len - 1);
68#endif
69}
70
71static PyObject *idprop_py_from_idp_int(const IDProperty *prop)
72{
73 return PyLong_FromLong(long(IDP_int_get(prop)));
74}
75
76static PyObject *idprop_py_from_idp_float(const IDProperty *prop)
77{
78 return PyFloat_FromDouble(double(IDP_float_get(prop)));
79}
80
81static PyObject *idprop_py_from_idp_double(const IDProperty *prop)
82{
83 return PyFloat_FromDouble(IDP_double_get(prop));
84}
85
86static PyObject *idprop_py_from_idp_bool(const IDProperty *prop)
87{
88 return PyBool_FromLong(IDP_bool_get(prop));
89}
90
91static PyObject *idprop_py_from_idp_group(ID *id, IDProperty *prop, IDProperty *parent)
92{
93 BPy_IDProperty *group = PyObject_New(BPy_IDProperty, &BPy_IDGroup_Type);
94 group->owner_id = id;
95 group->prop = prop;
96 group->parent = parent; /* can be nullptr */
97 return reinterpret_cast<PyObject *>(group);
98}
99
100static PyObject *idprop_py_from_idp_id(IDProperty *prop)
101{
102 return pyrna_id_CreatePyObject(static_cast<ID *>(prop->data.pointer));
103}
104
105static PyObject *idprop_py_from_idp_array(ID *id, IDProperty *prop)
106{
108 array->owner_id = id;
109 array->prop = prop;
110 return reinterpret_cast<PyObject *>(array);
111}
112
113static PyObject *idprop_py_from_idp_idparray(ID *id, IDProperty *prop)
114{
115 PyObject *seq = PyList_New(prop->len);
117 int i;
118
119 if (!seq) {
120 PyErr_Format(
121 PyExc_RuntimeError, "%s: IDP_IDPARRAY: PyList_New(%d) failed", __func__, prop->len);
122 return nullptr;
123 }
124
125 for (i = 0; i < prop->len; i++) {
126 PyObject *wrap = BPy_IDGroup_WrapData(id, array++, prop);
127
128 /* BPy_IDGroup_MapDataToPy sets the error */
129 if (wrap == nullptr) [[unlikely]] {
130 Py_DECREF(seq);
131 return nullptr;
132 }
133
134 PyList_SET_ITEM(seq, i, wrap);
135 }
136
137 return seq;
138}
139
141
142/* -------------------------------------------------------------------- */
145
146/* use for both array and group */
147static Py_hash_t BPy_IDGroup_hash(BPy_IDProperty *self)
148{
149 return Py_HashPointer(self->prop);
150}
151
152static PyObject *BPy_IDGroup_repr(BPy_IDProperty *self)
153{
154 return PyUnicode_FromFormat("<bpy id prop: owner=\"%s\", name=\"%s\", address=%p>",
155 self->owner_id ? self->owner_id->name : "<NONE>",
156 self->prop->name,
157 self->prop);
158}
159
160PyObject *BPy_IDGroup_WrapData(ID *id, IDProperty *prop, IDProperty *parent)
161{
162 switch (prop->type) {
163 case IDP_STRING:
164 return idprop_py_from_idp_string(prop);
165 case IDP_INT:
166 return idprop_py_from_idp_int(prop);
167 case IDP_FLOAT:
168 return idprop_py_from_idp_float(prop);
169 case IDP_DOUBLE:
170 return idprop_py_from_idp_double(prop);
171 case IDP_BOOLEAN:
172 return idprop_py_from_idp_bool(prop);
173 case IDP_GROUP:
174 return idprop_py_from_idp_group(id, prop, parent);
175 case IDP_ARRAY:
176 return idprop_py_from_idp_array(id, prop);
177 case IDP_IDPARRAY:
178 return idprop_py_from_idp_idparray(id, prop); /* this could be better a internal type */
179 case IDP_ID:
180 return idprop_py_from_idp_id(prop);
181 default:
182 Py_RETURN_NONE;
183 }
184}
185
186/* UNUSED, currently assignment overwrites into new properties, rather than setting in-place. */
187#if 0
188static int BPy_IDGroup_SetData(BPy_IDProperty *self, IDProperty *prop, PyObject *value)
189{
190 switch (prop->type) {
191 case IDP_STRING: {
192 char *st;
193 if (!PyUnicode_Check(value)) {
194 PyErr_SetString(PyExc_TypeError, "expected a string!");
195 return -1;
196 }
197/* NOTE: if this code is enabled, bytes support needs to be added */
198# ifdef USE_STRING_COERCE
199 {
200 int alloc_len;
201 PyObject *value_coerce = nullptr;
202
203 st = (char *)PyC_UnicodeAsBytes(value, &value_coerce);
204 alloc_len = strlen(st) + 1;
205
206 st = PyUnicode_AsUTF8(value);
207 IDP_ResizeArray(prop, alloc_len);
208 memcpy(IDP_string_get(prop), st, alloc_len);
209 Py_XDECREF(value_coerce);
210 }
211# else
212 length_ssize_t st_len;
213 st = PyUnicode_AsUTF8AndSize(value, &st_len);
214 IDP_ResizeArray(prop, st_len + 1);
215 memcpy(IDP_string_get(prop), st, st_len + 1);
216# endif
217
218 return 0;
219 }
220
221 case IDP_INT: {
222 int ivalue = PyLong_AsSsize_t(value);
223 if (ivalue == -1 && PyErr_Occurred()) {
224 PyErr_SetString(PyExc_TypeError, "expected an int type");
225 return -1;
226 }
227 IDP_int_set(prop, ivalue);
228 break;
229 }
230 case IDP_FLOAT: {
231 float fvalue = float(PyFloat_AsDouble(value));
232 if (fvalue == -1 && PyErr_Occurred()) {
233 PyErr_SetString(PyExc_TypeError, "expected a float");
234 return -1;
235 }
236 IDP_float_set(self->prop, fvalue);
237 break;
238 }
239 case IDP_DOUBLE: {
240 double dvalue = PyFloat_AsDouble(value);
241 if (dvalue == -1 && PyErr_Occurred()) {
242 PyErr_SetString(PyExc_TypeError, "expected a float");
243 return -1;
244 }
245 IDP_double_set(self->prop, value);
246 break;
247 }
248 default:
249 PyErr_SetString(PyExc_AttributeError, "attempt to set read-only attribute!");
250 return -1;
251 }
252 return 0;
253}
254#endif
255
257 /* Wrap. */
258 BPy_IDGroup_GetName_doc,
259 "The name of this Group.\n"
260 "\n"
261 ":type: str\n");
262static PyObject *BPy_IDGroup_GetName(BPy_IDProperty *self, void * /*closure*/)
263{
264 return PyUnicode_FromString(self->prop->name);
265}
266
267static int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void * /*closure*/)
268{
269 const char *name;
270 Py_ssize_t name_len;
271
272 if (!PyUnicode_Check(value)) {
273 PyErr_SetString(PyExc_TypeError, "expected a string!");
274 return -1;
275 }
276
277 name = PyUnicode_AsUTF8AndSize(value, &name_len);
278
279 if (!(name_len < MAX_IDPROP_NAME)) {
280 PyErr_SetString(PyExc_TypeError, "string length cannot exceed 63 characters!");
281 return -1;
282 }
283 if (STREQ(name, self->prop->name)) {
284 return 0;
285 }
286 if (IDProperty *parent = self->parent) {
287 if (IDP_GetPropertyFromGroup(parent, name)) {
288 PyErr_SetString(PyExc_NameError, "property name already exists in parent group");
289 return -1;
290 }
291 }
292
293 memcpy(self->prop->name, name, name_len + 1);
294 return 0;
295}
296
297#if 0
298static PyObject *BPy_IDGroup_GetType(BPy_IDProperty *self)
299{
300 return PyLong_FromLong(self->prop->type);
301}
302#endif
303
304static PyGetSetDef BPy_IDGroup_getseters[] = {
305 {"name",
306 reinterpret_cast<getter>(BPy_IDGroup_GetName),
307 reinterpret_cast<setter>(BPy_IDGroup_SetName),
308 BPy_IDGroup_GetName_doc,
309 nullptr},
310 {nullptr, nullptr, nullptr, nullptr, nullptr},
311};
312
313static Py_ssize_t BPy_IDGroup_Map_Len(BPy_IDProperty *self)
314{
315 if (self->prop->type != IDP_GROUP) {
316 PyErr_SetString(PyExc_TypeError, "len() of unsized object");
317 return -1;
318 }
319
320 return self->prop->len;
321}
322
323static PyObject *BPy_IDGroup_Map_GetItem(BPy_IDProperty *self, PyObject *item)
324{
326 const char *name;
327
328 if (self->prop->type != IDP_GROUP) {
329 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
330 return nullptr;
331 }
332
333 name = PyUnicode_AsUTF8(item);
334
335 if (name == nullptr) {
336 PyErr_SetString(PyExc_TypeError, "only strings are allowed as keys of ID properties");
337 return nullptr;
338 }
339
340 idprop = IDP_GetPropertyFromGroup(self->prop, name);
341
342 if (idprop == nullptr) {
343 PyErr_SetString(PyExc_KeyError, "key not in subgroup dict");
344 return nullptr;
345 }
346
347 return BPy_IDGroup_WrapData(self->owner_id, idprop, self->prop);
348}
349
350/* Return identified matching IDProperty type, or -1 if error (e.g. mixed and/or incompatible
351 * types, etc.). */
352static char idp_sequence_type(PyObject *seq_fast)
353{
354 PyObject **seq_fast_items = PySequence_Fast_ITEMS(seq_fast);
355 PyObject *item;
356 char type = IDP_INT;
357
358 Py_ssize_t i, len = PySequence_Fast_GET_SIZE(seq_fast);
359
360 for (i = 0; i < len; i++) {
361 item = seq_fast_items[i];
362 if (PyFloat_Check(item)) {
363 /* Mixed float and any other type but int.
364 * NOTE: Mixed float/int is allowed, and considered as float values. */
365 if (!ELEM(type, IDP_INT, IDP_DOUBLE)) {
366 return -1;
367 }
368 type = IDP_DOUBLE;
369 }
370 else if (PyBool_Check(item)) {
371 /* Mixed boolean and any other type. */
372 if (i != 0 && (type != IDP_BOOLEAN)) {
373 return -1;
374 }
375 type = IDP_BOOLEAN;
376 }
377 else if (PyLong_Check(item)) {
378 /* Mixed int and any other type but float.
379 * NOTE: Mixed float/int is allowed, and considered as float values. */
380 if (!ELEM(type, IDP_INT, IDP_DOUBLE)) {
381 return -1;
382 }
383 }
384 else if (PyMapping_Check(item)) {
385 /* Mixed dict and any other type. */
386 if (i != 0 && (type != IDP_IDPARRAY)) {
387 return -1;
388 }
389 type = IDP_IDPARRAY;
390 }
391 else {
392 return -1;
393 }
394 }
395
396 return type;
397}
398
399static const char *idp_try_read_name(PyObject *name_obj)
400{
401 const char *name = nullptr;
402 if (name_obj) {
403 Py_ssize_t name_len;
404 name = PyUnicode_AsUTF8AndSize(name_obj, &name_len);
405
406 if (name == nullptr) {
407 PyErr_Format(PyExc_KeyError,
408 "invalid id-property key, expected a string, not a %.200s",
409 Py_TYPE(name_obj)->tp_name);
410 return nullptr;
411 }
412
413 if (!(name_len < MAX_IDPROP_NAME)) {
414 PyErr_SetString(PyExc_KeyError,
415 "the length of IDProperty names is limited to 63 characters");
416 return nullptr;
417 }
418 }
419 else {
420 name = "";
421 }
422 return name;
423}
424
426
427/* -------------------------------------------------------------------- */
430
449
451 const char *name,
452 PyObject *ob,
453 const bool do_conversion,
454 const bool can_create)
455{
456 IDProperty *prop = nullptr;
457 const double value = PyFloat_AsDouble(ob);
458 if (prop_exist) {
459 if (prop_exist->type == IDP_DOUBLE) {
460 IDP_double_set(prop_exist, value);
461 prop = prop_exist;
462 }
463 else if (do_conversion) {
464 switch (prop_exist->type) {
465 case IDP_FLOAT:
466 IDP_float_set(prop_exist, float(value));
467 prop = prop_exist;
468 break;
469 case IDP_STRING:
470 case IDP_INT:
471 case IDP_ARRAY:
472 case IDP_GROUP:
473 case IDP_ID:
474 case IDP_DOUBLE:
475 case IDP_IDPARRAY:
476 case IDP_BOOLEAN:
477 break;
478 }
479 }
480 }
481 if (!prop && can_create) {
482 prop = bke::idprop::create(name, value).release();
483 }
484 return prop;
485}
486
488 const char *name,
489 PyObject *ob,
490 const bool do_conversion,
491 const bool can_create)
492{
493 IDProperty *prop = nullptr;
494 const bool value = PyC_Long_AsBool(ob);
495 if (prop_exist) {
496 if (prop_exist->type == IDP_BOOLEAN) {
497 IDP_bool_set(prop_exist, value);
498 prop = prop_exist;
499 }
500 else if (do_conversion) {
501 switch (prop_exist->type) {
502 case IDP_INT:
503 IDP_int_set(prop_exist, int(value));
504 prop = prop_exist;
505 break;
506 case IDP_STRING:
507 case IDP_FLOAT:
508 case IDP_ARRAY:
509 case IDP_GROUP:
510 case IDP_ID:
511 case IDP_DOUBLE:
512 case IDP_IDPARRAY:
513 case IDP_BOOLEAN:
514 break;
515 }
516 }
517 }
518 if (!prop && can_create) {
519 prop = bke::idprop::create_bool(name, value).release();
520 }
521 return prop;
522}
523
525 const char *name,
526 PyObject *ob,
527 const bool do_conversion,
528 const bool can_create)
529{
530 IDProperty *prop = nullptr;
531 if (prop_exist) {
532 if (prop_exist->type == IDP_INT) {
533 const int value = PyC_Long_AsI32(ob);
534 if (value == -1 && PyErr_Occurred()) {
535 return prop;
536 }
537 IDP_int_set(prop_exist, value);
538 prop = prop_exist;
539 }
540 else if (do_conversion) {
541 const int64_t value = PyC_Long_AsI64(ob);
542 if (value == -1 && PyErr_Occurred()) {
543 return prop;
544 }
545 switch (prop_exist->type) {
546 case IDP_FLOAT:
547 IDP_float_set(prop_exist, float(value));
548 prop = prop_exist;
549 break;
550 case IDP_DOUBLE:
551 IDP_double_set(prop_exist, double(value));
552 prop = prop_exist;
553 break;
554 case IDP_STRING:
555 case IDP_INT:
556 case IDP_ARRAY:
557 case IDP_GROUP:
558 case IDP_ID:
559 case IDP_IDPARRAY:
560 case IDP_BOOLEAN:
561 break;
562 }
563 }
564 }
565 if (!prop && can_create) {
566 const int value = PyC_Long_AsI32(ob);
567 if (value == -1 && PyErr_Occurred()) {
568 return prop;
569 }
570 prop = bke::idprop::create(name, value).release();
571 }
572 return prop;
573}
574
576 const char *name,
577 PyObject *ob,
578 const bool /*do_conversion*/,
579 const bool can_create)
580{
581 IDProperty *prop = nullptr;
582 Py_ssize_t value_len = 0;
583 const char *value = nullptr;
584
585#ifdef USE_STRING_COERCE
586 PyObject *value_coerce = nullptr;
587 value = PyC_UnicodeAsBytesAndSize(ob, &value_len, &value_coerce);
588#else
589 value = PyUnicode_AsUTF8AndSize(ob, &value_len);
590#endif
591
592 if (prop_exist) {
593 if (prop_exist->type == IDP_STRING && prop_exist->subtype == IDP_STRING_SUB_UTF8) {
594 IDP_AssignStringMaxSize(prop_exist, value, int(value_len) + 1);
595 prop = prop_exist;
596 }
597 /* No conversion. */
598 }
599
600 if (!prop && can_create) {
601 IDPropertyTemplate val = {0};
602 val.string.str = value;
603 val.string.len = int(value_len) + 1;
604 val.string.subtype = IDP_STRING_SUB_UTF8;
605 prop = IDP_New(IDP_STRING, &val, name);
606 }
607
608#ifdef USE_STRING_COERCE
609 Py_XDECREF(value_coerce);
610#endif
611
612 return prop;
613}
614
616 const char *name,
617 PyObject *ob,
618 const bool /*do_conversion*/,
619 const bool can_create)
620{
621 IDProperty *prop = nullptr;
622 Py_ssize_t value_len = PyBytes_GET_SIZE(ob);
623 const char *value = PyBytes_AS_STRING(ob);
624
625 if (prop_exist) {
626 if (prop_exist->type == IDP_STRING && prop_exist->subtype == IDP_STRING_SUB_BYTE) {
627 IDP_AssignStringMaxSize(prop_exist, value, int(value_len) + 1);
628 prop = prop_exist;
629 }
630 /* No conversion. */
631 }
632
633 if (!prop && can_create) {
634 IDPropertyTemplate val = {0};
635 val.string.str = value;
636 val.string.len = int(value_len);
637 val.string.subtype = IDP_STRING_SUB_BYTE;
638 prop = IDP_New(IDP_STRING, &val, name);
639 }
640
641 return prop;
642}
643
644static int idp_array_type_from_formatstr_and_size(const char *typestr, Py_ssize_t itemsize)
645{
646 const char format = PyC_StructFmt_type_from_str(typestr);
647
649 if (itemsize == 4) {
650 return IDP_FLOAT;
651 }
652 if (itemsize == 8) {
653 return IDP_DOUBLE;
654 }
655 }
656 if (PyC_StructFmt_type_is_int_any(format)) {
657 if (itemsize == 4) {
658 return IDP_INT;
659 }
660 /* TODO: Support Booleans? */
661 }
662
663 return -1;
664}
665
666static const char *idp_format_from_array_type(int type)
667{
668 if (type == IDP_INT) {
669 return "i";
670 }
671 if (type == IDP_FLOAT) {
672 return "f";
673 }
674 if (type == IDP_DOUBLE) {
675 return "d";
676 }
677 if (type == IDP_BOOLEAN) {
678 return "b";
679 }
680 return nullptr;
681}
682
684 const char *name,
685 const Py_buffer &buffer,
686 const int idp_type,
687 const bool /*do_conversion*/,
688 const bool can_create)
689{
690 BLI_assert(idp_type != -1);
691 IDProperty *prop = nullptr;
692
693 if (prop_exist) {
694 if (prop_exist->type == IDP_ARRAY && prop_exist->subtype == idp_type) {
695 BLI_assert(buffer.len == prop_exist->len);
696 memcpy(IDP_array_voidp_get(prop_exist), buffer.buf, buffer.len);
697 prop = prop_exist;
698 }
699 /* No conversion. */
700 }
701 if (!prop && can_create) {
702 IDPropertyTemplate val = {0};
703 val.array.type = idp_type;
704 val.array.len = buffer.len / buffer.itemsize;
705 prop = IDP_New(IDP_ARRAY, &val, name);
706 memcpy(IDP_array_voidp_get(prop), buffer.buf, buffer.len);
707 }
708 return prop;
709}
710
712 const char *name,
713 PyObject *ob,
714 const bool do_conversion,
715 const bool can_create)
716{
717 IDProperty *prop = nullptr;
718 IDPropertyTemplate val = {0};
719
720 PyObject **ob_seq_fast_items;
721 PyObject *item;
722 int i;
723
724 ob_seq_fast_items = PySequence_Fast_ITEMS(ob);
725
726 /* IDProperties do not support mixed type of data in an array. Try to extract a single type from
727 * the whole sequence, or error. */
728 val.array.type = idp_sequence_type(ob);
729 if (val.array.type == char(-1)) {
730 PyErr_SetString(PyExc_TypeError,
731 "only floats, ints, booleans and dicts are allowed in ID property arrays");
732 return nullptr;
733 }
735 /* Should never happen. */
736 PyErr_SetString(PyExc_RuntimeError, "internal error with idp array.type");
738 return nullptr;
739 }
740
741 val.array.len = PySequence_Fast_GET_SIZE(ob);
742
743 /* NOTE: For now do not consider resizing existing array property. Also do not handle IDPARRAY.
744 * - 'static type' also means 'fixed length' (e.g. vectors or matrices cases).
745 * - For 'dynamic type' case, it's not really a problem if array properties get replaced
746 * currently.
747 */
748 if (prop_exist && prop_exist->len == val.array.len) {
749 switch (val.array.type) {
750 case IDP_DOUBLE: {
751 const bool to_float = (prop_exist->subtype == IDP_FLOAT);
752 if (!(prop_exist->type == IDP_ARRAY &&
753 (prop_exist->subtype == IDP_DOUBLE || (do_conversion && to_float))))
754 {
755 break;
756 }
757 prop = prop_exist;
758 void *prop_data = IDP_array_voidp_get(prop);
759 for (i = 0; i < val.array.len; i++) {
760 item = ob_seq_fast_items[i];
761 const double value = PyFloat_AsDouble(item);
762 if ((value == -1.0) && PyErr_Occurred()) {
763 continue;
764 }
765 if (to_float) {
766 static_cast<float *>(prop_data)[i] = float(value);
767 }
768 else {
769 static_cast<double *>(prop_data)[i] = value;
770 }
771 }
772 break;
773 }
774 case IDP_INT: {
775 const bool to_float = (prop_exist->subtype == IDP_FLOAT);
776 const bool to_double = (prop_exist->subtype == IDP_DOUBLE);
777 if (!(prop_exist->type == IDP_ARRAY &&
778 (prop_exist->subtype == IDP_INT || (do_conversion && (to_float || to_double)))))
779 {
780 break;
781 }
782 prop = prop_exist;
783 void *prop_data = IDP_array_voidp_get(prop);
784 for (i = 0; i < val.array.len; i++) {
785 item = ob_seq_fast_items[i];
786 if (to_float || to_double) {
787 const int64_t value = PyC_Long_AsI64(item);
788 if ((value == -1) && PyErr_Occurred()) {
789 continue;
790 }
791 if (to_float) {
792 static_cast<float *>(prop_data)[i] = float(value);
793 }
794 else { /* if (to_double) */
795 static_cast<double *>(prop_data)[i] = double(value);
796 }
797 }
798 else {
799 const int value = PyC_Long_AsI32(item);
800 if ((value == -1) && PyErr_Occurred()) {
801 continue;
802 }
803 static_cast<int *>(prop_data)[i] = value;
804 }
805 }
806 break;
807 }
808 case IDP_BOOLEAN: {
809 const bool to_int = (prop_exist->subtype == IDP_INT);
810 if (!(prop_exist->type == IDP_ARRAY &&
811 (prop_exist->subtype == IDP_BOOLEAN || (do_conversion && to_int))))
812 {
813 break;
814 }
815 prop = prop_exist;
816 void *prop_data = IDP_array_voidp_get(prop);
817 for (i = 0; i < val.array.len; i++) {
818 item = ob_seq_fast_items[i];
819 const int value = PyC_Long_AsBool(item);
820 if ((value == -1) && PyErr_Occurred()) {
821 continue;
822 }
823 if (to_int) {
824 static_cast<int *>(prop_data)[i] = value;
825 }
826 else {
827 static_cast<bool *>(prop_data)[i] = bool(value);
828 }
829 }
830 break;
831 }
832 case IDP_IDPARRAY: {
833 /* TODO? */
834 break;
835 }
836 }
837 }
838
839 if (prop || !can_create) {
840 return prop;
841 }
842
843 switch (val.array.type) {
844 case IDP_DOUBLE: {
845 double *prop_data;
846 prop = IDP_New(IDP_ARRAY, &val, name);
847 prop_data = IDP_array_double_get(prop);
848 for (i = 0; i < val.array.len; i++) {
849 item = ob_seq_fast_items[i];
850 if (((prop_data[i] = PyFloat_AsDouble(item)) == -1.0) && PyErr_Occurred()) {
851 IDP_FreeProperty(prop);
852 return nullptr;
853 }
854 }
855 break;
856 }
857 case IDP_INT: {
858 int *prop_data;
859 prop = IDP_New(IDP_ARRAY, &val, name);
860 prop_data = IDP_array_int_get(prop);
861 for (i = 0; i < val.array.len; i++) {
862 item = ob_seq_fast_items[i];
863 if (((prop_data[i] = PyC_Long_AsI32(item)) == -1) && PyErr_Occurred()) {
864 IDP_FreeProperty(prop);
865 return nullptr;
866 }
867 }
868 break;
869 }
870 case IDP_IDPARRAY: {
871 prop = IDP_NewIDPArray(name);
872 for (i = 0; i < val.array.len; i++) {
873 item = ob_seq_fast_items[i];
874 if (BPy_IDProperty_Map_ValidateAndCreate(nullptr, prop, item) == false) {
875 IDP_FreeProperty(prop);
876 return nullptr;
877 }
878 }
879 break;
880 }
881 case IDP_BOOLEAN: {
882 prop = IDP_New(IDP_ARRAY, &val, name);
883 int8_t *prop_data = IDP_array_bool_get(prop);
884 for (i = 0; i < val.array.len; i++) {
885 item = ob_seq_fast_items[i];
886 const int value = PyC_Long_AsBool(item);
887 if ((value == -1) && PyErr_Occurred()) {
888 IDP_FreeProperty(prop);
889 return nullptr;
890 }
891 prop_data[i] = (value != 0);
892 }
893 break;
894 }
895 }
896 return prop;
897}
898
900 const char *name,
901 PyObject *ob,
902 const bool do_conversion,
903 const bool can_create)
904{
905 Py_buffer buffer;
906 bool use_buffer = false;
907 int idp_buffer_type = -1;
908
909 if (PyObject_CheckBuffer(ob)) {
910 if (PyObject_GetBuffer(ob, &buffer, PyBUF_ND | PyBUF_FORMAT) == -1) {
911 /* Request failed. A `PyExc_BufferError` will have been raised,
912 * so clear it to silently fall back to accessing as a sequence. */
913 PyErr_Clear();
914 }
915 else {
916 idp_buffer_type = idp_array_type_from_formatstr_and_size(buffer.format, buffer.itemsize);
917 if (idp_buffer_type != -1) {
918 /* If creating a new IDProp is not allowed, and the existing one is not usable (same size
919 * and type), then the 'buffer assignment' process cannot be used. */
920 if (!can_create && (!prop_exist || (prop_exist->type != idp_buffer_type) ||
921 (prop_exist->len != buffer.len)))
922 {
923 PyBuffer_Release(&buffer);
924 }
925 else {
926 use_buffer = true;
927 }
928 }
929 else {
930 PyBuffer_Release(&buffer);
931 }
932 }
933 }
934
935 if (use_buffer) {
937 prop_exist, name, buffer, idp_buffer_type, do_conversion, can_create);
938 PyBuffer_Release(&buffer);
939 return prop;
940 }
941
942 PyObject *ob_seq_fast = PySequence_Fast(ob, "py -> idprop");
943 if (ob_seq_fast != nullptr) {
945 prop_exist, name, ob_seq_fast, do_conversion, can_create);
946 Py_DECREF(ob_seq_fast);
947 return prop;
948 }
949
950 return nullptr;
951}
952
953static IDProperty *idp_from_PyMapping(IDProperty * /*prop_exist*/,
954 const char *name,
955 PyObject *ob,
956 const bool /*do_conversion*/,
957 const bool /*can_create*/)
958{
959 IDProperty *prop;
960
961 /* TODO: Handle editing in-place of existing property (#IDP_FLAG_STATIC_TYPE flag). */
962
963 PyObject *keys, *vals, *key, *pval;
964 int i, len;
965 /* yay! we get into recursive stuff now! */
966 len = PyMapping_Length(ob);
967 if (len == -1) {
968 return nullptr;
969 }
970 keys = PyMapping_Keys(ob);
971 if (keys == nullptr) {
972 return nullptr;
973 }
974 vals = PyMapping_Values(ob);
975 if (vals == nullptr) {
976 Py_DECREF(keys);
977 return nullptr;
978 }
979
980 /* We allocate the group first; if we hit any invalid data,
981 * we can delete it easily enough. */
982 prop = bke::idprop::create_group(name).release();
983 for (i = 0; i < len; i++) {
984 key = PySequence_GetItem(keys, i);
985 pval = PySequence_GetItem(vals, i);
986 if (BPy_IDProperty_Map_ValidateAndCreate(key, prop, pval) == false) {
987 IDP_FreeProperty(prop);
988 Py_XDECREF(keys);
989 Py_XDECREF(vals);
990 Py_XDECREF(key);
991 Py_XDECREF(pval);
992 /* error is already set */
993 return nullptr;
994 }
995 Py_XDECREF(key);
996 Py_XDECREF(pval);
997 }
998 Py_XDECREF(keys);
999 Py_XDECREF(vals);
1000 return prop;
1001}
1002
1004 const char *name,
1005 PyObject *ob,
1006 const bool /*do_conversion*/,
1007 const bool can_create)
1008{
1009 IDProperty *prop = nullptr;
1010 ID *value = nullptr;
1011 pyrna_id_FromPyObject(ob, &value);
1012
1013 if (value && (value->flag & ID_FLAG_EMBEDDED_DATA) != 0) {
1014 PyErr_SetString(PyExc_ValueError, "Cannot assign an embedded ID pointer to an id-property");
1015 return nullptr;
1016 }
1017
1018 if (prop_exist) {
1019 if (prop_exist->type == IDP_ID) {
1020 IDP_AssignID(prop_exist, value, 0);
1021 prop = prop_exist;
1022 }
1023 /* No conversion. */
1024 }
1025 if (!prop && can_create) {
1026 prop = bke::idprop::create(name, value).release();
1027 }
1028 return prop;
1029}
1030
1032 const char *name,
1033 PyObject *ob,
1034 const bool do_conversion,
1035 const bool can_create)
1036{
1037 if (name == nullptr) {
1038 return nullptr;
1039 }
1040
1041 if (PyFloat_Check(ob)) {
1042 return idp_from_PyFloat(prop_exist, name, ob, do_conversion, can_create);
1043 }
1044 if (PyBool_Check(ob)) {
1045 return idp_from_PyBool(prop_exist, name, ob, do_conversion, can_create);
1046 }
1047 if (PyLong_Check(ob)) {
1048 return idp_from_PyLong(prop_exist, name, ob, do_conversion, can_create);
1049 }
1050 if (PyUnicode_Check(ob)) {
1051 return idp_from_PyUnicode(prop_exist, name, ob, do_conversion, can_create);
1052 }
1053 if (PyBytes_Check(ob)) {
1054 return idp_from_PyBytes(prop_exist, name, ob, do_conversion, can_create);
1055 }
1056 if (PySequence_Check(ob)) {
1057 return idp_from_PySequence(prop_exist, name, ob, do_conversion, can_create);
1058 }
1059 if (ob == Py_None || pyrna_id_CheckPyObject(ob)) {
1060 return idp_from_DatablockPointer(prop_exist, name, ob, do_conversion, can_create);
1061 }
1062 if (PyMapping_Check(ob)) {
1063 return idp_from_PyMapping(prop_exist, name, ob, do_conversion, can_create);
1064 }
1065
1066 PyErr_Format(
1067 PyExc_TypeError, "invalid id-property type %.200s not supported", Py_TYPE(ob)->tp_name);
1068 return nullptr;
1069}
1070
1072
1073/* -------------------------------------------------------------------- */
1076
1077bool BPy_IDProperty_Map_ValidateAndCreate(PyObject *key, IDProperty *group, PyObject *ob)
1078{
1079 const char *name = idp_try_read_name(key);
1080 if (!name) {
1081 return false;
1082 }
1083
1084 /* If the container is an array of IDProperties, always add a new property to it. */
1085 if (group->type == IDP_IDPARRAY) {
1086 IDProperty *new_prop = BPy_IDProperty_FromPyObject(nullptr, name, ob, false, true);
1087 if (new_prop == nullptr) {
1088 return false;
1089 }
1090
1091 IDP_AppendArray(group, new_prop);
1092 /* IDP_AppendArray does a shallow copy (memcpy), only free memory */
1093 MEM_delete(new_prop);
1094
1095 return true;
1096 }
1097
1098 IDProperty *prop_exist = IDP_GetPropertyFromGroup(group, name);
1099
1100 /* If existing property is flagged to be statically typed, do not re-type it. Assign the value if
1101 * possible (potentially converting it), or fail. See #122743. */
1102 if (prop_exist && (prop_exist->flag & IDP_FLAG_STATIC_TYPE) != 0) {
1103 IDProperty *prop = BPy_IDProperty_FromPyObject(prop_exist, name, ob, true, false);
1104 BLI_assert(ELEM(prop, prop_exist, nullptr));
1105 if (prop != prop_exist) {
1106 PyErr_Format(PyExc_TypeError,
1107 "Cannot assign a '%.200s' value to the existing '%s' %s IDProperty",
1108 Py_TYPE(ob)->tp_name,
1109 name,
1110 IDP_type_str(prop_exist));
1111 return false;
1112 }
1113 return true;
1114 }
1115
1116 /* Attempt to assign new value in existing IDProperty, if types (and potentially subtypes) match
1117 * exactly. Otherwise, create a new IDProperty. */
1118 IDProperty *new_prop = BPy_IDProperty_FromPyObject(prop_exist, name, ob, false, true);
1119 if (new_prop == nullptr) {
1120 return false;
1121 }
1122 if (new_prop == prop_exist) {
1123 return true;
1124 }
1125
1126 /* Property was created with no existing counterpart, just insert it in the group container. */
1127 if (!prop_exist) {
1128 IDP_ReplaceInGroup_ex(group, new_prop, nullptr, 0);
1129 return true;
1130 }
1131
1132 /* Try to preserve UI data from the existing, replaced property. See: #37073. */
1133 if (prop_exist->ui_data) {
1134 /* Take ownership of the existing property's UI data. */
1135 const eIDPropertyUIDataType src_type = IDP_ui_data_type(prop_exist);
1136 IDPropertyUIData *ui_data = prop_exist->ui_data;
1137 prop_exist->ui_data = nullptr;
1138
1139 new_prop->ui_data = IDP_TryConvertUIData(ui_data, src_type, IDP_ui_data_type(new_prop));
1140 }
1141 /* Copy over the 'overridable' flag from existing property. */
1142 new_prop->flag |= (prop_exist->flag & IDP_FLAG_OVERRIDABLE_LIBRARY);
1143
1144 IDP_ReplaceInGroup_ex(group, new_prop, prop_exist, 0);
1145 return true;
1146}
1147
1148int BPy_Wrap_SetMapItem(IDProperty *prop, PyObject *key, PyObject *val)
1149{
1150 if (prop->type != IDP_GROUP) {
1151 PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
1152 return -1;
1153 }
1154
1155 if (val == nullptr) { /* del idprop[key] */
1156 IDProperty *pkey;
1157 const char *name = PyUnicode_AsUTF8(key);
1158
1159 if (name == nullptr) {
1160 PyErr_Format(PyExc_KeyError, "expected a string, not %.200s", Py_TYPE(key)->tp_name);
1161 return -1;
1162 }
1163
1164 pkey = IDP_GetPropertyFromGroup(prop, name);
1165 if (pkey) {
1166 IDP_FreeFromGroup(prop, pkey);
1167 return 0;
1168 }
1169
1170 PyErr_SetString(PyExc_KeyError, "property not found in group");
1171 return -1;
1172 }
1173
1174 bool ok;
1175
1176 ok = BPy_IDProperty_Map_ValidateAndCreate(key, prop, val);
1177 if (ok == false) {
1178 return -1;
1179 }
1180
1181 return 0;
1182}
1183
1184static int BPy_IDGroup_Map_SetItem(BPy_IDProperty *self, PyObject *key, PyObject *val)
1185{
1186 return BPy_Wrap_SetMapItem(self->prop, key, val);
1187}
1188
1189static PyObject *BPy_IDGroup_iter(BPy_IDProperty *self)
1190{
1191 PyObject *iterable = BPy_IDGroup_ViewKeys_CreatePyObject(self);
1192 PyObject *ret;
1193 if (iterable) {
1194 ret = PyObject_GetIter(iterable);
1195 Py_DECREF(iterable);
1196 }
1197 else {
1198 ret = nullptr;
1199 }
1200 return ret;
1201}
1202
1204{
1205 switch (prop->type) {
1206 case IDP_STRING:
1207 return idprop_py_from_idp_string(prop);
1208 case IDP_INT:
1209 return idprop_py_from_idp_int(prop);
1210 case IDP_FLOAT:
1211 return idprop_py_from_idp_float(prop);
1212 case IDP_DOUBLE:
1213 return idprop_py_from_idp_double(prop);
1214 case IDP_BOOLEAN:
1215 return idprop_py_from_idp_bool(prop);
1216 case IDP_ID:
1217 return idprop_py_from_idp_id(prop);
1218 case IDP_ARRAY: {
1219 PyObject *seq = PyList_New(prop->len);
1220 int i;
1221
1222 if (!seq) {
1223 PyErr_Format(
1224 PyExc_RuntimeError, "%s: IDP_ARRAY: PyList_New(%d) failed", __func__, prop->len);
1225 return nullptr;
1226 }
1227
1228 switch (prop->subtype) {
1229 case IDP_FLOAT: {
1230 const float *array = IDP_array_float_get(prop);
1231 for (i = 0; i < prop->len; i++) {
1232 PyList_SET_ITEM(seq, i, PyFloat_FromDouble(array[i]));
1233 }
1234 break;
1235 }
1236 case IDP_DOUBLE: {
1237 const double *array = IDP_array_double_get(prop);
1238 for (i = 0; i < prop->len; i++) {
1239 PyList_SET_ITEM(seq, i, PyFloat_FromDouble(array[i]));
1240 }
1241 break;
1242 }
1243 case IDP_INT: {
1244 const int *array = IDP_array_int_get(prop);
1245 for (i = 0; i < prop->len; i++) {
1246 PyList_SET_ITEM(seq, i, PyLong_FromLong(array[i]));
1247 }
1248 break;
1249 }
1250 case IDP_BOOLEAN: {
1251 const int8_t *array = IDP_array_bool_get(prop);
1252 for (i = 0; i < prop->len; i++) {
1253 PyList_SET_ITEM(seq, i, PyBool_FromLong(array[i]));
1254 }
1255 break;
1256 }
1257 default:
1258 PyErr_Format(
1259 PyExc_RuntimeError, "%s: invalid/corrupt array type '%d'!", __func__, prop->subtype);
1260 Py_DECREF(seq);
1261 return nullptr;
1262 }
1263
1264 return seq;
1265 }
1266 case IDP_IDPARRAY: {
1267 PyObject *seq = PyList_New(prop->len);
1269 int i;
1270
1271 if (!seq) {
1272 PyErr_Format(
1273 PyExc_RuntimeError, "%s: IDP_IDPARRAY: PyList_New(%d) failed", __func__, prop->len);
1274 return nullptr;
1275 }
1276
1277 for (i = 0; i < prop->len; i++) {
1278 PyObject *wrap = BPy_IDGroup_MapDataToPy(array++);
1279
1280 /* BPy_IDGroup_MapDataToPy sets the error */
1281 if (wrap == nullptr) [[unlikely]] {
1282 Py_DECREF(seq);
1283 return nullptr;
1284 }
1285
1286 PyList_SET_ITEM(seq, i, wrap);
1287 }
1288 return seq;
1289 }
1290 case IDP_GROUP: {
1291 PyObject *dict = _PyDict_NewPresized(prop->len);
1292 IDProperty *loop;
1293
1294 for (loop = prop->data.group.first(); loop; loop = loop->next) {
1295 PyObject *wrap = BPy_IDGroup_MapDataToPy(loop);
1296
1297 /* BPy_IDGroup_MapDataToPy sets the error */
1298 if (wrap == nullptr) [[unlikely]] {
1299 Py_DECREF(dict);
1300 return nullptr;
1301 }
1302
1303 PyDict_SetItemString(dict, loop->name, wrap);
1304 Py_DECREF(wrap);
1305 }
1306 return dict;
1307 }
1308 }
1309
1310 PyErr_Format(PyExc_RuntimeError,
1311 "%s ERROR: '%s' property exists with a bad type code '%d'!",
1312 __func__,
1313 prop->name,
1314 prop->type);
1315 return nullptr;
1316}
1317
1319
1320/* -------------------------------------------------------------------- */
1323
1325{
1326 if (self->group == nullptr) {
1327 return PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name);
1328 }
1329 return PyUnicode_FromFormat("<%s \"%s\">", Py_TYPE(self)->tp_name, self->group->prop->name);
1330}
1331
1333{
1334 if (self->group != nullptr) {
1335 PyObject_GC_UnTrack(self);
1336 }
1337 Py_CLEAR(self->group);
1338 PyObject_GC_Del(self);
1339}
1340
1341static int BPy_IDGroup_Iter_traverse(BPy_IDGroup_Iter *self, visitproc visit, void *arg)
1342{
1343 Py_VISIT(self->group);
1344 return 0;
1345}
1346
1348{
1349 Py_CLEAR(self->group);
1350 return 0;
1351}
1352
1354{
1355 return (self->group != nullptr);
1356}
1357
1359{
1360 if (self->len_init == self->group->prop->len) {
1361 return true;
1362 }
1363 PyErr_SetString(PyExc_RuntimeError, "IDPropertyGroup changed size during iteration");
1364 return false;
1365}
1366
1368{
1369 if (self->cur != nullptr) {
1370 /* When `cur` is set, `group` cannot be nullptr. */
1372 return nullptr;
1373 }
1374 IDProperty *cur = self->cur;
1375 self->cur = self->reversed ? self->cur->prev : self->cur->next;
1376 return PyUnicode_FromString(cur->name);
1377 }
1378 PyErr_SetNone(PyExc_StopIteration);
1379 return nullptr;
1380}
1381
1383{
1384 if (self->cur != nullptr) {
1385 /* When `cur` is set, `group` cannot be nullptr. */
1387 return nullptr;
1388 }
1389 IDProperty *cur = self->cur;
1390 self->cur = self->reversed ? self->cur->prev : self->cur->next;
1391 return BPy_IDGroup_WrapData(self->group->owner_id, cur, self->group->prop);
1392 }
1393 PyErr_SetNone(PyExc_StopIteration);
1394 return nullptr;
1395}
1396
1398{
1399 if (self->cur != nullptr) {
1400 /* When `cur` is set, `group` cannot be nullptr. */
1402 return nullptr;
1403 }
1404 IDProperty *cur = self->cur;
1405 self->cur = self->reversed ? self->cur->prev : self->cur->next;
1406 PyObject *ret = PyTuple_New(2);
1408 PyUnicode_FromString(cur->name),
1409 BPy_IDGroup_WrapData(self->group->owner_id, cur, self->group->prop));
1410 return ret;
1411 }
1412 PyErr_SetNone(PyExc_StopIteration);
1413 return nullptr;
1414}
1415
1416PyDoc_STRVAR(BPy_IDGroup_IterKeys_doc, "Iterator over :class:`IDPropertyGroup` keys.");
1417PyDoc_STRVAR(BPy_IDGroup_IterValues_doc, "Iterator over :class:`IDPropertyGroup` values.");
1418PyDoc_STRVAR(BPy_IDGroup_IterItems_doc,
1419 "Iterator over :class:`IDPropertyGroup` items (key/value pairs).");
1420
1421PyTypeObject BPy_IDGroup_IterKeys_Type = {PyVarObject_HEAD_INIT(nullptr, 0)};
1422PyTypeObject BPy_IDGroup_IterValues_Type = {PyVarObject_HEAD_INIT(nullptr, 0)};
1423PyTypeObject BPy_IDGroup_IterItems_Type = {PyVarObject_HEAD_INIT(nullptr, 0)};
1424
1425/* ID Property Group Iterator. */
1427{
1428#define SHARED_MEMBER_SET(member, value) \
1429 { \
1430 k_ty->member = v_ty->member = i_ty->member = value; \
1431 } \
1432 ((void)0)
1433
1434 PyTypeObject *k_ty = &BPy_IDGroup_IterKeys_Type;
1435 PyTypeObject *v_ty = &BPy_IDGroup_IterValues_Type;
1436 PyTypeObject *i_ty = &BPy_IDGroup_IterItems_Type;
1437
1438 /* Unique members. */
1439 k_ty->tp_name = "IDPropertyGroupIterKeys";
1440 v_ty->tp_name = "IDPropertyGroupIterValues";
1441 i_ty->tp_name = "IDPropertyGroupIterItems";
1442
1443 k_ty->tp_doc = BPy_IDGroup_IterKeys_doc;
1444 v_ty->tp_doc = BPy_IDGroup_IterValues_doc;
1445 i_ty->tp_doc = BPy_IDGroup_IterItems_doc;
1446
1447 k_ty->tp_iternext = reinterpret_cast<iternextfunc>(BPy_Group_IterKeys_next);
1448 v_ty->tp_iternext = reinterpret_cast<iternextfunc>(BPy_Group_IterValues_next);
1449 i_ty->tp_iternext = reinterpret_cast<iternextfunc>(BPy_Group_IterItems_next);
1450
1451 /* Shared members. */
1452 SHARED_MEMBER_SET(tp_basicsize, sizeof(BPy_IDGroup_Iter));
1453 SHARED_MEMBER_SET(tp_dealloc, (destructor)BPy_IDGroup_Iter_dealloc);
1454 SHARED_MEMBER_SET(tp_repr, (reprfunc)BPy_IDGroup_Iter_repr);
1455 SHARED_MEMBER_SET(tp_flags, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC);
1456 SHARED_MEMBER_SET(tp_traverse, (traverseproc)BPy_IDGroup_Iter_traverse);
1457 SHARED_MEMBER_SET(tp_clear, (inquiry)BPy_IDGroup_Iter_clear);
1458 SHARED_MEMBER_SET(tp_is_gc, (inquiry)BPy_IDGroup_Iter_is_gc);
1459 SHARED_MEMBER_SET(tp_iter, PyObject_SelfIter);
1460
1461#undef SHARED_MEMBER_SET
1462}
1463
1465 const bool reversed,
1466 PyTypeObject *type)
1467{
1468 BLI_assert(group ? group->prop->type == IDP_GROUP : true);
1469 BPy_IDGroup_Iter *iter = PyObject_GC_New(BPy_IDGroup_Iter, type);
1470 iter->reversed = reversed;
1471 iter->group = group;
1472 if (group != nullptr) {
1473 Py_INCREF(group);
1474 BLI_assert(!PyObject_GC_IsTracked((PyObject *)iter));
1475 PyObject_GC_Track(iter);
1476 iter->cur = static_cast<IDProperty *>(
1477 (reversed ? group->prop->data.group.last() : group->prop->data.group.first()));
1478 iter->len_init = group->prop->len;
1479 }
1480 else {
1481 iter->cur = nullptr;
1482 iter->len_init = 0;
1483 }
1484 return reinterpret_cast<PyObject *>(iter);
1485}
1486
1487static PyObject *BPy_IDGroup_IterKeys_CreatePyObject(BPy_IDProperty *group, const bool reversed)
1488{
1489 return IDGroup_Iter_New_WithType(group, reversed, &BPy_IDGroup_IterKeys_Type);
1490}
1491
1492static PyObject *BPy_IDGroup_IterValues_CreatePyObject(BPy_IDProperty *group, const bool reversed)
1493{
1495}
1496
1497static PyObject *BPy_IDGroup_IterItems_CreatePyObject(BPy_IDProperty *group, const bool reversed)
1498{
1499 return IDGroup_Iter_New_WithType(group, reversed, &BPy_IDGroup_IterItems_Type);
1500}
1501
1503
1504/* -------------------------------------------------------------------- */
1517
1519{
1520 if (self->group == nullptr) {
1521 return PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name);
1522 }
1523 return PyUnicode_FromFormat("<%s \"%s\">", Py_TYPE(self)->tp_name, self->group->prop->name);
1524}
1525
1527{
1528 if (self->group != nullptr) {
1529 PyObject_GC_UnTrack(self);
1530 }
1531 Py_CLEAR(self->group);
1532 PyObject_GC_Del(self);
1533}
1534
1535static int BPy_IDGroup_View_traverse(BPy_IDGroup_View *self, visitproc visit, void *arg)
1536{
1537 Py_VISIT(self->group);
1538 return 0;
1539}
1540
1542{
1543 Py_CLEAR(self->group);
1544 return 0;
1545}
1546
1548{
1549 return (self->group != nullptr);
1550}
1551
1552/* View Specific API's (Key/Value/Items). */
1553
1555{
1557}
1558
1560{
1562}
1563
1565{
1567}
1568
1569static Py_ssize_t BPy_Group_View_len(BPy_IDGroup_View *self)
1570{
1571 if (self->group == nullptr) {
1572 return 0;
1573 }
1574 return self->group->prop->len;
1575}
1576
1577static int BPy_Group_ViewKeys_Contains(BPy_IDGroup_View *self, PyObject *value)
1578{
1579 if (self->group == nullptr) {
1580 return 0;
1581 }
1582 return BPy_IDGroup_Contains(self->group, value);
1583}
1584
1585static int BPy_Group_ViewValues_Contains(BPy_IDGroup_View *self, PyObject *value)
1586{
1587 if (self->group == nullptr) {
1588 return 0;
1589 }
1590 /* TODO: implement this without first converting to a list. */
1591 PyObject *list = PySequence_List(reinterpret_cast<PyObject *>(self));
1592 const int result = PySequence_Contains(list, value);
1593 Py_DECREF(list);
1594 return result;
1595}
1596
1597static int BPy_Group_ViewItems_Contains(BPy_IDGroup_View *self, PyObject *value)
1598{
1599 if (self->group == nullptr) {
1600 return 0;
1601 }
1602 /* TODO: implement this without first converting to a list. */
1603 PyObject *list = PySequence_List(reinterpret_cast<PyObject *>(self));
1604 const int result = PySequence_Contains(list, value);
1605 Py_DECREF(list);
1606 return result;
1607}
1608
1609static PySequenceMethods BPy_IDGroup_ViewKeys_as_sequence = {
1610 /*sq_length*/ reinterpret_cast<lenfunc>(BPy_Group_View_len),
1611 /*sq_concat*/ nullptr,
1612 /*sq_repeat*/ nullptr,
1613 /*sq_item*/ nullptr,
1614 /*was_sq_slice*/ nullptr,
1615 /*sq_ass_item*/ nullptr,
1616 /*was_sq_ass_slice*/ nullptr,
1617 /*sq_contains*/ reinterpret_cast<objobjproc>(BPy_Group_ViewKeys_Contains),
1618};
1619
1620static PySequenceMethods BPy_IDGroup_ViewValues_as_sequence = {
1621 /*sq_length*/ reinterpret_cast<lenfunc>(BPy_Group_View_len),
1622 /*sq_concat*/ nullptr,
1623 /*sq_repeat*/ nullptr,
1624 /*sq_item*/ nullptr,
1625 /*was_sq_slice*/ nullptr,
1626 /*sq_ass_item*/ nullptr,
1627 /*was_sq_ass_slice*/ nullptr,
1628 /*sq_contains*/ reinterpret_cast<objobjproc>(BPy_Group_ViewValues_Contains),
1629};
1630
1631static PySequenceMethods BPy_IDGroup_ViewItems_as_sequence = {
1632 /*sq_length*/ reinterpret_cast<lenfunc>(BPy_Group_View_len),
1633 /*sq_concat*/ nullptr,
1634 /*sq_repeat*/ nullptr,
1635 /*sq_item*/ nullptr,
1636 /*was_sq_slice*/ nullptr,
1637 /*sq_ass_item*/ nullptr,
1638 /*was_sq_ass_slice*/ nullptr,
1639 /*sq_contains*/ reinterpret_cast<objobjproc>(BPy_Group_ViewItems_Contains),
1640};
1641
1642/* Methods. */
1643
1645 /* Wrap. */
1646 BPy_IDGroup_View_reversed_doc,
1647 "Return a reverse iterator over the ID Property keys values or items.");
1648static PyObject *BPy_IDGroup_View_reversed(BPy_IDGroup_View *self, PyObject * /*ignored*/)
1649{
1650 BPy_IDGroup_View *result = IDGroup_View_New_WithType(self->group, Py_TYPE(self));
1651 result->reversed = !self->reversed;
1652 return reinterpret_cast<PyObject *>(result);
1653}
1654
1655static PyMethodDef BPy_IDGroup_View_methods[] = {
1656 {"__reversed__",
1657 (PyCFunction)(void (*)())BPy_IDGroup_View_reversed,
1658 METH_NOARGS,
1659 BPy_IDGroup_View_reversed_doc},
1660 {nullptr, nullptr},
1661};
1662
1664 /* Wrap. */
1665 BPy_IDGroup_ViewKeys_doc,
1666 "A view of :class:`IDPropertyGroup` keys "
1667 "(supports ``len()``, ``in``, iteration, and ``reversed()``).");
1669 /* Wrap. */
1670 BPy_IDGroup_ViewValues_doc,
1671 "A view of :class:`IDPropertyGroup` values "
1672 "(supports ``len()``, ``in``, iteration, and ``reversed()``).");
1674 /* Wrap. */
1675 BPy_IDGroup_ViewItems_doc,
1676 "A view of :class:`IDPropertyGroup` items as key/value pairs "
1677 "(supports ``len()``, ``in``, iteration, and ``reversed()``).");
1678
1679PyTypeObject BPy_IDGroup_ViewKeys_Type = {PyVarObject_HEAD_INIT(nullptr, 0)};
1680PyTypeObject BPy_IDGroup_ViewValues_Type = {PyVarObject_HEAD_INIT(nullptr, 0)};
1681PyTypeObject BPy_IDGroup_ViewItems_Type = {PyVarObject_HEAD_INIT(nullptr, 0)};
1682
1683/* ID Property Group View. */
1685{
1686 PyTypeObject *k_ty = &BPy_IDGroup_ViewKeys_Type;
1687 PyTypeObject *v_ty = &BPy_IDGroup_ViewValues_Type;
1688 PyTypeObject *i_ty = &BPy_IDGroup_ViewItems_Type;
1689
1690 /* Unique members. */
1691 k_ty->tp_name = "IDPropertyGroupViewKeys";
1692 v_ty->tp_name = "IDPropertyGroupViewValues";
1693 i_ty->tp_name = "IDPropertyGroupViewItems";
1694
1695 k_ty->tp_doc = BPy_IDGroup_ViewKeys_doc;
1696 v_ty->tp_doc = BPy_IDGroup_ViewValues_doc;
1697 i_ty->tp_doc = BPy_IDGroup_ViewItems_doc;
1698
1699 k_ty->tp_iter = reinterpret_cast<getiterfunc>(BPy_Group_ViewKeys_iter);
1700 v_ty->tp_iter = reinterpret_cast<getiterfunc>(BPy_Group_ViewValues_iter);
1701 i_ty->tp_iter = reinterpret_cast<getiterfunc>(BPy_Group_ViewItems_iter);
1702
1703 k_ty->tp_as_sequence = &BPy_IDGroup_ViewKeys_as_sequence;
1704 v_ty->tp_as_sequence = &BPy_IDGroup_ViewValues_as_sequence;
1705 i_ty->tp_as_sequence = &BPy_IDGroup_ViewItems_as_sequence;
1706
1707/* Shared members. */
1708#define SHARED_MEMBER_SET(member, value) \
1709 { \
1710 k_ty->member = v_ty->member = i_ty->member = value; \
1711 } \
1712 ((void)0)
1713
1714 SHARED_MEMBER_SET(tp_basicsize, sizeof(BPy_IDGroup_View));
1715 SHARED_MEMBER_SET(tp_dealloc, (destructor)BPy_IDGroup_View_dealloc);
1716 SHARED_MEMBER_SET(tp_repr, (reprfunc)BPy_IDGroup_View_repr);
1717 SHARED_MEMBER_SET(tp_flags, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC);
1718 SHARED_MEMBER_SET(tp_traverse, (traverseproc)BPy_IDGroup_View_traverse);
1719 SHARED_MEMBER_SET(tp_clear, (inquiry)BPy_IDGroup_View_clear);
1720 SHARED_MEMBER_SET(tp_is_gc, (inquiry)BPy_IDGroup_View_is_gc);
1722
1723#undef SHARED_MEMBER_SET
1724}
1725
1727
1728/* -------------------------------------------------------------------- */
1731
1733 /* Wrap. */
1734 BPy_IDGroup_pop_doc,
1735 ".. method:: pop(key, default)\n"
1736 "\n"
1737 " Remove an item from the group, returning a Python representation.\n"
1738 "\n"
1739 " :raises KeyError: When the item doesn't exist and no *default* is given.\n"
1740 "\n"
1741 " :param key: Name of item to remove.\n"
1742 " :type key: str\n"
1743 " :param default: Value to return when *key* isn't found "
1744 "(optional, a :exc:`KeyError` is raised when omitted and the key is not found).\n"
1745 " :type default: Any\n"
1746 " :return: A Python representation of the removed item, or *default*.\n"
1747 " :rtype: Any\n");
1748static PyObject *BPy_IDGroup_pop(BPy_IDProperty *self, PyObject *args)
1749{
1751 PyObject *pyform;
1752
1753 char *key;
1754 PyObject *def = nullptr;
1755
1756 if (!PyArg_ParseTuple(args,
1757 "s" /* `key` */
1758 "|" /* Optional arguments. */
1759 "O" /* `default` */
1760 ":pop",
1761 &key,
1762 &def))
1763 {
1764 return nullptr;
1765 }
1766
1767 idprop = IDP_GetPropertyFromGroup(self->prop, key);
1768 if (idprop == nullptr) {
1769 if (def == nullptr) {
1770 PyErr_SetString(PyExc_KeyError, "item not in group");
1771 return nullptr;
1772 }
1773 return Py_NewRef(def);
1774 }
1775
1777 if (pyform == nullptr) {
1778 /* Ok something bad happened with the #PyObject, so don't remove the prop from the group.
1779 * if `pyform` is nullptr, then it already should have raised an exception. */
1780 return nullptr;
1781 }
1782
1784 return pyform;
1785}
1786
1787/* utility function */
1788static void BPy_IDGroup_CorrectListLen(IDProperty *prop, PyObject *seq, int len, const char *func)
1789{
1790 int j;
1791
1792 printf("%s: ID Property Error found and corrected!\n", func);
1793
1794 /* fill rest of list with valid references to None */
1795 for (j = len; j < prop->len; j++) {
1796 PyList_SET_ITEM(seq, j, Py_NewRef(Py_None));
1797 }
1798
1799 /* Set correct group length. */
1800 prop->len = len;
1801}
1802
1804{
1805 PyObject *list = PyList_New(prop->len);
1806 IDProperty *loop;
1807 int i;
1808
1809 for (i = 0, loop = prop->data.group.first(); loop && (i < prop->len); loop = loop->next, i++) {
1810 PyList_SET_ITEM(list, i, PyUnicode_FromString(loop->name));
1811 }
1812
1813 /* if the id prop is corrupt, count the remaining */
1814 for (; loop; loop = loop->next, i++) {
1815 /* pass */
1816 }
1817
1818 if (i != prop->len) { /* if the loop didn't finish, we know the length is wrong */
1819 BPy_IDGroup_CorrectListLen(prop, list, i, __func__);
1820 Py_DECREF(list); /* Free the list. */
1821 /* Call self again. */
1822 return BPy_Wrap_GetKeys(prop);
1823 }
1824
1825 return list;
1826}
1827
1828PyObject *BPy_Wrap_GetValues(ID *id, IDProperty *prop)
1829{
1830 PyObject *list = PyList_New(prop->len);
1831 IDProperty *loop;
1832 int i;
1833
1834 for (i = 0, loop = prop->data.group.first(); loop; loop = loop->next, i++) {
1835 PyList_SET_ITEM(list, i, BPy_IDGroup_WrapData(id, loop, prop));
1836 }
1837
1838 if (i != prop->len) {
1839 BPy_IDGroup_CorrectListLen(prop, list, i, __func__);
1840 Py_DECREF(list); /* Free the list. */
1841 /* Call self again. */
1842 return BPy_Wrap_GetValues(id, prop);
1843 }
1844
1845 return list;
1846}
1847
1848PyObject *BPy_Wrap_GetItems(ID *id, IDProperty *prop)
1849{
1850 PyObject *seq = PyList_New(prop->len);
1851 IDProperty *loop;
1852 int i;
1853
1854 for (i = 0, loop = prop->data.group.first(); loop; loop = loop->next, i++) {
1855 PyObject *item = PyTuple_New(2);
1857 item, PyUnicode_FromString(loop->name), BPy_IDGroup_WrapData(id, loop, prop));
1858 PyList_SET_ITEM(seq, i, item);
1859 }
1860
1861 if (i != prop->len) {
1862 BPy_IDGroup_CorrectListLen(prop, seq, i, __func__);
1863 Py_DECREF(seq); /* Free the list. */
1864 /* Call self again. */
1865 return BPy_Wrap_GetItems(id, prop);
1866 }
1867
1868 return seq;
1869}
1870
1872{
1873 PyObject *self = prop ? idprop_py_from_idp_group(id, prop, nullptr) : nullptr;
1874 PyObject *ret = BPy_IDGroup_ViewKeys_CreatePyObject(reinterpret_cast<BPy_IDProperty *>(self));
1875 Py_XDECREF(self); /* Owned by `ret`. */
1876 return ret;
1877}
1878
1880{
1881 PyObject *self = prop ? idprop_py_from_idp_group(id, prop, nullptr) : nullptr;
1882 PyObject *ret = BPy_IDGroup_ViewValues_CreatePyObject(reinterpret_cast<BPy_IDProperty *>(self));
1883 Py_XDECREF(self); /* Owned by `ret`. */
1884 return ret;
1885}
1886
1888{
1889 PyObject *self = prop ? idprop_py_from_idp_group(id, prop, nullptr) : nullptr;
1890 PyObject *ret = BPy_IDGroup_ViewItems_CreatePyObject(reinterpret_cast<BPy_IDProperty *>(self));
1891 Py_XDECREF(self); /* Owned by `ret`. */
1892 return ret;
1893}
1894
1896 /* Wrap. */
1897 BPy_IDGroup_keys_doc,
1898 ".. method:: keys()\n"
1899 "\n"
1900 " Return a view of the keys in the group.\n"
1901 "\n"
1902 " :return: A view of the keys.\n"
1903 " :rtype: :class:`IDPropertyGroupViewKeys`\n");
1904static PyObject *BPy_IDGroup_keys(BPy_IDProperty *self)
1905{
1907}
1908
1910 /* Wrap. */
1911 BPy_IDGroup_values_doc,
1912 ".. method:: values()\n"
1913 "\n"
1914 " Return the values associated with this group.\n"
1915 "\n"
1916 " :return: A view of the values.\n"
1917 " :rtype: :class:`IDPropertyGroupViewValues`\n");
1918static PyObject *BPy_IDGroup_values(BPy_IDProperty *self)
1919{
1921}
1922
1924 /* Wrap. */
1925 BPy_IDGroup_items_doc,
1926 ".. method:: items()\n"
1927 "\n"
1928 " Return a view of the items in the group, behaves like dictionary method items.\n"
1929 "\n"
1930 " :return: A view of the items.\n"
1931 " :rtype: :class:`IDPropertyGroupViewItems`\n");
1932static PyObject *BPy_IDGroup_items(BPy_IDProperty *self)
1933{
1935}
1936
1937static int BPy_IDGroup_Contains(BPy_IDProperty *self, PyObject *value)
1938{
1939 const char *name = PyUnicode_AsUTF8(value);
1940
1941 if (!name) {
1942 PyErr_Format(PyExc_TypeError, "expected a string, not a %.200s", Py_TYPE(value)->tp_name);
1943 return -1;
1944 }
1945
1946 return IDP_GetPropertyFromGroup(self->prop, name) ? 1 : 0;
1947}
1948
1950 /* Wrap. */
1951 BPy_IDGroup_update_doc,
1952 ".. method:: update(other)\n"
1953 "\n"
1954 " Update key-value pairs from *other*, overwriting existing keys.\n"
1955 "\n"
1956 " .. note::\n"
1957 "\n"
1958 " Unlike :meth:`dict.update`, keyword arguments are not supported.\n"
1959 "\n"
1960 " :param other: Updates the values in the group with this.\n"
1961 /* TODO: replace `Any` with an alias for all types an ID property can use. */
1962 " :type other: :class:`IDPropertyGroup` | dict[str, Any]\n");
1963static PyObject *BPy_IDGroup_update(BPy_IDProperty *self, PyObject *value)
1964{
1965 PyObject *pkey, *pval;
1966 Py_ssize_t i = 0;
1967
1968 if (BPy_IDGroup_Check(value)) {
1969 BPy_IDProperty *other = reinterpret_cast<BPy_IDProperty *>(value);
1970 if (self->prop == other->prop) [[unlikely]] {
1971 Py_RETURN_NONE;
1972 }
1973
1974 /* XXX, possible one is inside the other */
1975 IDP_MergeGroup(self->prop, other->prop, true);
1976 }
1977 else if (PyDict_Check(value)) {
1978 while (PyDict_Next(value, &i, &pkey, &pval)) {
1979 BPy_IDGroup_Map_SetItem(self, pkey, pval);
1980 if (PyErr_Occurred()) {
1981 return nullptr;
1982 }
1983 }
1984 }
1985 else {
1986 PyErr_Format(PyExc_TypeError,
1987 "expected a dict or an IDPropertyGroup type, not a %.200s",
1988 Py_TYPE(value)->tp_name);
1989 return nullptr;
1990 }
1991
1992 Py_RETURN_NONE;
1993}
1994
1996 /* Wrap. */
1997 BPy_IDGroup_to_dict_doc,
1998 ".. method:: to_dict()\n"
1999 "\n"
2000 " Return a purely Python version of the group.\n"
2001 "\n"
2002 " :return: A dictionary representation of the group.\n"
2003 " :rtype: dict[str, Any]\n");
2005{
2006 return BPy_IDGroup_MapDataToPy(self->prop);
2007}
2008
2010 /* Wrap. */
2011 BPy_IDGroup_clear_doc,
2012 ".. method:: clear()\n"
2013 "\n"
2014 " Clear all members from this group.\n");
2015static PyObject *BPy_IDGroup_clear(BPy_IDProperty *self)
2016{
2017 IDP_ClearProperty(self->prop);
2018 Py_RETURN_NONE;
2019}
2020
2022 /* Wrap. */
2023 BPy_IDGroup_get_doc,
2024 ".. method:: get(key, default=None)\n"
2025 "\n"
2026 " Return the value for key, if it exists, else default.\n"
2027 "\n"
2028 " :param key: The key to look up.\n"
2029 " :type key: str\n"
2030 " :param default: Value to return if *key* is not found.\n"
2031 " :type default: Any\n"
2032 " :return: The value for the key, or *default* if not found.\n"
2033 " :rtype: Any\n");
2034static PyObject *BPy_IDGroup_get(BPy_IDProperty *self, PyObject *args)
2035{
2037 const char *key;
2038 PyObject *def = Py_None;
2039
2040 if (!PyArg_ParseTuple(args,
2041 "s" /* `key` */
2042 "|" /* Optional arguments. */
2043 "O" /* `default` */
2044 ":get",
2045 &key,
2046 &def))
2047 {
2048 return nullptr;
2049 }
2050
2051 idprop = IDP_GetPropertyFromGroup(self->prop, key);
2052 if (idprop) {
2053 PyObject *pyobj = BPy_IDGroup_WrapData(self->owner_id, idprop, self->prop);
2054 if (pyobj) {
2055 return pyobj;
2056 }
2057 }
2058
2059 Py_INCREF(def);
2060 return def;
2061}
2062
2063#ifdef __GNUC__
2064# ifdef __clang__
2065# pragma clang diagnostic push
2066# pragma clang diagnostic ignored "-Wcast-function-type"
2067# else
2068# pragma GCC diagnostic push
2069# pragma GCC diagnostic ignored "-Wcast-function-type"
2070# endif
2071#endif
2072
2073static PyMethodDef BPy_IDGroup_methods[] = {
2074 {"pop", reinterpret_cast<PyCFunction>(BPy_IDGroup_pop), METH_VARARGS, BPy_IDGroup_pop_doc},
2075 {"keys", reinterpret_cast<PyCFunction>(BPy_IDGroup_keys), METH_NOARGS, BPy_IDGroup_keys_doc},
2076 {"values",
2077 reinterpret_cast<PyCFunction>(BPy_IDGroup_values),
2078 METH_NOARGS,
2079 BPy_IDGroup_values_doc},
2080 {"items",
2081 reinterpret_cast<PyCFunction>(BPy_IDGroup_items),
2082 METH_NOARGS,
2083 BPy_IDGroup_items_doc},
2084 {"update", reinterpret_cast<PyCFunction>(BPy_IDGroup_update), METH_O, BPy_IDGroup_update_doc},
2085 {"get", reinterpret_cast<PyCFunction>(BPy_IDGroup_get), METH_VARARGS, BPy_IDGroup_get_doc},
2086 {"to_dict",
2087 reinterpret_cast<PyCFunction>(BPy_IDGroup_to_dict),
2088 METH_NOARGS,
2089 BPy_IDGroup_to_dict_doc},
2090 {"clear",
2091 reinterpret_cast<PyCFunction>(BPy_IDGroup_clear),
2092 METH_NOARGS,
2093 BPy_IDGroup_clear_doc},
2094 {nullptr, nullptr, 0, nullptr},
2095};
2096
2097#ifdef __GNUC__
2098# ifdef __clang__
2099# pragma clang diagnostic pop
2100# else
2101# pragma GCC diagnostic pop
2102# endif
2103#endif
2104
2106
2107/* -------------------------------------------------------------------- */
2110
2111static PySequenceMethods BPy_IDGroup_Seq = {
2112 /*sq_length*/ reinterpret_cast<lenfunc>(BPy_IDGroup_Map_Len),
2113 /*sq_concat*/ nullptr,
2114 /*sq_repeat*/ nullptr,
2115 /* TODO: setting this will allow `PySequence_Check()` to return True. */
2116 /*sq_item*/ nullptr,
2117 /*was_sq_slice*/ nullptr, /* DEPRECATED. */
2118 /*sq_ass_item*/ nullptr,
2119 /*was_sq_ass_slice*/ nullptr, /* DEPRECATED. */
2120 /*sq_contains*/ reinterpret_cast<objobjproc>(BPy_IDGroup_Contains),
2121 /*sq_inplace_concat*/ nullptr,
2122 /*sq_inplace_repeat*/ nullptr,
2123};
2124
2125static PyMappingMethods BPy_IDGroup_Mapping = {
2126 /*mp_length*/ reinterpret_cast<lenfunc>(BPy_IDGroup_Map_Len),
2127 /*mp_subscript*/ reinterpret_cast<binaryfunc>(BPy_IDGroup_Map_GetItem),
2128 /*mp_ass_subscript*/ reinterpret_cast<objobjargproc>(BPy_IDGroup_Map_SetItem),
2129};
2130
2132 /* Wrap. */
2133 BPy_IDGroup_Type_doc,
2134 "A dictionary-like group of ID properties, "
2135 "supporting key access, iteration, and membership testing.\n"
2136 "\n"
2137 ".. note::\n"
2138 "\n"
2139 " Only supports a maximum of 1024 levels of nesting.");
2140
2141PyTypeObject BPy_IDGroup_Type = {
2142 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
2143 /* For printing, in format `<module>.<name>`. */
2144 /*tp_name*/ "IDPropertyGroup",
2145 /*tp_basicsize*/ sizeof(BPy_IDProperty),
2146 /*tp_itemsize*/ 0,
2147 /*tp_dealloc*/ nullptr,
2148 /*tp_vectorcall_offset*/ 0,
2149 /*tp_getattr*/ nullptr,
2150 /*tp_setattr*/ nullptr,
2151 /*tp_as_async*/ nullptr,
2152 /*tp_repr*/ reinterpret_cast<reprfunc>(BPy_IDGroup_repr),
2153 /*tp_as_number*/ nullptr,
2154 /*tp_as_sequence*/ &BPy_IDGroup_Seq,
2155 /*tp_as_mapping*/ &BPy_IDGroup_Mapping,
2156 /*tp_hash*/ reinterpret_cast<hashfunc>(BPy_IDGroup_hash),
2157 /*tp_call*/ nullptr,
2158 /*tp_str*/ nullptr,
2159 /*tp_getattro*/ nullptr,
2160 /*tp_setattro*/ nullptr,
2161 /*tp_as_buffer*/ nullptr,
2162 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
2163 /*tp_doc*/ BPy_IDGroup_Type_doc,
2164 /*tp_traverse*/ nullptr,
2165 /*tp_clear*/ nullptr,
2166 /*tp_richcompare*/ nullptr,
2167 /*tp_weaklistoffset*/ 0,
2168 /*tp_iter*/ reinterpret_cast<getiterfunc>(BPy_IDGroup_iter),
2169 /*tp_iternext*/ nullptr,
2170 /*tp_methods*/ BPy_IDGroup_methods,
2171 /*tp_members*/ nullptr,
2172 /*tp_getset*/ BPy_IDGroup_getseters,
2173 /*tp_base*/ nullptr,
2174 /*tp_dict*/ nullptr,
2175 /*tp_descr_get*/ nullptr,
2176 /*tp_descr_set*/ nullptr,
2177 /*tp_dictoffset*/ 0,
2178 /*tp_init*/ nullptr,
2179 /*tp_alloc*/ nullptr,
2180 /*tp_new*/ nullptr,
2181 /*tp_free*/ nullptr,
2182 /*tp_is_gc*/ nullptr,
2183 /*tp_bases*/ nullptr,
2184 /*tp_mro*/ nullptr,
2185 /*tp_cache*/ nullptr,
2186 /*tp_subclasses*/ nullptr,
2187 /*tp_weaklist*/ nullptr,
2188 /*tp_del*/ nullptr,
2189 /*tp_version_tag*/ 0,
2190 /*tp_finalize*/ nullptr,
2191 /*tp_vectorcall*/ nullptr,
2192};
2193
2195
2196/* -------------------------------------------------------------------- */
2199
2200static PyTypeObject *idp_array_py_type(BPy_IDArray *self, size_t *elem_size)
2201{
2202 switch (self->prop->subtype) {
2203 case IDP_FLOAT:
2204 *elem_size = sizeof(float);
2205 return &PyFloat_Type;
2206 case IDP_DOUBLE:
2207 *elem_size = sizeof(double);
2208 return &PyFloat_Type;
2209 case IDP_BOOLEAN:
2210 *elem_size = sizeof(int8_t);
2211 return &PyBool_Type;
2212 case IDP_INT:
2213 *elem_size = sizeof(int);
2214 return &PyLong_Type;
2215 default:
2216 *elem_size = 0;
2217 return nullptr;
2218 }
2219}
2220
2221static PyObject *BPy_IDArray_repr(BPy_IDArray *self)
2222{
2223 return PyUnicode_FromFormat("<bpy id property array [%d]>", self->prop->len);
2224}
2225
2227 /* Wrap. */
2228 BPy_IDArray_get_typecode_doc,
2229 "The type of the data in the array "
2230 "{'f': float (32-bit), 'd': double (64-bit), 'i': int, 'b': bool}. "
2231 "Both 'f' and 'd' use Python's :class:`float` type but differ in storage precision.\n"
2232 "\n"
2233 ":type: Literal['f', 'd', 'i', 'b']\n");
2234static PyObject *BPy_IDArray_get_typecode(BPy_IDArray *self, void * /*closure*/)
2235{
2236 const char *typecode;
2237 switch (self->prop->subtype) {
2238 case IDP_FLOAT:
2239 typecode = "f";
2240 break;
2241 case IDP_DOUBLE:
2242 typecode = "d";
2243 break;
2244 case IDP_INT:
2245 typecode = "i";
2246 break;
2247 case IDP_BOOLEAN:
2248 typecode = "b";
2249 break;
2250 default: {
2251 PyErr_Format(PyExc_RuntimeError,
2252 "%s: invalid/corrupt array type '%d'!",
2253 __func__,
2254 self->prop->subtype);
2255
2256 return nullptr;
2257 }
2258 }
2259 return PyUnicode_FromString(typecode);
2260}
2261
2262static PyGetSetDef BPy_IDArray_getseters[] = {
2263 /* matches pythons array.typecode */
2264 {"typecode",
2265 reinterpret_cast<getter>(BPy_IDArray_get_typecode),
2266 static_cast<setter>(nullptr),
2267 BPy_IDArray_get_typecode_doc,
2268 nullptr},
2269 {nullptr, nullptr, nullptr, nullptr, nullptr},
2270};
2271
2273 /* Wrap. */
2274 BPy_IDArray_to_list_doc,
2275 ".. method:: to_list()\n"
2276 "\n"
2277 " Return the array as a list.\n"
2278 "\n"
2279 " :return: The array as a list.\n"
2280 " :rtype: list[int] | list[float] | list[bool]\n");
2281static PyObject *BPy_IDArray_to_list(BPy_IDArray *self)
2282{
2283 return BPy_IDGroup_MapDataToPy(self->prop);
2284}
2285
2286#ifdef __GNUC__
2287# ifdef __clang__
2288# pragma clang diagnostic push
2289# pragma clang diagnostic ignored "-Wcast-function-type"
2290# else
2291# pragma GCC diagnostic push
2292# pragma GCC diagnostic ignored "-Wcast-function-type"
2293# endif
2294#endif
2295
2296static PyMethodDef BPy_IDArray_methods[] = {
2297 {"to_list",
2298 reinterpret_cast<PyCFunction>(BPy_IDArray_to_list),
2299 METH_NOARGS,
2300 BPy_IDArray_to_list_doc},
2301 {nullptr, nullptr, 0, nullptr},
2302};
2303
2304#ifdef __GNUC__
2305# ifdef __clang__
2306# pragma clang diagnostic pop
2307# else
2308# pragma GCC diagnostic pop
2309# endif
2310#endif
2311
2312static Py_ssize_t BPy_IDArray_Len(BPy_IDArray *self)
2313{
2314 return self->prop->len;
2315}
2316
2317static PyObject *BPy_IDArray_GetItem(BPy_IDArray *self, Py_ssize_t index)
2318{
2319 if (index < 0 || index >= self->prop->len) {
2320 PyErr_SetString(PyExc_IndexError, "index out of range!");
2321 return nullptr;
2322 }
2323
2324 switch (self->prop->subtype) {
2325 case IDP_FLOAT:
2326 return PyFloat_FromDouble(IDP_array_float_get(self->prop)[index]);
2327 case IDP_DOUBLE:
2328 return PyFloat_FromDouble(IDP_array_double_get(self->prop)[index]);
2329 case IDP_INT:
2330 return PyLong_FromLong(long(IDP_array_int_get(self->prop)[index]));
2331 case IDP_BOOLEAN:
2332 return PyBool_FromLong(long(IDP_array_bool_get(self->prop)[index]));
2333 }
2334
2335 PyErr_Format(
2336 PyExc_RuntimeError, "%s: invalid/corrupt array type '%d'!", __func__, self->prop->subtype);
2337
2338 return nullptr;
2339}
2340
2341static int BPy_IDArray_SetItem(BPy_IDArray *self, Py_ssize_t index, PyObject *value)
2342{
2343 if (index < 0 || index >= self->prop->len) {
2344 PyErr_SetString(PyExc_RuntimeError, "index out of range!");
2345 return -1;
2346 }
2347
2348 switch (self->prop->subtype) {
2349 case IDP_FLOAT: {
2350 const float f = float(PyFloat_AsDouble(value));
2351 if (f == -1 && PyErr_Occurred()) {
2352 return -1;
2353 }
2354 IDP_array_float_get(self->prop)[index] = f;
2355 break;
2356 }
2357 case IDP_DOUBLE: {
2358 const double d = PyFloat_AsDouble(value);
2359 if (d == -1 && PyErr_Occurred()) {
2360 return -1;
2361 }
2362 IDP_array_double_get(self->prop)[index] = d;
2363 break;
2364 }
2365 case IDP_INT: {
2366 const int i = PyC_Long_AsI32(value);
2367 if (i == -1 && PyErr_Occurred()) {
2368 return -1;
2369 }
2370
2371 IDP_array_int_get(self->prop)[index] = i;
2372 break;
2373 }
2374 case IDP_BOOLEAN: {
2375 const int i = PyC_Long_AsBool(value);
2376 if (i == -1 && PyErr_Occurred()) {
2377 return -1;
2378 }
2379
2380 IDP_array_bool_get(self->prop)[index] = i;
2381 break;
2382 }
2383 }
2384 return 0;
2385}
2386
2387static PySequenceMethods BPy_IDArray_Seq = {
2388 /*sq_length*/ reinterpret_cast<lenfunc>(BPy_IDArray_Len),
2389 /*sq_concat*/ nullptr,
2390 /*sq_repeat*/ nullptr,
2391 /*sq_item*/ reinterpret_cast<ssizeargfunc>(BPy_IDArray_GetItem),
2392 /*was_sq_slice*/ nullptr, /* DEPRECATED. */
2393 /*sq_ass_item*/ reinterpret_cast<ssizeobjargproc>(BPy_IDArray_SetItem),
2394 /*was_sq_ass_slice*/ nullptr, /* DEPRECATED. */
2395 /*sq_contains*/ nullptr,
2396 /*sq_inplace_concat*/ nullptr,
2397 /*sq_inplace_repeat*/ nullptr,
2398};
2399
2400/* sequence slice (get): idparr[a:b] */
2401static PyObject *BPy_IDArray_slice(BPy_IDArray *self, int begin, int end)
2402{
2403 IDProperty *prop = self->prop;
2404 PyObject *tuple;
2405 int count;
2406
2407 CLAMP(begin, 0, prop->len);
2408 if (end < 0) {
2409 end = prop->len + end + 1;
2410 }
2411 CLAMP(end, 0, prop->len);
2412 begin = std::min(begin, end);
2413
2414 tuple = PyTuple_New(end - begin);
2415
2416 switch (prop->subtype) {
2417 case IDP_FLOAT: {
2418 const float *array = IDP_array_float_get(prop);
2419 for (count = begin; count < end; count++) {
2420 PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(array[count]));
2421 }
2422 break;
2423 }
2424 case IDP_DOUBLE: {
2425 const double *array = IDP_array_double_get(prop);
2426 for (count = begin; count < end; count++) {
2427 PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(array[count]));
2428 }
2429 break;
2430 }
2431 case IDP_INT: {
2432 const int *array = IDP_array_int_get(prop);
2433 for (count = begin; count < end; count++) {
2434 PyTuple_SET_ITEM(tuple, count - begin, PyLong_FromLong(array[count]));
2435 }
2436 break;
2437 }
2438 case IDP_BOOLEAN: {
2439 const int8_t *array = IDP_array_bool_get(prop);
2440 for (count = begin; count < end; count++) {
2441 PyTuple_SET_ITEM(tuple, count - begin, PyBool_FromLong(long(array[count])));
2442 }
2443 break;
2444 }
2445 }
2446
2447 return tuple;
2448}
2449/* sequence slice (set): idparr[a:b] = value */
2450static int BPy_IDArray_ass_slice(BPy_IDArray *self, int begin, int end, PyObject *seq)
2451{
2452 IDProperty *prop = self->prop;
2453 size_t elem_size;
2454 const PyTypeObject *py_type = idp_array_py_type(self, &elem_size);
2455 size_t alloc_len;
2456 size_t size;
2457 void *vec;
2458
2459 CLAMP(begin, 0, prop->len);
2460 CLAMP(end, 0, prop->len);
2461 begin = std::min(begin, end);
2462
2463 size = (end - begin);
2464 alloc_len = size * elem_size;
2465
2466 /* NOTE: we count on int/float being the same size here */
2467 vec = MEM_new_uninitialized(alloc_len, "array assignment");
2468
2469 if (PyC_AsArray(vec, elem_size, seq, size, py_type, "slice assignment: ") == -1) {
2470 MEM_delete_void(vec);
2471 return -1;
2472 }
2473
2474 memcpy(
2475 static_cast<void *>((static_cast<char *> IDP_array_voidp_get(prop)) + (begin * elem_size)),
2476 vec,
2477 alloc_len);
2478
2479 MEM_delete_void(vec);
2480 return 0;
2481}
2482
2483static PyObject *BPy_IDArray_subscript(BPy_IDArray *self, PyObject *item)
2484{
2485 if (PyIndex_Check(item)) {
2486 Py_ssize_t i;
2487 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2488 if (i == -1 && PyErr_Occurred()) {
2489 return nullptr;
2490 }
2491 if (i < 0) {
2492 i += self->prop->len;
2493 }
2494 return BPy_IDArray_GetItem(self, i);
2495 }
2496 if (PySlice_Check(item)) {
2497 Py_ssize_t start, stop, step, slicelength;
2498
2499 if (PySlice_GetIndicesEx(item, self->prop->len, &start, &stop, &step, &slicelength) < 0) {
2500 return nullptr;
2501 }
2502
2503 if (slicelength <= 0) {
2504 return PyTuple_New(0);
2505 }
2506 if (step == 1) {
2507 return BPy_IDArray_slice(self, start, stop);
2508 }
2509
2510 PyErr_SetString(PyExc_TypeError, "slice steps not supported with vectors");
2511 return nullptr;
2512 }
2513
2514 PyErr_Format(PyExc_TypeError,
2515 "vector indices must be integers, not %.200s",
2516 __func__,
2517 Py_TYPE(item)->tp_name);
2518 return nullptr;
2519}
2520
2521static int BPy_IDArray_ass_subscript(BPy_IDArray *self, PyObject *item, PyObject *value)
2522{
2523 if (PyIndex_Check(item)) {
2524 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2525 if (i == -1 && PyErr_Occurred()) {
2526 return -1;
2527 }
2528 if (i < 0) {
2529 i += self->prop->len;
2530 }
2531 return BPy_IDArray_SetItem(self, i, value);
2532 }
2533 if (PySlice_Check(item)) {
2534 Py_ssize_t start, stop, step, slicelength;
2535
2536 if (PySlice_GetIndicesEx(item, self->prop->len, &start, &stop, &step, &slicelength) < 0) {
2537 return -1;
2538 }
2539
2540 if (step == 1) {
2541 return BPy_IDArray_ass_slice(self, start, stop, value);
2542 }
2543
2544 PyErr_SetString(PyExc_TypeError, "slice steps not supported with vectors");
2545 return -1;
2546 }
2547
2548 PyErr_Format(
2549 PyExc_TypeError, "vector indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
2550 return -1;
2551}
2552
2553static PyMappingMethods BPy_IDArray_AsMapping = {
2554 /*mp_length*/ reinterpret_cast<lenfunc>(BPy_IDArray_Len),
2555 /*mp_subscript*/ reinterpret_cast<binaryfunc>(BPy_IDArray_subscript),
2556 /*mp_ass_subscript*/ reinterpret_cast<objobjargproc>(BPy_IDArray_ass_subscript),
2557};
2558
2559static int itemsize_by_idarray_type(int array_type)
2560{
2561 if (array_type == IDP_INT) {
2562 return sizeof(int);
2563 }
2564 if (array_type == IDP_FLOAT) {
2565 return sizeof(float);
2566 }
2567 if (array_type == IDP_DOUBLE) {
2568 return sizeof(double);
2569 }
2570 if (array_type == IDP_BOOLEAN) {
2571 return sizeof(bool);
2572 }
2573 return -1; /* should never happen */
2574}
2575
2576static int BPy_IDArray_getbuffer(BPy_IDArray *self, Py_buffer *view, int flags)
2577{
2578 IDProperty *prop = self->prop;
2579 const int itemsize = itemsize_by_idarray_type(prop->subtype);
2580 const int length = itemsize * prop->len;
2581
2582 if (PyBuffer_FillInfo(view,
2583 reinterpret_cast<PyObject *>(self),
2584 IDP_array_voidp_get(prop),
2585 length,
2586 false,
2587 flags) == -1)
2588 {
2589 return -1;
2590 }
2591
2592 view->itemsize = itemsize;
2593 view->format = const_cast<char *>(idp_format_from_array_type(prop->subtype));
2594
2595 Py_ssize_t *shape = MEM_new_uninitialized<Py_ssize_t>(__func__);
2596 shape[0] = prop->len;
2597 view->shape = shape;
2598
2599 return 0;
2600}
2601
2602static void BPy_IDArray_releasebuffer(BPy_IDArray * /*self*/, Py_buffer *view)
2603{
2604 MEM_delete(view->shape);
2605}
2606
2607static PyBufferProcs BPy_IDArray_Buffer = {
2608 /*bf_getbuffer*/ reinterpret_cast<getbufferproc>(BPy_IDArray_getbuffer),
2609 /*bf_releasebuffer*/ reinterpret_cast<releasebufferproc>(BPy_IDArray_releasebuffer),
2610};
2611
2613
2614/* -------------------------------------------------------------------- */
2617
2619 /* Wrap. */
2620 BPy_IDArray_Type_doc,
2621 "An array of values with a fixed type, "
2622 "supporting indexing and slicing.");
2623
2624PyTypeObject BPy_IDArray_Type = {
2625 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
2626 /* For printing, in format `<module>.<name>`. */
2627 /*tp_name*/ "IDPropertyArray",
2628 /*tp_basicsize*/ sizeof(BPy_IDArray),
2629 /*tp_itemsize*/ 0,
2630 /*tp_dealloc*/ nullptr,
2631 /*tp_vectorcall_offset*/ 0,
2632 /*tp_getattr*/ nullptr,
2633 /*tp_setattr*/ nullptr,
2634 /*tp_as_async*/ nullptr,
2635 /*tp_repr*/ reinterpret_cast<reprfunc>(BPy_IDArray_repr),
2636 /*tp_as_number*/ nullptr,
2637 /*tp_as_sequence*/ &BPy_IDArray_Seq,
2638 /*tp_as_mapping*/ &BPy_IDArray_AsMapping,
2639 /*tp_hash*/ nullptr,
2640 /*tp_call*/ nullptr,
2641 /*tp_str*/ nullptr,
2642 /*tp_getattro*/ nullptr,
2643 /*tp_setattro*/ nullptr,
2644 /*tp_as_buffer*/ &BPy_IDArray_Buffer,
2645 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
2646 /*tp_doc*/ BPy_IDArray_Type_doc,
2647 /*tp_traverse*/ nullptr,
2648 /*tp_clear*/ nullptr,
2649 /*tp_richcompare*/ nullptr,
2650 /*tp_weaklistoffset*/ 0,
2651 /*tp_iter*/ nullptr,
2652 /*tp_iternext*/ nullptr,
2653 /*tp_methods*/ BPy_IDArray_methods,
2654 /*tp_members*/ nullptr,
2655 /*tp_getset*/ BPy_IDArray_getseters,
2656 /*tp_base*/ nullptr,
2657 /*tp_dict*/ nullptr,
2658 /*tp_descr_get*/ nullptr,
2659 /*tp_descr_set*/ nullptr,
2660 /*tp_dictoffset*/ 0,
2661 /*tp_init*/ nullptr,
2662 /*tp_alloc*/ nullptr,
2663 /*tp_new*/ nullptr,
2664 /*tp_free*/ nullptr,
2665 /*tp_is_gc*/ nullptr,
2666 /*tp_bases*/ nullptr,
2667 /*tp_mro*/ nullptr,
2668 /*tp_cache*/ nullptr,
2669 /*tp_subclasses*/ nullptr,
2670 /*tp_weaklist*/ nullptr,
2671 /*tp_del*/ nullptr,
2672 /*tp_version_tag*/ 0,
2673 /*tp_finalize*/ nullptr,
2674 /*tp_vectorcall*/ nullptr,
2675};
2676
2678
2679/* -------------------------------------------------------------------- */
2682
2684{
2687
2688 PyType_Ready(&BPy_IDGroup_Type);
2689 PyType_Ready(&BPy_IDArray_Type);
2690
2691 PyType_Ready(&BPy_IDGroup_IterKeys_Type);
2692 PyType_Ready(&BPy_IDGroup_IterValues_Type);
2693 PyType_Ready(&BPy_IDGroup_IterItems_Type);
2694
2695 PyType_Ready(&BPy_IDGroup_ViewKeys_Type);
2696 PyType_Ready(&BPy_IDGroup_ViewValues_Type);
2697 PyType_Ready(&BPy_IDGroup_ViewItems_Type);
2698}
2699
2708{
2709 BLI_assert(group ? group->prop->type == IDP_GROUP : true);
2710 BPy_IDGroup_View *iter = PyObject_GC_New(BPy_IDGroup_View, type);
2711 iter->reversed = false;
2712 iter->group = group;
2713 if (group != nullptr) {
2714 Py_INCREF(group);
2715 BLI_assert(!PyObject_GC_IsTracked((PyObject *)iter));
2716 PyObject_GC_Track(iter);
2717 }
2718 return iter;
2719}
2720
2722{
2723 return reinterpret_cast<PyObject *>(
2725}
2726
2728{
2729 return reinterpret_cast<PyObject *>(
2731}
2732
2734{
2735 return reinterpret_cast<PyObject *>(
2737}
2738
2740
2741/* -------------------------------------------------------------------- */
2744
2745static PyModuleDef IDProp_types_module_def = {
2746 /*m_base*/ PyModuleDef_HEAD_INIT,
2747 /*m_name*/ "idprop.types",
2748 /*m_doc*/ nullptr,
2749 /*m_size*/ 0,
2750 /*m_methods*/ nullptr,
2751 /*m_slots*/ nullptr,
2752 /*m_traverse*/ nullptr,
2753 /*m_clear*/ nullptr,
2754 /*m_free*/ nullptr,
2755};
2756
2757static PyObject *BPyInit_idprop_types()
2758{
2759 PyObject *submodule;
2760
2761 submodule = PyModule_Create(&IDProp_types_module_def);
2762
2765
2766 /* `bmesh_py_types.cc` */
2767 PyModule_AddType(submodule, &BPy_IDGroup_Type);
2768
2769 PyModule_AddType(submodule, &BPy_IDGroup_ViewKeys_Type);
2770 PyModule_AddType(submodule, &BPy_IDGroup_ViewValues_Type);
2771 PyModule_AddType(submodule, &BPy_IDGroup_ViewItems_Type);
2772
2773 PyModule_AddType(submodule, &BPy_IDGroup_IterKeys_Type);
2774 PyModule_AddType(submodule, &BPy_IDGroup_IterValues_Type);
2775 PyModule_AddType(submodule, &BPy_IDGroup_IterItems_Type);
2776
2777 PyModule_AddType(submodule, &BPy_IDArray_Type);
2778
2779 PyModule_AddType(submodule, &BPy_IDPropertyUIManager_Type);
2780
2781 return submodule;
2782}
2783
2785
2786/* -------------------------------------------------------------------- */
2789
2790static PyMethodDef IDProp_methods[] = {
2791 {nullptr, nullptr, 0, nullptr},
2792};
2793
2795 /* Wrap. */
2796 IDProp_module_doc,
2797 "This module provides access to ID property types, used for\n"
2798 "custom properties on data-blocks, accessed via ``[\"key\"]`` syntax.\n"
2799 "\n"
2800 "- See :ref:`info_quickstart-custom_properties` for example usage.\n"
2801 "- See :ref:`bpy_types-custom_properties` for types that support custom properties.\n");
2802static PyModuleDef IDProp_module_def = {
2803 /*m_base*/ PyModuleDef_HEAD_INIT,
2804 /*m_name*/ "idprop",
2805 /*m_doc*/ IDProp_module_doc,
2806 /*m_size*/ 0,
2807 /*m_methods*/ IDProp_methods,
2808 /*m_slots*/ nullptr,
2809 /*m_traverse*/ nullptr,
2810 /*m_clear*/ nullptr,
2811 /*m_free*/ nullptr,
2812};
2813
2815{
2816 PyObject *mod;
2817 PyObject *submodule;
2818 PyObject *sys_modules = PyImport_GetModuleDict();
2819
2820 mod = PyModule_Create(&IDProp_module_def);
2821
2822 /* idprop.types */
2823 PyModule_AddObject(mod, "types", (submodule = BPyInit_idprop_types()));
2824 PyC_Module_AddToSysModules(sys_modules, submodule);
2825
2826 return mod;
2827}
2828
2830
2831} // namespace blender
#define IDP_float_get(prop)
#define IDP_array_voidp_get(prop)
#define IDP_bool_set(prop, value)
#define IDP_int_get(prop)
#define IDP_array_double_get(prop)
#define IDP_int_set(prop, value)
#define IDP_string_get(prop)
#define IDP_double_get(prop)
#define IDP_array_bool_get(prop)
#define IDP_array_int_get(prop)
#define IDP_property_array_get(prop)
#define IDP_float_set(prop, value)
#define IDP_double_set(prop, value)
#define IDP_array_float_get(prop)
#define IDP_bool_get(prop)
ID and Library types, which are fundamental for SDNA.
#define MAX_IDPROP_NAME
Definition DNA_ID.h:174
Read Blender memory allocation and freeing.
#define CLAMP(a, b, c)
#define BLI_assert_unreachable()
#define ELEM(...)
#define STREQ(a, b)
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
#define SHARED_MEMBER_SET(member, value)
#define BPy_IDGroup_Check(v)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
std::unique_ptr< IDProperty, IDPropertyDeleter > create_bool(StringRef prop_name, bool value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_BOOLEAN, set its name and value.
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
BLI_INLINE ColorTheme4f to_float(const ColorTheme4b &theme4b)
Definition BLI_color.hh:64
BLI_INLINE Texture * wrap(MTLTexture *texture)
BMesh Iterators.
static PyObject * BPy_IDArray_GetItem(BPy_IDArray *self, Py_ssize_t index)
bool BPy_IDProperty_Map_ValidateAndCreate(PyObject *key, IDProperty *group, PyObject *ob)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
PyObject * BPyInit_idprop()
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool pyrna_id_CheckPyObject(PyObject *obj)
Definition bpy_rna.cc:8999
void IDP_FreeFromGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:812
static const char * idp_try_read_name(PyObject *name_obj)
__int64 int64_t
Definition BKE_text.h:90
static PyObject * BPy_IDGroup_IterItems_CreatePyObject(BPy_IDProperty *group, const bool reversed)
static PyMappingMethods BPy_IDArray_AsMapping
static PyObject * idprop_py_from_idp_group(ID *id, IDProperty *prop, IDProperty *parent)
static PyObject * BPy_Group_ViewValues_iter(BPy_IDGroup_View *self)
static char idp_sequence_type(PyObject *seq_fast)
void IDProp_Init_Types()
static PyMethodDef IDProp_methods[]
static void BPy_IDGroup_View_dealloc(BPy_IDGroup_View *self)
static PyObject * BPy_Group_IterValues_next(BPy_IDGroup_Iter *self)
static PyObject * BPy_Group_ViewKeys_iter(BPy_IDGroup_View *self)
static int BPy_IDGroup_Iter_is_gc(BPy_IDGroup_Iter *self)
void IDPropertyUIData_Init_Types()
PyObject * pyrna_id_CreatePyObject(ID *id)
Definition bpy_rna.cc:8978
PyTypeObject BPy_IDGroup_IterValues_Type
static PyGetSetDef BPy_IDArray_getseters[]
static PyObject * BPy_IDGroup_get(BPy_IDProperty *self, PyObject *args)
char PyC_StructFmt_type_from_str(const char *typestr)
static PyMethodDef BPy_IDGroup_View_methods[]
static PyObject * BPy_IDGroup_ViewKeys_CreatePyObject(BPy_IDProperty *group)
static PyObject * BPy_IDArray_get_typecode(BPy_IDArray *self, void *)
static int BPy_IDArray_SetItem(BPy_IDArray *self, Py_ssize_t index, PyObject *value)
static PyObject * BPy_IDGroup_repr(BPy_IDProperty *self)
static PyObject * idprop_py_from_idp_idparray(ID *id, IDProperty *prop)
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:818
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyObject * BPy_Group_ViewItems_iter(BPy_IDGroup_View *self)
static PyObject * BPy_IDGroup_pop(BPy_IDProperty *self, PyObject *args)
static int BPy_Group_ViewValues_Contains(BPy_IDGroup_View *self, PyObject *value)
static Py_ssize_t BPy_Group_View_len(BPy_IDGroup_View *self)
static PyBufferProcs BPy_IDArray_Buffer
int PyC_AsArray(void *array, const size_t array_item_size, PyObject *value, const Py_ssize_t length, const PyTypeObject *type, const char *error_prefix)
static int BPy_Group_ViewItems_Contains(BPy_IDGroup_View *self, PyObject *value)
static PyMappingMethods BPy_IDGroup_Mapping
static PyObject * BPy_IDGroup_iter(BPy_IDProperty *self)
PyObject * BPy_IDGroup_MapDataToPy(IDProperty *prop)
PyTypeObject BPy_IDArray_Type
static PyMethodDef BPy_IDGroup_methods[]
PyObject * BPy_Wrap_GetItems_View_WithID(ID *id, IDProperty *prop)
PyTypeObject BPy_IDGroup_ViewValues_Type
static PySequenceMethods BPy_IDGroup_ViewValues_as_sequence
static PyObject * BPy_IDArray_subscript(BPy_IDArray *self, PyObject *item)
static PyObject * BPy_IDGroup_Map_GetItem(BPy_IDProperty *self, PyObject *item)
static int BPy_IDArray_ass_subscript(BPy_IDArray *self, PyObject *item, PyObject *value)
static int BPy_Group_ViewKeys_Contains(BPy_IDGroup_View *self, PyObject *value)
static PyObject * BPy_IDGroup_ViewItems_CreatePyObject(BPy_IDProperty *group)
eIDPropertyUIDataType IDP_ui_data_type(const IDProperty *prop)
Definition idprop.cc:1964
static PyMethodDef BPy_IDArray_methods[]
static PyObject * IDGroup_Iter_New_WithType(BPy_IDProperty *group, const bool reversed, PyTypeObject *type)
void IDP_AssignID(IDProperty *prop, ID *id, int flag)
Definition idprop.cc:616
void MEM_delete_void(void *vmemh)
PyObject * BPy_Wrap_GetKeys_View_WithID(ID *id, IDProperty *prop)
static PyObject * BPy_IDArray_repr(BPy_IDArray *self)
void IDP_MergeGroup(IDProperty *dest, const IDProperty *src, bool do_overwrite) ATTR_NONNULL()
Definition idprop.cc:783
static PyObject * BPy_IDGroup_GetName(BPy_IDProperty *self, void *)
PyTypeObject BPy_IDGroup_IterItems_Type
PyObject * BPy_Wrap_GetItems(ID *id, IDProperty *prop)
static IDProperty * idp_from_PyMapping(IDProperty *, const char *name, PyObject *ob, const bool, const bool)
static int BPy_IDGroup_Iter_clear(BPy_IDGroup_Iter *self)
static PyObject * BPy_IDArray_to_list(BPy_IDArray *self)
static PyObject * idprop_py_from_idp_id(IDProperty *prop)
void IDP_ReplaceInGroup_ex(IDProperty *group, IDProperty *prop, IDProperty *prop_exist, int flag)
Definition idprop.cc:716
static const char * idp_format_from_array_type(int type)
static PyObject * idprop_py_from_idp_string(const IDProperty *prop)
static IDProperty * idp_from_PySequence(IDProperty *prop_exist, const char *name, PyObject *ob, const bool do_conversion, const bool can_create)
static void BPy_IDArray_releasebuffer(BPy_IDArray *, Py_buffer *view)
PyObject * BPy_Wrap_GetKeys(IDProperty *prop)
static PyTypeObject * idp_array_py_type(BPy_IDArray *self, size_t *elem_size)
static int itemsize_by_idarray_type(int array_type)
static int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void *)
static Py_ssize_t BPy_IDGroup_Map_Len(BPy_IDProperty *self)
void IDP_ClearProperty(IDProperty *prop)
Definition idprop.cc:1408
static PyObject * idprop_py_from_idp_int(const IDProperty *prop)
IDPropertyUIData * IDP_TryConvertUIData(IDPropertyUIData *src, eIDPropertyUIDataType src_type, eIDPropertyUIDataType dst_type)
Definition idprop.cc:2052
static PyObject * BPy_IDGroup_to_dict(BPy_IDProperty *self)
static PyObject * BPy_IDGroup_View_reversed(BPy_IDGroup_View *self, PyObject *)
static PyObject * idprop_py_from_idp_double(const IDProperty *prop)
static IDProperty * idp_from_PyLong(IDProperty *prop_exist, const char *name, PyObject *ob, const bool do_conversion, const bool can_create)
@ IDP_FLAG_OVERRIDABLE_LIBRARY
@ IDP_FLAG_STATIC_TYPE
static PyObject * idprop_py_from_idp_bool(const IDProperty *prop)
PyTypeObject BPy_IDGroup_IterKeys_Type
static IDProperty * idp_from_PyFloat(IDProperty *prop_exist, const char *name, PyObject *ob, const bool do_conversion, const bool can_create)
static int BPy_IDGroup_View_traverse(BPy_IDGroup_View *self, visitproc visit, void *arg)
static IDProperty * idp_from_PyUnicode(IDProperty *prop_exist, const char *name, PyObject *ob, const bool, const bool can_create)
PyTypeObject BPy_IDGroup_Type
static PyModuleDef IDProp_types_module_def
static PyModuleDef IDProp_module_def
bool pyrna_id_FromPyObject(PyObject *obj, ID **id)
Definition bpy_rna.cc:8988
static int BPy_IDGroup_Map_SetItem(BPy_IDProperty *self, PyObject *key, PyObject *val)
static int BPy_IDGroup_View_clear(BPy_IDGroup_View *self)
static int idp_array_type_from_formatstr_and_size(const char *typestr, Py_ssize_t itemsize)
static PyObject * BPy_IDGroup_ViewValues_CreatePyObject(BPy_IDProperty *group)
static BPy_IDGroup_View * IDGroup_View_New_WithType(BPy_IDProperty *group, PyTypeObject *type)
PyObject * BPy_IDGroup_WrapData(ID *id, IDProperty *prop, IDProperty *parent)
static PyObject * BPy_IDGroup_items(BPy_IDProperty *self)
static Py_ssize_t BPy_IDArray_Len(BPy_IDArray *self)
static PyObject * BPy_IDGroup_keys(BPy_IDProperty *self)
static bool BPy_Group_Iter_same_size_or_raise_error(BPy_IDGroup_Iter *self)
@ IDP_STRING_SUB_UTF8
@ IDP_STRING_SUB_BYTE
void IDP_ResizeArray(IDProperty *prop, int newlen)
Definition idprop.cc:263
const char * IDP_type_str(eIDPropertyType type, short sub_type)
static PySequenceMethods BPy_IDGroup_Seq
static PyObject * BPyInit_idprop_types()
const char * PyC_UnicodeAsBytes(PyObject *py_str, PyObject **r_coerce)
static PyObject * BPy_IDGroup_clear(BPy_IDProperty *self)
int PyC_Long_AsBool(PyObject *value)
void IDP_AppendArray(IDProperty *prop, IDProperty *item)
Definition idprop.cc:172
PyObject * BPy_Wrap_GetValues_View_WithID(ID *id, IDProperty *prop)
static int BPy_IDGroup_View_is_gc(BPy_IDGroup_View *self)
static PyObject * idprop_py_from_idp_float(const IDProperty *prop)
static PyObject * BPy_IDGroup_Iter_repr(BPy_IDGroup_Iter *self)
static IDProperty * idp_from_PyBytes(IDProperty *prop_exist, const char *name, PyObject *ob, const bool, const bool can_create)
static void BPy_IDGroup_Iter_dealloc(BPy_IDGroup_Iter *self)
static int BPy_IDGroup_Contains(BPy_IDProperty *self, PyObject *value)
static void BPy_IDGroup_CorrectListLen(IDProperty *prop, PyObject *seq, int len, const char *func)
PyTypeObject BPy_IDPropertyUIManager_Type
static IDProperty * idp_from_PySequence_Fast(IDProperty *prop_exist, const char *name, PyObject *ob, const bool do_conversion, const bool can_create)
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
static IDProperty * idp_from_PySequence_Buffer(IDProperty *prop_exist, const char *name, const Py_buffer &buffer, const int idp_type, const bool, const bool can_create)
iter begin(iter)
bool PyC_StructFmt_type_is_float_any(char format)
int PyC_Module_AddToSysModules(PyObject *sys_modules, PyObject *module)
static PyObject * BPy_IDGroup_IterKeys_CreatePyObject(BPy_IDProperty *group, const bool reversed)
static int BPy_IDArray_ass_slice(BPy_IDArray *self, int begin, int end, PyObject *seq)
static PyObject * BPy_IDGroup_update(BPy_IDProperty *self, PyObject *value)
static void IDGroup_Iter_init_type()
static PySequenceMethods BPy_IDGroup_ViewKeys_as_sequence
IDProperty * IDP_New(char type, const IDPropertyTemplate *val, StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:1132
static PyObject * BPy_Group_IterItems_next(BPy_IDGroup_Iter *self)
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:775
PyTypeObject BPy_IDGroup_ViewItems_Type
static int BPy_IDGroup_Iter_traverse(BPy_IDGroup_Iter *self, visitproc visit, void *arg)
static PyObject * BPy_IDGroup_IterValues_CreatePyObject(BPy_IDProperty *group, const bool reversed)
static void IDGroup_View_init_type()
static IDProperty * idp_from_DatablockPointer(IDProperty *prop_exist, const char *name, PyObject *ob, const bool, const bool can_create)
IDProperty * BPy_IDProperty_FromPyObject(IDProperty *prop_exist, const char *name, PyObject *ob, const bool do_conversion, const bool can_create)
PyObject * PyC_UnicodeFromBytesAndSize(const char *str, Py_ssize_t size)
bool PyC_StructFmt_type_is_int_any(char format)
static PyObject * BPy_Group_IterKeys_next(BPy_IDGroup_Iter *self)
static PySequenceMethods BPy_IDGroup_ViewItems_as_sequence
static PyGetSetDef BPy_IDGroup_getseters[]
PyObject * BPy_Wrap_GetValues(ID *id, IDProperty *prop)
PyTypeObject BPy_IDGroup_ViewKeys_Type
eIDPropertyUIDataType
static int BPy_IDArray_getbuffer(BPy_IDArray *self, Py_buffer *view, int flags)
static PyObject * idprop_py_from_idp_array(ID *id, IDProperty *prop)
static PyObject * BPy_IDGroup_View_repr(BPy_IDGroup_View *self)
static Py_hash_t BPy_IDGroup_hash(BPy_IDProperty *self)
IDProperty * IDP_NewIDPArray(StringRef name) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:104
static PyObject * BPy_IDArray_slice(BPy_IDArray *self, int begin, int end)
signed char int8_t
Definition BKE_text.h:76
void IDP_AssignStringMaxSize(IDProperty *prop, const char *st, size_t st_maxncpy) ATTR_NONNULL()
Definition idprop.cc:470
static PyObject * BPy_IDGroup_values(BPy_IDProperty *self)
static PySequenceMethods BPy_IDArray_Seq
int BPy_Wrap_SetMapItem(IDProperty *prop, PyObject *key, PyObject *val)
const char * PyC_UnicodeAsBytesAndSize(PyObject *py_str, Py_ssize_t *r_size, PyObject **r_coerce)
static IDProperty * idp_from_PyBool(IDProperty *prop_exist, const char *name, PyObject *ob, const bool do_conversion, const bool can_create)
header-only compatibility defines.
#define Py_HashPointer
header-only utilities
#define PyTuple_SET_ITEMS(op_arg,...)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct IDProperty * prop
PyObject_HEAD BPy_IDProperty * group
struct IDProperty * cur
PyObject_HEAD BPy_IDProperty * group
struct IDProperty * parent
struct IDProperty * prop
PyObject_HEAD struct ID * owner_id
ListBaseT< struct IDProperty > group
Definition DNA_ID.h:133
eIDPropertyFlag flag
Definition DNA_ID.h:151
IDPropertyUIData * ui_data
Definition DNA_ID.h:171
eIDPropertyType type
Definition DNA_ID.h:145
struct IDProperty * prev
Definition DNA_ID.h:144
struct IDProperty * next
Definition DNA_ID.h:144
IDPropertyData data
Definition DNA_ID.h:157
char name[258]
Definition DNA_ID.h:433
short flag
Definition DNA_ID.h:439
struct blender::IDPropertyTemplate::@100215251242210352227251277016155305041022127377 array
struct blender::IDPropertyTemplate::@116303142222164257205355242364302075230051143265 string