Blender V5.3
bpy_rna_array.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
10
11#include <Python.h>
12
13#include "CLG_log.h"
14
15#include "BLI_utildefines.hh"
16
17#include "RNA_types.hh"
18
19#include "bpy_rna.hh"
20
21#include "MEM_guardedalloc.h"
22
23#include "RNA_access.hh"
24
25#include "BPY_extern_clog.hh"
26
28
29#define USE_MATHUTILS
30
31#ifdef USE_MATHUTILS
32# include "../mathutils/mathutils.hh" /* so we can have mathutils callbacks */
33#endif
34
35#define MAX_ARRAY_DIMENSION 10
36
37namespace blender {
38
39struct ItemConvertArgData;
40
41using ItemConvertFunc = void (*)(const ItemConvertArgData *arg, PyObject *py_data, char *data);
42using ItemTypeCheckFunc = bool (*)(PyObject *py_data);
43using RNA_SetArrayFunc = void (*)(PointerRNA *ptr, PropertyRNA *prop, const char *data);
44using RNA_SetIndexFunc = void (*)(PointerRNA *ptr, PropertyRNA *prop, int index, void *data_item);
45
47 union {
48 struct {
49 int range[2];
50 } int_data;
51 struct {
52 float range[2];
53 } float_data;
54 };
55};
56
64
65static int validate_array_type(PyObject *seq,
66 int dim,
67 int totdim,
68 int dimsize[],
69 bool is_dynamic,
70 ItemTypeCheckFunc check_item_type,
71 const char *item_type_str,
72 const char *error_prefix);
73
74static int validate_array_type_fast(PyObject *seq_fast,
75 int dim,
76 int totdim,
77 int dimsize[],
78 const bool is_dynamic,
79 ItemTypeCheckFunc check_item_type,
80 const char *item_type_str,
81 const char *error_prefix)
82{
83 /* Ensure this is the result of `PySequence_Fast`. */
84 BLI_assert(PyList_Check(seq_fast) || PyTuple_Check(seq_fast));
85
86 const int seq_size = PySequence_Fast_GET_SIZE(seq_fast);
87 PyObject **seq_items = PySequence_Fast_ITEMS(seq_fast);
88
89 /* not the last dimension */
90 if (dim + 1 < totdim) {
91 /* check that a sequence contains dimsize[dim] items */
92 for (int i = 0; i < seq_size; i++) {
93 PyObject *item = seq_items[i];
94 Py_ssize_t item_seq_size;
95
96 if ((item_seq_size = PySequence_Size(item)) == -1) {
97 PyErr_Format(PyExc_TypeError,
98 "%s expected a sequence of %s, not %s",
99 error_prefix,
100 item_type_str,
101 Py_TYPE(item)->tp_name);
102 return -1;
103 }
104 /* arr[3][4][5]
105 * dimsize[1] = 4
106 * dimsize[2] = 5
107 *
108 * dim = 0 */
109 if (item_seq_size != dimsize[dim + 1]) {
110 PyErr_Format(PyExc_ValueError,
111 "%s sequences of dimension %d should contain %d items, not %d",
112 error_prefix,
113 dim + 1,
114 dimsize[dim + 1],
115 item_seq_size);
116 return -1;
117 }
118 if (validate_array_type(item,
119 dim + 1,
120 totdim,
121 dimsize,
122 is_dynamic,
123 check_item_type,
124 item_type_str,
125 error_prefix) == -1)
126 {
127 return -1;
128 }
129 }
130 }
131 else {
132 /* check that items are of correct type */
133 if ((seq_size != dimsize[dim]) && (is_dynamic == false)) {
134 PyErr_Format(PyExc_ValueError,
135 "%s sequences of dimension %d should contain %d items, not %d",
136 error_prefix,
137 dim,
138 dimsize[dim],
139 seq_size);
140 return -1;
141 }
142
143 for (int i = 0; i < seq_size; i++) {
144 PyObject *item = seq_items[i];
145
146 if (!check_item_type(item)) {
147 PyErr_Format(PyExc_TypeError,
148 "%s expected sequence items of type %s, not %s",
149 error_prefix,
150 item_type_str,
151 Py_TYPE(item)->tp_name);
152 return -1;
153 }
154 }
155 }
156
157 return 0; /* ok */
158}
159
160/*
161 * arr[3][4][5]
162 * 0 1 2 <- dimension index
163 */
164
165/*
166 * arr[2] = x
167 *
168 * py_to_array_index(arraydim=0, arrayoffset=0, index=2)
169 * validate_array(lvalue_dim=0)
170 * ... make real index ...
171 */
172
173/* arr[3] = x, self->arraydim is 0, lvalue_dim is 1 */
174/* Ensures that a python sequence has expected number of
175 * items/sub-items and items are of desired type. */
176static int validate_array_type(PyObject *seq,
177 int dim,
178 int totdim,
179 int dimsize[],
180 const bool is_dynamic,
181 ItemTypeCheckFunc check_item_type,
182 const char *item_type_str,
183 const char *error_prefix)
184{
185 PyObject *seq_fast = PySequence_Fast(seq, error_prefix);
186 if (seq_fast == nullptr) {
187 return -1;
188 }
190 seq_fast, dim, totdim, dimsize, is_dynamic, check_item_type, item_type_str, error_prefix);
191 Py_DECREF(seq_fast);
192 return result;
193}
194
195/* Returns the number of items in a single- or multi-dimensional sequence. */
196static int count_items(PyObject *seq, int dim)
197{
198 int totitem = 0;
199
200 if (dim > 1) {
201 PyObject *seq_fast = PySequence_Fast(seq, "");
202 if (seq_fast == nullptr) {
203 return -1;
204 }
205
206 const Py_ssize_t seq_size = PySequence_Fast_GET_SIZE(seq_fast);
207 PyObject **seq_items = PySequence_Fast_ITEMS(seq_fast);
208 for (Py_ssize_t i = 0; i < seq_size; i++) {
209 PyObject *item = seq_items[i];
210 const int tot = count_items(item, dim - 1);
211 if (tot != -1) {
212 totitem += tot;
213 }
214 else {
215 totitem = -1;
216 break;
217 }
218 }
219 Py_DECREF(seq_fast);
220 }
221 else {
222 totitem = PySequence_Size(seq);
223 }
224
225 return totitem;
226}
227
228/* Modifies property array length if needed and PROP_DYNAMIC flag is set. */
229static int validate_array_length(PyObject *rvalue,
230 PointerRNA *ptr,
231 PropertyRNA *prop,
232 const bool prop_is_param_dyn_alloc,
233 int lvalue_dim,
234 int *r_totitem,
235 const char *error_prefix)
236{
237 int dimsize[MAX_ARRAY_DIMENSION];
238 int tot, totdim, len;
239
240 totdim = RNA_property_array_dimension(ptr, prop, dimsize);
241 tot = count_items(rvalue, totdim - lvalue_dim);
242
243 if (tot == -1) {
244 PyErr_Format(PyExc_ValueError,
245 "%s %.200s.%.200s, error validating the sequence length",
246 error_prefix,
249 return -1;
250 }
251 if ((RNA_property_flag(prop) & PROP_DYNAMIC) && lvalue_dim == 0) {
252 const int tot_expected = RNA_property_array_length(ptr, prop);
253 if (tot_expected != tot) {
254 *r_totitem = tot;
255 if (!prop_is_param_dyn_alloc) {
256 PyErr_Format(PyExc_ValueError,
257 "%s %s.%s: array length cannot be changed to %d (expected %d)",
258 error_prefix,
261 tot,
262 tot_expected);
263 return -1;
264 }
265 return 0;
266 }
267
268 len = tot;
269 }
270 else {
271 /* length is a constraint */
272 if (!lvalue_dim) {
273 len = RNA_property_array_length(ptr, prop);
274 }
275 /* array item assignment */
276 else {
277 int i;
278
279 len = 1;
280
281 /* arr[3][4][5]
282 *
283 * arr[2] = x
284 * dimsize = {4, 5}
285 * dimsize[1] = 4
286 * dimsize[2] = 5
287 * lvalue_dim = 0, totdim = 3
288 *
289 * arr[2][3] = x
290 * lvalue_dim = 1
291 *
292 * arr[2][3][4] = x
293 * lvalue_dim = 2 */
294 for (i = lvalue_dim; i < totdim; i++) {
295 len *= dimsize[i];
296 }
297 }
298
299 if (tot != len) {
300 PyErr_Format(PyExc_ValueError,
301 "%s %.200s.%.200s, sequence must have %d items total, not %d",
302 error_prefix,
305 len,
306 tot);
307 return -1;
308 }
309 }
310
311 *r_totitem = len;
312
313 return 0;
314}
315
316static int validate_array(PyObject *rvalue,
317 PointerRNA *ptr,
318 PropertyRNA *prop,
319 const bool prop_is_param_dyn_alloc,
320 int lvalue_dim,
321 ItemTypeCheckFunc check_item_type,
322 const char *item_type_str,
323 int *r_totitem,
324 const char *error_prefix)
325{
326 int dimsize[MAX_ARRAY_DIMENSION];
327 const int totdim = RNA_property_array_dimension(ptr, prop, dimsize);
328
329 /* validate type first because length validation may modify property array length */
330
331#ifdef USE_MATHUTILS
332 if (lvalue_dim == 0) { /* only valid for first level array */
333 if (MatrixObject_Check(rvalue)) {
334 MatrixObject *pymat = reinterpret_cast<MatrixObject *>(rvalue);
335
336 if (BaseMath_ReadCallback(pymat) == -1) {
337 return -1;
338 }
339
340 if (RNA_property_type(prop) != PROP_FLOAT) {
341 PyErr_Format(PyExc_ValueError,
342 "%s %.200s.%.200s, matrix assign to non float array",
343 error_prefix,
346 return -1;
347 }
348 if (totdim != 2) {
349 PyErr_Format(PyExc_ValueError,
350 "%s %.200s.%.200s, matrix assign array with %d dimensions",
351 error_prefix,
354 totdim);
355 return -1;
356 }
357 if (pymat->col_num != dimsize[0] || pymat->row_num != dimsize[1]) {
358 PyErr_Format(PyExc_ValueError,
359 "%s %.200s.%.200s, matrix assign dimension size mismatch, "
360 "is %dx%d, expected be %dx%d",
361 error_prefix,
364 pymat->col_num,
365 pymat->row_num,
366 dimsize[0],
367 dimsize[1]);
368 return -1;
369 }
370
371 *r_totitem = dimsize[0] * dimsize[1];
372 return 0;
373 }
374 }
375#endif /* USE_MATHUTILS */
376
377 {
378 const int prop_flag = RNA_property_flag(prop);
379 if (validate_array_type(rvalue,
380 lvalue_dim,
381 totdim,
382 dimsize,
383 (prop_flag & PROP_DYNAMIC) != 0,
384 check_item_type,
385 item_type_str,
386 error_prefix) == -1)
387 {
388 return -1;
389 }
390
392 rvalue, ptr, prop, prop_is_param_dyn_alloc, lvalue_dim, r_totitem, error_prefix);
393 }
394}
395
396static char *copy_value_single(PyObject *item,
397 PointerRNA *ptr,
398 PropertyRNA *prop,
399 char *data,
400 uint item_size,
401 int *index,
402 const ItemConvert_FuncArg *convert_item,
403 RNA_SetIndexFunc rna_set_index)
404{
405 if (!data) {
406 union {
407 float fl;
408 int i;
409 } value_buf;
410 char *value = static_cast<char *>(static_cast<void *>(&value_buf));
411
412 convert_item->func(&convert_item->arg, item, value);
413 rna_set_index(ptr, prop, *index, value);
414 (*index) += 1;
415 }
416 else {
417 convert_item->func(&convert_item->arg, item, data);
418 data += item_size;
419 }
420
421 return data;
422}
423
427static char *copy_values_fast(PyObject *seq,
428 PointerRNA *ptr,
429 PropertyRNA *prop,
430 int dim,
431 int totdim,
432 char *data,
433 uint item_size,
434 int *index,
435 const ItemConvert_FuncArg *convert_item,
436 RNA_SetIndexFunc rna_set_index)
437{
438 PyObject *seq_fast = PySequence_Fast(seq, "");
439 if (seq_fast == nullptr) {
440 return nullptr;
441 }
442
443 const Py_ssize_t seq_size = PySequence_Fast_GET_SIZE(seq_fast);
444 PyObject **seq_items = PySequence_Fast_ITEMS(seq_fast);
445
446 if (dim + 1 < totdim) {
447 for (Py_ssize_t i = 0; i < seq_size; i++) {
448 data = copy_values_fast(seq_items[i],
449 ptr,
450 prop,
451 dim + 1,
452 totdim,
453 data,
454 item_size,
455 index,
456 convert_item,
457 rna_set_index);
458 }
459 }
460 else {
461 for (Py_ssize_t i = 0; i < seq_size; i++) {
463 seq_items[i], ptr, prop, data, item_size, index, convert_item, rna_set_index);
464 }
465 }
466
467 Py_DECREF(seq_fast);
468
469 return data;
470}
471
472static char *copy_values(PyObject *seq,
473 PointerRNA *ptr,
474 PropertyRNA *prop,
475 int dim,
476 char *data,
477 uint item_size,
478 int *index,
479 const ItemConvert_FuncArg *convert_item,
480 RNA_SetIndexFunc rna_set_index)
481{
482#ifdef USE_MATHUTILS
483 if (data && dim == 0) {
484 if (MatrixObject_Check(seq)) {
485 MatrixObject *pymat = reinterpret_cast<MatrixObject *>(seq);
486 const size_t allocsize = pymat->col_num * pymat->row_num * sizeof(float);
487
488 /* read callback already done by validate */
489 /* since this is the first iteration we can assume data is allocated */
490 memcpy(data, pymat->matrix, allocsize);
491
492 /* not really needed but do for completeness */
493 data += allocsize;
494
495 return data;
496 }
497 }
498#endif /* USE_MATHUTILS */
499
500 const int totdim = RNA_property_array_dimension(ptr, prop, nullptr);
501 return copy_values_fast(
502 seq, ptr, prop, dim, totdim, data, item_size, index, convert_item, rna_set_index);
503}
504
505static int py_to_array(PyObject *seq,
506 PointerRNA *ptr,
507 PropertyRNA *prop,
508 char *param_data,
509 ItemTypeCheckFunc check_item_type,
510 const char *item_type_str,
511 int item_size,
512 const ItemConvert_FuncArg *convert_item,
513 RNA_SetArrayFunc rna_set_array,
514 const char *error_prefix)
515{
516 // int totdim, dim_size[MAX_ARRAY_DIMENSION];
517 int totitem;
518 char *data = nullptr;
519
520 // totdim = RNA_property_array_dimension(ptr, prop, dim_size); /* UNUSED */
521 const int flag = RNA_property_flag(prop);
522
523 /* Use #ParameterDynAlloc which defines its own array length. */
524 const bool prop_is_param_dyn_alloc = param_data && (flag & PROP_DYNAMIC);
525
527 ptr,
528 prop,
529 prop_is_param_dyn_alloc,
530 0,
531 check_item_type,
532 item_type_str,
533 &totitem,
534 error_prefix) == -1)
535 {
536 return -1;
537 }
538
539 if (totitem) {
540 /* NOTE: this code is confusing. */
541 if (prop_is_param_dyn_alloc) {
542 /* not freeing allocated mem, RNA_parameter_list_free() will do this */
543 ParameterDynAlloc *param_alloc = reinterpret_cast<ParameterDynAlloc *>(param_data);
544 param_alloc->array_tot = totitem;
545
546 /* freeing param list will free */
547 param_alloc->array = MEM_new_zeroed(item_size * totitem, "py_to_array dyn");
548
549 data = static_cast<char *>(param_alloc->array);
550 }
551 else if (param_data) {
552 data = param_data;
553 }
554 else {
555 data = static_cast<char *>(PyMem_MALLOC(item_size * totitem));
556 }
557
558 /* will only fail in very rare cases since we already validated the
559 * python data, the check here is mainly for completeness. */
560 if (copy_values(seq, ptr, prop, 0, data, item_size, nullptr, convert_item, nullptr) != nullptr)
561 {
562 if (param_data == nullptr) {
563 /* nullptr can only pass through in case RNA property array-length is 0 (impossible?) */
564 rna_set_array(ptr, prop, data);
565 PyMem_FREE(data);
566 }
567 }
568 else {
569 if (param_data == nullptr) {
570 PyMem_FREE(data);
571 }
572
573 PyErr_Format(PyExc_TypeError,
574 "%s internal error parsing sequence of type '%s' after successful validation",
575 error_prefix,
576 Py_TYPE(seq)->tp_name);
577 return -1;
578 }
579 }
580
581 return 0;
582}
583
584static int py_to_array_index(PyObject *py,
585 PointerRNA *ptr,
586 PropertyRNA *prop,
587 int lvalue_dim,
588 int arrayoffset,
589 int index,
590 ItemTypeCheckFunc check_item_type,
591 const char *item_type_str,
592 const ItemConvert_FuncArg *convert_item,
593 RNA_SetIndexFunc rna_set_index,
594 const char *error_prefix)
595{
596 int totdim, dimsize[MAX_ARRAY_DIMENSION];
597 int totitem, i;
598
599 totdim = RNA_property_array_dimension(ptr, prop, dimsize);
600
601 /* convert index */
602
603 /* arr[3][4][5]
604 *
605 * arr[2] = x
606 * lvalue_dim = 0, index = 0 + 2 * 4 * 5
607 *
608 * arr[2][3] = x
609 * lvalue_dim = 1, index = 40 + 3 * 5 */
610
611 lvalue_dim++;
612
613 for (i = lvalue_dim; i < totdim; i++) {
614 index *= dimsize[i];
615 }
616
617 index += arrayoffset;
618
619 if (lvalue_dim == totdim) { /* single item, assign directly */
620 if (!check_item_type(py)) {
621 PyErr_Format(PyExc_TypeError,
622 "%s %.200s.%.200s, expected a %s type, not %s",
623 error_prefix,
626 item_type_str,
627 Py_TYPE(py)->tp_name);
628 return -1;
629 }
630 copy_value_single(py, ptr, prop, nullptr, 0, &index, convert_item, rna_set_index);
631 }
632 else {
633 const bool prop_is_param_dyn_alloc = false;
634 if (validate_array(py,
635 ptr,
636 prop,
637 prop_is_param_dyn_alloc,
638 lvalue_dim,
639 check_item_type,
640 item_type_str,
641 &totitem,
642 error_prefix) == -1)
643 {
644 return -1;
645 }
646
647 if (totitem) {
648 copy_values(py, ptr, prop, lvalue_dim, nullptr, 0, &index, convert_item, rna_set_index);
649 }
650 }
651 return 0;
652}
653
654static void py_to_float(const ItemConvertArgData *arg, PyObject *py, char *data)
655{
656 const float *range = arg->float_data.range;
657 float value = float(PyFloat_AsDouble(py));
658 CLAMP(value, range[0], range[1]);
659 *reinterpret_cast<float *>(data) = value;
660}
661
662static void py_to_int(const ItemConvertArgData *arg, PyObject *py, char *data)
663{
664 const int *range = arg->int_data.range;
665 int value = PyC_Long_AsI32(py);
666 CLAMP(value, range[0], range[1]);
667 *reinterpret_cast<int *>(data) = value;
668}
669
670static void py_to_bool(const ItemConvertArgData * /*arg*/, PyObject *py, char *data)
671{
672 *reinterpret_cast<bool *>(data) = bool(PyObject_IsTrue(py));
673}
674
675static bool py_float_check(PyObject *py)
676{
677 /* accept both floats and integers */
678 return PyNumber_Check(py);
679}
680
681static bool py_int_check(PyObject *py)
682{
683 /* accept only integers */
684 return PyLong_Check(py);
685}
686
687static bool py_bool_check(PyObject *py)
688{
689 return PyBool_Check(py);
690}
691
692static void float_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, void *value)
693{
694 RNA_property_float_set_index(ptr, prop, index, *static_cast<float *>(value));
695}
696
697static void int_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, void *value)
698{
699 RNA_property_int_set_index(ptr, prop, index, *static_cast<int *>(value));
700}
701
702static void bool_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, void *value)
703{
704 RNA_property_boolean_set_index(ptr, prop, index, *static_cast<bool *>(value));
705}
706
708 PropertyRNA *prop,
709 ItemConvert_FuncArg *convert_item)
710{
711 float *range = convert_item->arg.float_data.range;
712 convert_item->func = py_to_float;
713 RNA_property_float_range(ptr, prop, &range[0], &range[1]);
714}
715
717 PropertyRNA *prop,
718 ItemConvert_FuncArg *convert_item)
719{
720 int *range = convert_item->arg.int_data.range;
721 convert_item->func = py_to_int;
722 RNA_property_int_range(ptr, prop, &range[0], &range[1]);
723}
724
725static void convert_item_init_bool(PointerRNA * /*ptr*/,
726 PropertyRNA * /*prop*/,
727 ItemConvert_FuncArg *convert_item)
728{
729 convert_item->func = py_to_bool;
730}
731
733 PointerRNA *ptr, PropertyRNA *prop, char *param_data, PyObject *py, const char *error_prefix)
734{
735 int ret;
736 switch (RNA_property_type(prop)) {
737 case PROP_FLOAT: {
738 ItemConvert_FuncArg convert_item;
739 convert_item_init_float(ptr, prop, &convert_item);
740
741 ret = py_to_array(py,
742 ptr,
743 prop,
744 param_data,
746 "float",
747 sizeof(float),
748 &convert_item,
750 error_prefix);
751 break;
752 }
753 case PROP_INT: {
754 ItemConvert_FuncArg convert_item;
755 convert_item_init_int(ptr, prop, &convert_item);
756
757 ret = py_to_array(py,
758 ptr,
759 prop,
760 param_data,
762 "int",
763 sizeof(int),
764 &convert_item,
766 error_prefix);
767 break;
768 }
769 case PROP_BOOLEAN: {
770 ItemConvert_FuncArg convert_item;
771 convert_item_init_bool(ptr, prop, &convert_item);
772
773 ret = py_to_array(py,
774 ptr,
775 prop,
776 param_data,
778 "boolean",
779 sizeof(bool),
780 &convert_item,
782 error_prefix);
783 break;
784 }
785 default: {
786 PyErr_SetString(PyExc_TypeError, "not an array type");
787 ret = -1;
788 break;
789 }
790 }
791
792 return ret;
793}
794
796 PropertyRNA *prop,
797 int arraydim,
798 int arrayoffset,
799 int index,
800 PyObject *py,
801 const char *error_prefix)
802{
803 int ret;
804 switch (RNA_property_type(prop)) {
805 case PROP_FLOAT: {
806 ItemConvert_FuncArg convert_item;
807 convert_item_init_float(ptr, prop, &convert_item);
808
810 ptr,
811 prop,
812 arraydim,
813 arrayoffset,
814 index,
816 "float",
817 &convert_item,
819 error_prefix);
820 break;
821 }
822 case PROP_INT: {
823 ItemConvert_FuncArg convert_item;
824 convert_item_init_int(ptr, prop, &convert_item);
825
827 ptr,
828 prop,
829 arraydim,
830 arrayoffset,
831 index,
833 "int",
834 &convert_item,
836 error_prefix);
837 break;
838 }
839 case PROP_BOOLEAN: {
840 ItemConvert_FuncArg convert_item;
841 convert_item_init_bool(ptr, prop, &convert_item);
842
844 ptr,
845 prop,
846 arraydim,
847 arrayoffset,
848 index,
850 "boolean",
851 &convert_item,
853 error_prefix);
854 break;
855 }
856 default: {
857 PyErr_SetString(PyExc_TypeError, "not an array type");
858 ret = -1;
859 break;
860 }
861 }
862
863 return ret;
864}
865
867{
868 PyObject *item;
869
870 switch (RNA_property_type(prop)) {
871 case PROP_FLOAT:
872 item = PyFloat_FromDouble(RNA_property_float_get_index(ptr, prop, index));
873 break;
874 case PROP_BOOLEAN:
875 item = PyBool_FromLong(RNA_property_boolean_get_index(ptr, prop, index));
876 break;
877 case PROP_INT:
878 item = PyLong_FromLong(RNA_property_int_get_index(ptr, prop, index));
879 break;
880 default:
881 PyErr_SetString(PyExc_TypeError, "not an array type");
882 item = nullptr;
883 break;
884 }
885
886 return item;
887}
888
889#if 0
890/* XXX this is not used (and never will?) */
891/* Given an array property, creates an N-dimensional tuple of values. */
892static PyObject *pyrna_py_from_array_internal(PointerRNA *ptr,
893 PropertyRNA *prop,
894 int dim,
895 int *index)
896{
897 PyObject *tuple;
898 int i, len;
899 int totdim = RNA_property_array_dimension(ptr, prop, nullptr);
900
901 len = RNA_property_multi_array_length(ptr, prop, dim);
902
903 tuple = PyTuple_New(len);
904
905 for (i = 0; i < len; i++) {
906 PyObject *item;
907
908 if (dim + 1 < totdim) {
909 item = pyrna_py_from_array_internal(ptr, prop, dim + 1, index);
910 }
911 else {
912 item = pyrna_array_index(ptr, prop, *index);
913 *index = *index + 1;
914 }
915
916 if (!item) {
917 Py_DECREF(tuple);
918 return nullptr;
919 }
920
921 PyTuple_SET_ITEM(tuple, i, item);
922 }
923
924 return tuple;
925}
926#endif
927
929 PointerRNA *ptr,
930 PropertyRNA *prop,
931 int index)
932{
933 int totdim, arraydim, arrayoffset, dimsize[MAX_ARRAY_DIMENSION], i, len;
934 BPy_PropertyArrayRNA *ret = nullptr;
935
936 arraydim = self ? self->arraydim : 0;
937 arrayoffset = self ? self->arrayoffset : 0;
938
939 /* just in case check */
940 len = RNA_property_multi_array_length(ptr, prop, arraydim);
941 if (index >= len || index < 0) {
942 /* This shouldn't happen because higher level functions must check for invalid index. */
943 CLOG_WARN(BPY_LOG_RNA, "invalid index %d for array with length=%d", index, len);
944
945 PyErr_SetString(PyExc_IndexError, "out of range");
946 return nullptr;
947 }
948
949 totdim = RNA_property_array_dimension(ptr, prop, dimsize);
950
951 if (arraydim + 1 < totdim) {
952 ret = reinterpret_cast<BPy_PropertyArrayRNA *>(pyrna_prop_CreatePyObject(ptr, prop));
953 ret->arraydim = arraydim + 1;
954
955 /* arr[3][4][5]
956 *
957 * x = arr[2]
958 * index = 0 + 2 * 4 * 5
959 *
960 * x = arr[2][3]
961 * index = offset + 3 * 5 */
962
963 for (i = arraydim + 1; i < totdim; i++) {
964 index *= dimsize[i];
965 }
966
967 ret->arrayoffset = arrayoffset + index;
968 }
969 else {
970 index = arrayoffset + index;
971 ret = reinterpret_cast<BPy_PropertyArrayRNA *>(pyrna_array_index(ptr, prop, index));
972 }
973
974 return reinterpret_cast<PyObject *>(ret);
975}
976
978{
979 PyObject *ret;
980
982
983 /* Is this a math object? */
984 if (ret) {
985 return ret;
986 }
987
988 return pyrna_prop_CreatePyObject(ptr, prop);
989}
990
991int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value)
992{
993 /* TODO: multi-dimensional arrays. */
994
995 const int len = RNA_property_array_length(ptr, prop);
996 int type;
997 int i;
998
999 if (len == 0) {
1000 /* possible with dynamic arrays */
1001 return 0;
1002 }
1003
1004 if (RNA_property_array_dimension(ptr, prop, nullptr) > 1) {
1005 PyErr_SetString(PyExc_TypeError, "PropertyRNA - multi dimensional arrays not supported yet");
1006 return -1;
1007 }
1008
1009 type = RNA_property_type(prop);
1010
1011 switch (type) {
1012 case PROP_FLOAT: {
1013 const float value_f = PyFloat_AsDouble(value);
1014 if (value_f == -1 && PyErr_Occurred()) {
1015 PyErr_Clear();
1016 return 0;
1017 }
1018
1019 float tmp[32];
1020 float *tmp_arr;
1021
1022 if (len * sizeof(float) > sizeof(tmp)) {
1023 tmp_arr = static_cast<float *>(PyMem_MALLOC(len * sizeof(float)));
1024 }
1025 else {
1026 tmp_arr = tmp;
1027 }
1028
1029 RNA_property_float_get_array(ptr, prop, tmp_arr);
1030
1031 for (i = 0; i < len; i++) {
1032 if (tmp_arr[i] == value_f) {
1033 break;
1034 }
1035 }
1036
1037 if (tmp_arr != tmp) {
1038 PyMem_FREE(tmp_arr);
1039 }
1040
1041 return i < len ? 1 : 0;
1042 }
1043 case PROP_INT: {
1044 const int value_i = PyC_Long_AsI32(value);
1045 if (value_i == -1 && PyErr_Occurred()) {
1046 PyErr_Clear();
1047 return 0;
1048 }
1049
1050 int tmp[32];
1051 int *tmp_arr;
1052
1053 if (len * sizeof(int) > sizeof(tmp)) {
1054 tmp_arr = static_cast<int *>(PyMem_MALLOC(len * sizeof(int)));
1055 }
1056 else {
1057 tmp_arr = tmp;
1058 }
1059
1060 RNA_property_int_get_array(ptr, prop, tmp_arr);
1061
1062 for (i = 0; i < len; i++) {
1063 if (tmp_arr[i] == value_i) {
1064 break;
1065 }
1066 }
1067
1068 if (tmp_arr != tmp) {
1069 PyMem_FREE(tmp_arr);
1070 }
1071
1072 return i < len ? 1 : 0;
1073 }
1074 case PROP_BOOLEAN: {
1075 const int value_i = PyC_Long_AsBool(value);
1076 if (value_i == -1 && PyErr_Occurred()) {
1077 PyErr_Clear();
1078 return 0;
1079 }
1080
1081 bool tmp[32];
1082 bool *tmp_arr;
1083
1084 if (len * sizeof(bool) > sizeof(tmp)) {
1085 tmp_arr = static_cast<bool *>(PyMem_MALLOC(len * sizeof(bool)));
1086 }
1087 else {
1088 tmp_arr = tmp;
1089 }
1090
1091 RNA_property_boolean_get_array(ptr, prop, tmp_arr);
1092
1093 for (i = 0; i < len; i++) {
1094 if (tmp_arr[i] == bool(value_i)) {
1095 break;
1096 }
1097 }
1098
1099 if (tmp_arr != tmp) {
1100 PyMem_FREE(tmp_arr);
1101 }
1102
1103 return i < len ? 1 : 0;
1104 }
1105 }
1106
1107 /* should never reach this */
1108 PyErr_SetString(PyExc_TypeError, "PropertyRNA - type not in float/bool/int");
1109 return -1;
1110}
1111
1112} // namespace blender
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Read Blender memory allocation and freeing.
#define CLAMP(a, b, c)
#define MAX_ARRAY_DIMENSION
#define index(i)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define BaseMath_ReadCallback(_self)
Definition mathutils.hh:147
#define MatrixObject_Check(v)
BMesh Iterators.
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void int_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, void *value)
static void py_to_bool(const ItemConvertArgData *, PyObject *py, char *data)
static bool py_int_check(PyObject *py)
bool(*)(PyObject *py_data) ItemTypeCheckFunc
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
unsigned int uint
Definition BKE_text.h:55
static int validate_array_type(PyObject *seq, int dim, int totdim, int dimsize[], bool is_dynamic, ItemTypeCheckFunc check_item_type, const char *item_type_str, const char *error_prefix)
const char * RNA_property_identifier(const PropertyRNA *prop)
void RNA_property_boolean_get_array(PointerRNA *ptr, PropertyRNA *prop, bool *values)
float RNA_property_float_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
static char * copy_values(PyObject *seq, PointerRNA *ptr, PropertyRNA *prop, int dim, char *data, uint item_size, int *index, const ItemConvert_FuncArg *convert_item, RNA_SetIndexFunc rna_set_index)
static char * copy_value_single(PyObject *item, PointerRNA *ptr, PropertyRNA *prop, char *data, uint item_size, int *index, const ItemConvert_FuncArg *convert_item, RNA_SetIndexFunc rna_set_index)
static int validate_array_length(PyObject *rvalue, PointerRNA *ptr, PropertyRNA *prop, const bool prop_is_param_dyn_alloc, int lvalue_dim, int *r_totitem, const char *error_prefix)
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
void RNA_property_boolean_set_array(PointerRNA *ptr, PropertyRNA *prop, const bool *values)
void(*)(PointerRNA *ptr, PropertyRNA *prop, int index, void *data_item) RNA_SetIndexFunc
static char * copy_values_fast(PyObject *seq, PointerRNA *ptr, PropertyRNA *prop, int dim, int totdim, char *data, uint item_size, int *index, const ItemConvert_FuncArg *convert_item, RNA_SetIndexFunc rna_set_index)
static int validate_array(PyObject *rvalue, PointerRNA *ptr, PropertyRNA *prop, const bool prop_is_param_dyn_alloc, int lvalue_dim, ItemTypeCheckFunc check_item_type, const char *item_type_str, int *r_totitem, const char *error_prefix)
void RNA_property_int_set_array(PointerRNA *ptr, PropertyRNA *prop, const int *values)
PropertyType RNA_property_type(PropertyRNA *prop)
PyObject * pyrna_py_from_array_index(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, int index)
PyObject * pyrna_prop_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop)
Definition bpy_rna.cc:8931
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void(*)(const ItemConvertArgData *arg, PyObject *py_data, char *data) ItemConvertFunc
@ PROP_BOOLEAN
Definition RNA_types.hh:210
static int py_to_array(PyObject *seq, PointerRNA *ptr, PropertyRNA *prop, char *param_data, ItemTypeCheckFunc check_item_type, const char *item_type_str, int item_size, const ItemConvert_FuncArg *convert_item, RNA_SetArrayFunc rna_set_array, const char *error_prefix)
void RNA_property_int_get_array(PointerRNA *ptr, PropertyRNA *prop, int *values)
static void convert_item_init_float(PointerRNA *ptr, PropertyRNA *prop, ItemConvert_FuncArg *convert_item)
BMesh const char void * data
static void float_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, void *value)
bool RNA_property_boolean_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
int pyrna_py_to_array_index(PointerRNA *ptr, PropertyRNA *prop, int arraydim, int arrayoffset, int index, PyObject *py, const char *error_prefix)
PyObject * pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index)
int RNA_property_flag(PropertyRNA *prop)
static int count_items(PyObject *seq, int dim)
PyObject * pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
Definition bpy_rna.cc:827
static int validate_array_type_fast(PyObject *seq_fast, int dim, int totdim, int dimsize[], const bool is_dynamic, ItemTypeCheckFunc check_item_type, const char *item_type_str, const char *error_prefix)
CLG_LogRef * BPY_LOG_RNA
Definition bpy.hh:29
void(*)(PointerRNA *ptr, PropertyRNA *prop, const char *data) RNA_SetArrayFunc
static void py_to_int(const ItemConvertArgData *arg, PyObject *py, char *data)
PyObject * pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop)
int RNA_property_array_dimension(const PointerRNA *ptr, PropertyRNA *prop, int length[])
const char * RNA_struct_identifier(const StructRNA *type)
int PyC_Long_AsBool(PyObject *value)
static void convert_item_init_bool(PointerRNA *, PropertyRNA *, ItemConvert_FuncArg *convert_item)
static void bool_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, void *value)
@ PROP_DYNAMIC
Definition RNA_types.hh:498
static bool py_float_check(PyObject *py)
int RNA_property_multi_array_length(PointerRNA *ptr, PropertyRNA *prop, int dimension)
void RNA_property_int_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, int value)
void RNA_property_float_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, float value)
void RNA_property_boolean_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, bool value)
static int py_to_array_index(PyObject *py, PointerRNA *ptr, PropertyRNA *prop, int lvalue_dim, int arrayoffset, int index, ItemTypeCheckFunc check_item_type, const char *item_type_str, const ItemConvert_FuncArg *convert_item, RNA_SetIndexFunc rna_set_index, const char *error_prefix)
static void convert_item_init_int(PointerRNA *ptr, PropertyRNA *prop, ItemConvert_FuncArg *convert_item)
int pyrna_py_to_array(PointerRNA *ptr, PropertyRNA *prop, char *param_data, PyObject *py, const char *error_prefix)
void RNA_property_int_range(PointerRNA *ptr, PropertyRNA *prop, int *hardmin, int *hardmax)
static void py_to_float(const ItemConvertArgData *arg, PyObject *py, char *data)
int RNA_property_int_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value)
void RNA_property_float_range(PointerRNA *ptr, PropertyRNA *prop, float *hardmin, float *hardmax)
static bool py_bool_check(PyObject *py)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
StructRNA * type
Definition RNA_types.hh:63
struct blender::ItemConvertArgData::@231026316026002112033343320272153230201307225256::@246003124115327266256170274351103332367162372016 int_data
struct blender::ItemConvertArgData::@231026316026002112033343320272153230201307225256::@066114263055327301016176271220237047253256171113 float_data