Blender V5.3
idprop_py_ui_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 <Python.h>
10
11#include "python_compat.hh" /* IWYU pragma: keep. */
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_string.hh"
16#include "BLI_utildefines.hh"
17
18#include "idprop_py_ui_api.hh"
19
20#include "BKE_idprop.hh"
21
22#include "DNA_ID.h"
23
24#include "RNA_access.hh"
25#include "RNA_enum_types.hh"
26
27#define USE_STRING_COERCE
28
29#include "py_capi_rna.hh"
30#include "py_capi_utils.hh"
31
32namespace blender {
33
34/* -------------------------------------------------------------------- */
37
42 const char *rna_subtype,
43 const char *description)
44{
45 if (rna_subtype != nullptr) {
47 rna_subtype,
48 &ui_data->rna_subtype,
49 "IDPropertyUIManager.update") == -1)
50 {
51 return false;
52 }
53 }
54
55 if (description != nullptr) {
56 ui_data->description = BLI_strdup(description);
57 }
58
59 return true;
60}
61
62/* Utility function for parsing ints in an if statement. */
63static bool py_long_as_int(PyObject *py_long, int *r_int)
64{
65 if (PyLong_CheckExact(py_long)) {
66 *r_int = int(PyLong_AS_LONG(py_long));
67 return true;
68 }
69 return false;
70}
71
77static bool try_parse_enum_item(PyObject *py_item, const int index, IDPropertyUIDataEnumItem &item)
78{
79 if (!PyTuple_CheckExact(py_item)) {
80 return false;
81 }
82 Py_ssize_t item_size = PyTuple_GET_SIZE(py_item);
83 if (item_size < 3 || item_size > 5) {
84 return false;
85 }
86
87 Py_ssize_t identifier_len;
88 Py_ssize_t name_len;
89 Py_ssize_t description_len;
90 const char *identifier = PyUnicode_AsUTF8AndSize(PyTuple_GET_ITEM(py_item, 0), &identifier_len);
91 const char *name = PyUnicode_AsUTF8AndSize(PyTuple_GET_ITEM(py_item, 1), &name_len);
92 const char *description = PyUnicode_AsUTF8AndSize(PyTuple_GET_ITEM(py_item, 2),
93 &description_len);
94 if (!identifier || !name || !description) {
95 return false;
96 }
97
98 const char *icon_name = nullptr;
99 if (item_size <= 3) {
100 item.value = index;
101 }
102 else if (item_size == 4) {
103 if (!py_long_as_int(PyTuple_GET_ITEM(py_item, 3), &item.value)) {
104 return false;
105 }
106 }
107 else if (item_size == 5) {
108 /* Must have icon value or name. */
109 if (!py_long_as_int(PyTuple_GET_ITEM(py_item, 3), &item.icon) &&
110 !(icon_name = PyUnicode_AsUTF8(PyTuple_GET_ITEM(py_item, 3))))
111 {
112 return false;
113 }
114 if (!py_long_as_int(PyTuple_GET_ITEM(py_item, 4), &item.value)) {
115 return false;
116 }
117 }
118
119 item.identifier = BLI_strdup(identifier);
120 item.name = BLI_strdup(name);
121 item.description = BLI_strdup_null(description);
122 if (icon_name) {
124 }
125 return true;
126}
127
128static IDPropertyUIDataEnumItem *idprop_enum_items_from_py(PyObject *seq_fast, int &r_items_num)
129{
131
132 const Py_ssize_t seq_len = PySequence_Fast_GET_SIZE(seq_fast);
133 PyObject **seq_fast_items = PySequence_Fast_ITEMS(seq_fast);
134 int i;
135
136 items = MEM_new_array<IDPropertyUIDataEnumItem>(seq_len, __func__);
137 r_items_num = seq_len;
138
139 for (i = 0; i < seq_len; i++) {
140 IDPropertyUIDataEnumItem item = {nullptr, nullptr, nullptr, 0, 0};
141 PyObject *py_item = seq_fast_items[i];
142 if (try_parse_enum_item(py_item, i, item)) {
143 items[i] = item;
144 }
145 else if (py_item == Py_None) {
146 items[i].identifier = nullptr;
147 }
148 else {
149 IDP_EnumItemsFree(items, i);
150 PyErr_SetString(PyExc_TypeError,
151 "expected a tuple containing "
152 "(identifier, name, description) and optionally an "
153 "icon name and unique number");
154 return nullptr;
155 }
156 }
157
158 return items;
159}
160
166 IDPropertyUIDataInt *ui_data,
167 PyObject *default_value)
168{
169 if (PySequence_Check(default_value)) {
170 if (idprop->type != IDP_ARRAY) {
171 PyErr_SetString(PyExc_TypeError, "Only array properties can have array default values");
172 return false;
173 }
174
175 Py_ssize_t len = PySequence_Size(default_value);
176 int *new_default_array = MEM_new_array_uninitialized<int>(size_t(len), __func__);
177 if (PyC_AsArray(
178 new_default_array, sizeof(int), default_value, len, &PyLong_Type, "ui_data_update") ==
179 -1)
180 {
181 MEM_delete(new_default_array);
182 return false;
183 }
184
185 ui_data->default_array_len = len;
186 ui_data->default_array = new_default_array;
187 }
188 else {
189 const int value = PyC_Long_AsI32(default_value);
190 if ((value == -1) && PyErr_Occurred()) {
191 PyErr_SetString(PyExc_ValueError, "Cannot convert \"default\" argument to integer");
192 return false;
193 }
194
195 /* Use the non-array default, even for arrays, also prevent dangling pointer, see #127952. */
196 ui_data->default_array = nullptr;
197 ui_data->default_array_len = 0;
198
199 ui_data->default_value = value;
200 }
201
202 return true;
203}
204
208static bool idprop_ui_data_update_int(IDProperty *idprop, PyObject *args, PyObject *kwargs)
209{
210 const char *rna_subtype = nullptr;
211 const char *description = nullptr;
212 std::optional<int> min, max, soft_min, soft_max, step;
213 PyObject *default_value = nullptr;
214 PyObject *items = nullptr;
215
216 static const char *_keywords[] = {
217 "min",
218 "max",
219 "soft_min",
220 "soft_max",
221 "step",
222 "default",
223 "items",
224 "subtype",
225 "description",
226 nullptr,
227 };
228 static _PyArg_Parser _parser = {
229 "|$" /* Optional, keyword only arguments. */
230 "O&" /* `min` */
231 "O&" /* `max` */
232 "O&" /* `soft_min` */
233 "O&" /* `soft_max` */
234 "O&" /* `step` */
235 "O" /* `default` */
236 "O" /* `items` */
237 "z" /* `subtype` */
238 "z" /* `description` */
239 ":update",
240 _keywords,
241 nullptr,
242 };
243 if (!_PyArg_ParseTupleAndKeywordsFast(args,
244 kwargs,
245 &_parser,
247 &min,
249 &max,
251 &soft_min,
253 &soft_max,
255 &step,
256 &default_value,
257 &items,
258 &rna_subtype,
259 &description))
260 {
261 return false;
262 }
263
264 /* Write to a temporary copy of the UI data in case some part of the parsing fails. */
265 IDPropertyUIDataInt *ui_data_orig = reinterpret_cast<IDPropertyUIDataInt *>(idprop->ui_data);
266 IDPropertyUIDataInt ui_data = *ui_data_orig;
267
268 if (!idprop_ui_data_update_base(&ui_data.base, rna_subtype, description)) {
270 return false;
271 }
272
273 if (min) {
274 ui_data.min = *min;
275 ui_data.soft_min = std::max(ui_data.soft_min, ui_data.min);
276 ui_data.max = std::max(ui_data.min, ui_data.max);
277 }
278 if (max) {
279 ui_data.max = *max;
280 ui_data.soft_max = std::min(ui_data.soft_max, ui_data.max);
281 ui_data.min = std::min(ui_data.min, ui_data.max);
282 }
283 if (soft_min) {
284 ui_data.soft_min = *soft_min;
285 ui_data.soft_min = std::max(ui_data.soft_min, ui_data.min);
286 ui_data.soft_max = std::max(ui_data.soft_min, ui_data.soft_max);
287 }
288 if (soft_max) {
289 ui_data.soft_max = *soft_max;
290 ui_data.soft_max = std::min(ui_data.soft_max, ui_data.max);
291 ui_data.soft_min = std::min(ui_data.soft_min, ui_data.soft_max);
292 }
293 if (step) {
294 ui_data.step = *step;
295 }
296
297 if (!ELEM(default_value, nullptr, Py_None)) {
298 if (!idprop_ui_data_update_int_default(idprop, &ui_data, default_value)) {
300 &ui_data.base, IDP_ui_data_type(idprop), &ui_data_orig->base);
301 return false;
302 }
303 }
304
305 if (!ELEM(items, nullptr, Py_None)) {
306 PyObject *items_fast;
307 if (!(items_fast = PySequence_Fast(items, "expected a sequence of tuples for the enum items")))
308 {
310 &ui_data.base, IDP_ui_data_type(idprop), &ui_data_orig->base);
311 return false;
312 }
313
314 int idprop_items_num = 0;
315 IDPropertyUIDataEnumItem *idprop_items = idprop_enum_items_from_py(items_fast,
316 idprop_items_num);
317 /* If `idprop_items` is null, an exception will have been raised.
318 * If validate fails, and exception will be raised. */
319 const bool has_error = (idprop_items == nullptr) ||
321 idprop_items, idprop_items_num, [](const char *msg) {
322 PyErr_SetString(PyExc_ValueError, msg);
323 });
324 Py_DECREF(items_fast);
325 if (idprop_items != nullptr) {
326 /* Assign the data even in the case of an error since it's not practical to free
327 * the partially constructed enum. */
328 ui_data.enum_items = idprop_items;
329 ui_data.enum_items_num = idprop_items_num;
330 }
331
332 if (has_error) {
334 &ui_data.base, IDP_ui_data_type(idprop), &ui_data_orig->base);
335 return false;
336 }
337 }
338 else {
339 ui_data.enum_items = nullptr;
340 ui_data.enum_items_num = 0;
341 }
342
343 /* Write back to the property's UI data. */
345 *ui_data_orig = ui_data;
346 return true;
347}
348
354 IDPropertyUIDataBool *ui_data,
355 PyObject *default_value)
356{
357 if (PySequence_Check(default_value)) {
358 if (idprop->type != IDP_ARRAY) {
359 PyErr_SetString(PyExc_TypeError, "Only array properties can have array default values");
360 return false;
361 }
362
363 Py_ssize_t len = PySequence_Size(default_value);
364 int8_t *new_default_array = MEM_new_array_uninitialized<int8_t>(size_t(len), __func__);
365 if (PyC_AsArray(new_default_array,
366 sizeof(int8_t),
367 default_value,
368 len,
369 &PyBool_Type,
370 "ui_data_update") == -1)
371 {
372 MEM_delete(new_default_array);
373 return false;
374 }
375
376 ui_data->default_array_len = len;
377 ui_data->default_array = new_default_array;
378 }
379 else {
380 const int value = PyC_Long_AsBool(default_value);
381 if ((value == -1) && PyErr_Occurred()) {
382 PyErr_SetString(PyExc_ValueError, "Cannot convert \"default\" argument to integer");
383 return false;
384 }
385
386 /* Use the non-array default, even for arrays, also prevent dangling pointer, see #127952. */
387 ui_data->default_array_len = 0;
388 ui_data->default_array = nullptr;
389
390 ui_data->default_value = (value != 0);
391 }
392
393 return true;
394}
395
399static bool idprop_ui_data_update_bool(IDProperty *idprop, PyObject *args, PyObject *kwargs)
400{
401 const char *rna_subtype = nullptr;
402 const char *description = nullptr;
403 PyObject *default_value = nullptr;
404 const char *kwlist[] = {"default", "subtype", "description", nullptr};
405 if (!PyArg_ParseTupleAndKeywords(args,
406 kwargs,
407 "|$" /* Optional, keyword only arguments. */
408 "O" /* `default` */
409 "z" /* `subtype` */
410 "z" /* `description` */
411 ":update",
412 const_cast<char **>(kwlist),
413 &default_value,
414 &rna_subtype,
415 &description))
416 {
417 return false;
418 }
419
420 /* Write to a temporary copy of the UI data in case some part of the parsing fails. */
421 IDPropertyUIDataBool *ui_data_orig = reinterpret_cast<IDPropertyUIDataBool *>(idprop->ui_data);
422 IDPropertyUIDataBool ui_data = *ui_data_orig;
423
424 if (!idprop_ui_data_update_base(&ui_data.base, rna_subtype, description)) {
426 return false;
427 }
428
429 if (!ELEM(default_value, nullptr, Py_None)) {
430 if (!idprop_ui_data_update_bool_default(idprop, &ui_data, default_value)) {
432 &ui_data.base, IDP_ui_data_type(idprop), &ui_data_orig->base);
433 return false;
434 }
435 }
436
437 /* Write back to the property's UI data. */
439 *ui_data_orig = ui_data;
440 return true;
441}
442
448 IDPropertyUIDataFloat *ui_data,
449 PyObject *default_value)
450{
451 if (PySequence_Check(default_value)) {
452 if (idprop->type != IDP_ARRAY) {
453 PyErr_SetString(PyExc_TypeError, "Only array properties can have array default values");
454 return false;
455 }
456
457 Py_ssize_t len = PySequence_Size(default_value);
458 double *new_default_array = MEM_new_array_uninitialized<double>(size_t(len), __func__);
459 if (PyC_AsArray(new_default_array,
460 sizeof(double),
461 default_value,
462 len,
463 &PyFloat_Type,
464 "ui_data_update") == -1)
465 {
466 MEM_delete(new_default_array);
467 return false;
468 }
469
470 ui_data->default_array_len = len;
471 ui_data->default_array = new_default_array;
472 }
473 else {
474 const double value = PyFloat_AsDouble(default_value);
475 if ((value == -1.0) && PyErr_Occurred()) {
476 PyErr_SetString(PyExc_ValueError, "Cannot convert \"default\" argument to double");
477 return false;
478 }
479
480 /* Use the non-array default, even for arrays, also prevent dangling pointer, see #127952. */
481 ui_data->default_array_len = 0;
482 ui_data->default_array = nullptr;
483
484 ui_data->default_value = value;
485 }
486
487 return true;
488}
489
493static bool idprop_ui_data_update_float(IDProperty *idprop, PyObject *args, PyObject *kwargs)
494{
495 const char *rna_subtype = nullptr;
496 const char *description = nullptr;
497 std::optional<int> precision;
498 std::optional<double> min, max, soft_min, soft_max, step;
499 PyObject *default_value = nullptr;
500
501 static const char *_keywords[] = {
502 "min",
503 "max",
504 "soft_min",
505 "soft_max",
506 "step",
507 "precision",
508 "default",
509 "subtype",
510 "description",
511 nullptr,
512 };
513 static _PyArg_Parser _parser = {
514 "|$" /* Optional, keyword only arguments. */
515 "O&" /* `min` */
516 "O&" /* `max` */
517 "O&" /* `soft_min` */
518 "O&" /* `soft_max` */
519 "O&" /* `step` */
520 "O&" /* `precision` */
521 "O" /* `default` */
522 "z" /* `subtype` */
523 "z" /* `description` */
524 ":update",
525 _keywords,
526 nullptr,
527 };
528 if (!_PyArg_ParseTupleAndKeywordsFast(args,
529 kwargs,
530 &_parser,
532 &min,
534 &max,
536 &soft_min,
538 &soft_max,
540 &step,
542 &precision,
543 &default_value,
544 &rna_subtype,
545 &description))
546 {
547 return false;
548 }
549
550 /* Write to a temporary copy of the UI data in case some part of the parsing fails. */
551 IDPropertyUIDataFloat *ui_data_orig = reinterpret_cast<IDPropertyUIDataFloat *>(idprop->ui_data);
552 IDPropertyUIDataFloat ui_data = *ui_data_orig;
553
554 if (!idprop_ui_data_update_base(&ui_data.base, rna_subtype, description)) {
556 return false;
557 }
558
559 if (min) {
560 ui_data.min = *min;
561 ui_data.soft_min = std::max(ui_data.soft_min, ui_data.min);
562 ui_data.max = std::max(ui_data.min, ui_data.max);
563 }
564 if (max) {
565 ui_data.max = *max;
566 ui_data.soft_max = std::min(ui_data.soft_max, ui_data.max);
567 ui_data.min = std::min(ui_data.min, ui_data.max);
568 }
569 if (soft_min) {
570 ui_data.soft_min = *soft_min;
571 ui_data.soft_min = std::max(ui_data.soft_min, ui_data.min);
572 ui_data.soft_max = std::max(ui_data.soft_min, ui_data.soft_max);
573 }
574 if (soft_max) {
575 ui_data.soft_max = *soft_max;
576 ui_data.soft_max = std::min(ui_data.soft_max, ui_data.max);
577 ui_data.soft_min = std::min(ui_data.soft_min, ui_data.soft_max);
578 }
579 if (step) {
580 ui_data.step = float(*step);
581 }
582 if (precision) {
583 ui_data.precision = *precision;
584 }
585
586 if (!ELEM(default_value, nullptr, Py_None)) {
587 if (!idprop_ui_data_update_float_default(idprop, &ui_data, default_value)) {
589 &ui_data.base, IDP_ui_data_type(idprop), &ui_data_orig->base);
590 return false;
591 }
592 }
593
594 /* Write back to the property's UI data. */
596 *ui_data_orig = ui_data;
597 return true;
598}
599
603static bool idprop_ui_data_update_string(IDProperty *idprop, PyObject *args, PyObject *kwargs)
604{
605 const char *rna_subtype = nullptr;
606 const char *description = nullptr;
607 const char *default_value = nullptr;
608 const char *kwlist[] = {"default", "subtype", "description", nullptr};
609 if (!PyArg_ParseTupleAndKeywords(args,
610 kwargs,
611 "|$" /* Optional, keyword only arguments. */
612 "z" /* `default` */
613 "z" /* `subtype` */
614 "z" /* `description` */
615 ":update",
616 const_cast<char **>(kwlist),
617 &default_value,
618 &rna_subtype,
619 &description))
620 {
621 return false;
622 }
623
624 /* Write to a temporary copy of the UI data in case some part of the parsing fails. */
625 IDPropertyUIDataString *ui_data_orig = reinterpret_cast<IDPropertyUIDataString *>(
626 idprop->ui_data);
627 IDPropertyUIDataString ui_data = *ui_data_orig;
628
629 if (!idprop_ui_data_update_base(&ui_data.base, rna_subtype, description)) {
631 return false;
632 }
633
634 if (default_value != nullptr) {
635 ui_data.default_value = BLI_strdup(default_value);
636 }
637
638 /* Write back to the property's UI data. */
640 *ui_data_orig = ui_data;
641 return true;
642}
643
647static bool idprop_ui_data_update_id(IDProperty *idprop, PyObject *args, PyObject *kwargs)
648{
649 const char *rna_subtype = nullptr;
650 const char *description = nullptr;
651 const char *id_type = nullptr;
652 const char *kwlist[] = {"subtype", "description", "id_type", nullptr};
653 if (!PyArg_ParseTupleAndKeywords(args,
654 kwargs,
655 "|$" /* Optional, keyword only arguments. */
656 "z" /* `subtype` */
657 "z" /* `description` */
658 "z" /* `id_type` */
659 ":update",
660 const_cast<char **>(kwlist),
661 &rna_subtype,
662 &description,
663 &id_type))
664 {
665 return false;
666 }
667
668 /* Write to a temporary copy of the UI data in case some part of the parsing fails. */
669 IDPropertyUIDataID *ui_data_orig = reinterpret_cast<IDPropertyUIDataID *>(idprop->ui_data);
670 IDPropertyUIDataID ui_data = *ui_data_orig;
671
672 if (!idprop_ui_data_update_base(&ui_data.base, rna_subtype, description)) {
674 return false;
675 }
676
677 if (id_type != nullptr) {
678 int id_type_tmp;
680 rna_enum_id_type_items, id_type, &id_type_tmp, "IDPropertyUIManager.update") == -1)
681 {
682 return false;
683 }
684
685 ui_data.id_type = short(id_type_tmp);
686 }
687
688 /* Write back to the property's UI data. */
690 *ui_data_orig = ui_data;
691 return true;
692}
693
695 /* Wrap. */
696 BPy_IDPropertyUIManager_update_doc,
697 ".. method:: update(*, "
698 "subtype=None, "
699 "min=None, "
700 "max=None, "
701 "soft_min=None, "
702 "soft_max=None, "
703 "precision=None, "
704 "step=None, "
705 "default=None, "
706 "id_type=None, "
707 "items=None, "
708 "description=None)\n"
709 "\n"
710 " Update the RNA information of the IDProperty used for interaction and\n"
711 " display in the user interface. The required types for many of the keyword\n"
712 " arguments depend on the type of the property.\n"
713 "\n"
714 " :param subtype: Enumerator in :ref:`rna_enum_property_subtype_items`.\n"
715 " :type subtype: str | None\n"
716 " :param min: Minimum value (int & float properties).\n"
717 " :type min: int | float | None\n"
718 " :param max: Maximum value (int & float properties).\n"
719 " :type max: int | float | None\n"
720 " :param soft_min: Soft minimum value (int & float properties).\n"
721 " :type soft_min: int | float | None\n"
722 " :param soft_max: Soft maximum value (int & float properties).\n"
723 " :type soft_max: int | float | None\n"
724 " :param precision: Number of digits to display after the decimal point\n"
725 " (float properties).\n"
726 " :type precision: int | None\n"
727 " :param step: Step used when dragging the value (int & float properties).\n"
728 " :type step: int | float | None\n"
729 " :param default: Default value, may be a sequence for array properties.\n"
730 " :type default: Any\n"
731 " :param id_type: Enumerator in :ref:`rna_enum_id_type_items` (ID properties).\n"
732 " :type id_type: str | None\n"
733 " :param items: Enum items (int properties),\n"
734 " see :func:`bpy.props.EnumProperty` for the item format.\n"
735 " :type items: Iterable["
736 "tuple[str, str, str] | "
737 "tuple[str, str, str, int] | "
738 "tuple[str, str, str, str | int, int] | "
739 "None] | None\n"
740 " :param description: Tooltip text.\n"
741 " :type description: str | None\n");
743 PyObject *args,
744 PyObject *kwargs)
745{
746 IDProperty *property = self->property;
748
749 switch (IDP_ui_data_type(property)) {
751 IDP_ui_data_ensure(property);
752 if (!idprop_ui_data_update_int(property, args, kwargs)) {
753 return nullptr;
754 }
755 Py_RETURN_NONE;
757 IDP_ui_data_ensure(property);
758 if (!idprop_ui_data_update_bool(property, args, kwargs)) {
759 return nullptr;
760 }
761 Py_RETURN_NONE;
763 IDP_ui_data_ensure(property);
764 if (!idprop_ui_data_update_float(property, args, kwargs)) {
765 return nullptr;
766 }
767 Py_RETURN_NONE;
769 IDP_ui_data_ensure(property);
770 if (!idprop_ui_data_update_string(property, args, kwargs)) {
771 return nullptr;
772 }
773 Py_RETURN_NONE;
775 IDP_ui_data_ensure(property);
776 if (!idprop_ui_data_update_id(property, args, kwargs)) {
777 return nullptr;
778 }
779 Py_RETURN_NONE;
781 PyErr_Format(PyExc_TypeError, "IDProperty \"%s\" does not support RNA data", property->name);
782 return nullptr;
783 }
784
786 Py_RETURN_NONE;
787}
788
790
791/* -------------------------------------------------------------------- */
794
795static void idprop_ui_data_to_dict_int(IDProperty *property, PyObject *dict)
796{
797 IDPropertyUIDataInt *ui_data = reinterpret_cast<IDPropertyUIDataInt *>(property->ui_data);
798 PyObject *item;
799
800 PyDict_SetItemString(dict, "min", item = PyLong_FromLong(ui_data->min));
801 Py_DECREF(item);
802 PyDict_SetItemString(dict, "max", item = PyLong_FromLong(ui_data->max));
803 Py_DECREF(item);
804 PyDict_SetItemString(dict, "soft_min", item = PyLong_FromLong(ui_data->soft_min));
805 Py_DECREF(item);
806 PyDict_SetItemString(dict, "soft_max", item = PyLong_FromLong(ui_data->soft_max));
807 Py_DECREF(item);
808 PyDict_SetItemString(dict, "step", item = PyLong_FromLong(ui_data->step));
809 Py_DECREF(item);
810 if ((property->type == IDP_ARRAY) && ui_data->default_array) {
811 PyObject *list = PyList_New(ui_data->default_array_len);
812 for (int i = 0; i < ui_data->default_array_len; i++) {
813 PyList_SET_ITEM(list, i, PyLong_FromLong(ui_data->default_array[i]));
814 }
815 PyDict_SetItemString(dict, "default", list);
816 Py_DECREF(list);
817 }
818 else {
819 PyDict_SetItemString(dict, "default", item = PyLong_FromLong(ui_data->default_value));
820 Py_DECREF(item);
821 }
822
823 if (ui_data->enum_items_num > 0) {
824 PyObject *items_list = PyList_New(ui_data->enum_items_num);
825 for (int i = 0; i < ui_data->enum_items_num; ++i) {
826 const IDPropertyUIDataEnumItem &item = ui_data->enum_items[i];
827 BLI_assert(item.identifier != nullptr);
828 BLI_assert(item.name != nullptr);
829
830 PyObject *item_tuple = PyTuple_New(5);
831 PyTuple_SET_ITEM(item_tuple, 0, PyUnicode_FromString(item.identifier));
832 PyTuple_SET_ITEM(item_tuple, 1, PyUnicode_FromString(item.name));
833 PyTuple_SET_ITEM(
834 item_tuple, 2, PyUnicode_FromString(item.description ? item.description : ""));
835 PyTuple_SET_ITEM(item_tuple, 3, PyLong_FromLong(item.icon));
836 PyTuple_SET_ITEM(item_tuple, 4, PyLong_FromLong(item.value));
837
838 PyList_SET_ITEM(items_list, i, item_tuple);
839 }
840 PyDict_SetItemString(dict, "items", items_list);
841 Py_DECREF(items_list);
842 }
843}
844
845static void idprop_ui_data_to_dict_bool(IDProperty *property, PyObject *dict)
846{
847 IDPropertyUIDataBool *ui_data = reinterpret_cast<IDPropertyUIDataBool *>(property->ui_data);
848 PyObject *item;
849
850 if ((property->type == IDP_ARRAY) && ui_data->default_array) {
851 PyObject *list = PyList_New(ui_data->default_array_len);
852 for (int i = 0; i < ui_data->default_array_len; i++) {
853 PyList_SET_ITEM(list, i, PyBool_FromLong(ui_data->default_array[i]));
854 }
855 PyDict_SetItemString(dict, "default", list);
856 Py_DECREF(list);
857 }
858 else {
859 PyDict_SetItemString(dict, "default", item = PyBool_FromLong(ui_data->default_value));
860 Py_DECREF(item);
861 }
862}
863
864static void idprop_ui_data_to_dict_float(IDProperty *property, PyObject *dict)
865{
866 IDPropertyUIDataFloat *ui_data = reinterpret_cast<IDPropertyUIDataFloat *>(property->ui_data);
867 PyObject *item;
868
869 PyDict_SetItemString(dict, "min", item = PyFloat_FromDouble(ui_data->min));
870 Py_DECREF(item);
871 PyDict_SetItemString(dict, "max", item = PyFloat_FromDouble(ui_data->max));
872 Py_DECREF(item);
873 PyDict_SetItemString(dict, "soft_min", item = PyFloat_FromDouble(ui_data->soft_min));
874 Py_DECREF(item);
875 PyDict_SetItemString(dict, "soft_max", item = PyFloat_FromDouble(ui_data->soft_max));
876 Py_DECREF(item);
877 PyDict_SetItemString(dict, "step", item = PyFloat_FromDouble(double(ui_data->step)));
878 Py_DECREF(item);
879 PyDict_SetItemString(dict, "precision", item = PyLong_FromDouble(double(ui_data->precision)));
880 Py_DECREF(item);
881 if ((property->type == IDP_ARRAY) && ui_data->default_array) {
882 PyObject *list = PyList_New(ui_data->default_array_len);
883 for (int i = 0; i < ui_data->default_array_len; i++) {
884 PyList_SET_ITEM(list, i, PyFloat_FromDouble(ui_data->default_array[i]));
885 }
886 PyDict_SetItemString(dict, "default", list);
887 Py_DECREF(list);
888 }
889 else {
890 PyDict_SetItemString(dict, "default", item = PyFloat_FromDouble(ui_data->default_value));
891 Py_DECREF(item);
892 }
893}
894
895static void idprop_ui_data_to_dict_string(IDProperty *property, PyObject *dict)
896{
897 IDPropertyUIDataString *ui_data = reinterpret_cast<IDPropertyUIDataString *>(property->ui_data);
898 PyObject *item;
899
900 const char *default_value = (ui_data->default_value == nullptr) ? "" : ui_data->default_value;
901
902 PyDict_SetItemString(dict, "default", item = PyUnicode_FromString(default_value));
903 Py_DECREF(item);
904}
905
906static void idprop_ui_data_to_dict_id(IDProperty *property, PyObject *dict)
907{
908 IDPropertyUIDataID *ui_data = reinterpret_cast<IDPropertyUIDataID *>(property->ui_data);
909
910 short id_type_value = ui_data->id_type;
911 if (id_type_value == 0) {
912 /* While UI exposed custom properties do not allow the 'all ID types' `0` value, in
913 * py-defined IDProperties it is accepted. So force defining a valid id_type value when this
914 * function is called. */
915 ID *id = IDP_ID_get(property);
916 id_type_value = id ? GS(id->name) : ID_OB;
917 }
918
919 const char *id_type = nullptr;
920 if (!RNA_enum_identifier(rna_enum_id_type_items, id_type_value, &id_type)) {
921 /* Same fall-back as above, in case it is an unknown ID type (from a future version of
922 * Blender e.g.). */
924 }
925 PyObject *item = PyUnicode_FromString(id_type);
926 PyDict_SetItemString(dict, "id_type", item);
927 Py_DECREF(item);
928}
929
931 /* Wrap. */
932 BPy_IDPropertyUIManager_as_dict_doc,
933 ".. method:: as_dict()\n"
934 "\n"
935 " Return a dictionary of the property's RNA UI data. The fields in the\n"
936 " returned dictionary and their types will depend on the property's type.\n"
937 "\n"
938 " :return: The property's UI data.\n"
939 " :rtype: dict[str, Any]\n");
941{
942 IDProperty *property = self->property;
944
945 IDPropertyUIData *ui_data = IDP_ui_data_ensure(property);
946
947 PyObject *dict = PyDict_New();
948
949 /* RNA subtype. */
950 {
951 const char *subtype_id = nullptr;
953 /* An unknown subtype (from a future version of Blender e.g.). */
955 }
956 PyObject *item = PyUnicode_FromString(subtype_id);
957 PyDict_SetItemString(dict, "subtype", item);
958 Py_DECREF(item);
959 }
960
961 /* Description. */
962 if (ui_data->description != nullptr) {
963 PyObject *item = PyUnicode_FromString(ui_data->description);
964 PyDict_SetItemString(dict, "description", item);
965 Py_DECREF(item);
966 }
967
968 /* Type specific data. */
969 switch (IDP_ui_data_type(property)) {
971 idprop_ui_data_to_dict_string(property, dict);
972 break;
974 idprop_ui_data_to_dict_id(property, dict);
975 break;
977 idprop_ui_data_to_dict_int(property, dict);
978 break;
980 idprop_ui_data_to_dict_bool(property, dict);
981 break;
983 idprop_ui_data_to_dict_float(property, dict);
984 break;
987 break;
988 }
989
990 return dict;
991}
992
994
995/* -------------------------------------------------------------------- */
998
1000 /* Wrap. */
1001 BPy_IDPropertyUIManager_clear_doc,
1002 ".. method:: clear()\n"
1003 "\n"
1004 " Remove the RNA UI data from this IDProperty.\n");
1006{
1007 IDProperty *property = self->property;
1009
1010 if (property == nullptr) {
1011 PyErr_SetString(PyExc_RuntimeError, "IDPropertyUIManager missing property");
1013 return nullptr;
1014 }
1015
1016 if (property->ui_data != nullptr) {
1017 IDP_ui_data_free(property);
1018 }
1019
1020 Py_RETURN_NONE;
1021}
1022
1024
1025/* -------------------------------------------------------------------- */
1028
1030 /* Wrap. */
1031 BPy_IDPropertyUIManager_update_from_doc,
1032 ".. method:: update_from(ui_manager_source, /)\n"
1033 "\n"
1034 " Copy UI data from an IDProperty in the source group to a property in this group.\n"
1035 " If the source property has no UI data, the target UI data will be reset if it exists.\n"
1036 "\n"
1037 " :param ui_manager_source: The UI manager to copy from.\n"
1038 " :type ui_manager_source: :class:`IDPropertyUIManager`\n"
1039 " :raises TypeError: If the types of the two properties don't match.\n");
1041{
1042 IDProperty *property = self->property;
1044
1045 BPy_IDPropertyUIManager *ui_manager_src;
1046 if (!PyArg_ParseTuple(args,
1047 "O!" /* `ui_manager_source` */
1048 ":update_from",
1050 &ui_manager_src))
1051 {
1052 return nullptr;
1053 }
1054
1055 IDProperty *src_prop = ui_manager_src->property;
1056 if ((property->type != src_prop->type) || property->subtype != src_prop->subtype) {
1057 PyErr_SetString(PyExc_TypeError, "Properties type does not match.");
1058 return nullptr;
1059 }
1060
1061 if (property->ui_data != nullptr) {
1062 IDP_ui_data_free(property);
1063 }
1064
1065 if (ui_manager_src->property && ui_manager_src->property->ui_data) {
1066 property->ui_data = IDP_ui_data_copy(src_prop);
1067 }
1068
1069 Py_RETURN_NONE;
1070}
1071
1073
1074/* -------------------------------------------------------------------- */
1077
1078#ifdef __GNUC__
1079# ifdef __clang__
1080# pragma clang diagnostic push
1081# pragma clang diagnostic ignored "-Wcast-function-type"
1082# else
1083# pragma GCC diagnostic push
1084# pragma GCC diagnostic ignored "-Wcast-function-type"
1085# endif
1086#endif
1087
1088static PyMethodDef BPy_IDPropertyUIManager_methods[] = {
1089 {"update",
1090 reinterpret_cast<PyCFunction>(BPy_IDPropertyUIManager_update),
1091 METH_VARARGS | METH_KEYWORDS,
1092 BPy_IDPropertyUIManager_update_doc},
1093 {"as_dict",
1094 reinterpret_cast<PyCFunction>(BPy_IDIDPropertyUIManager_as_dict),
1095 METH_NOARGS,
1096 BPy_IDPropertyUIManager_as_dict_doc},
1097 {"clear",
1098 reinterpret_cast<PyCFunction>(BPy_IDPropertyUIManager_clear),
1099 METH_NOARGS,
1100 BPy_IDPropertyUIManager_clear_doc},
1101 {"update_from",
1102 reinterpret_cast<PyCFunction>(BPy_IDPropertyUIManager_update_from),
1103 METH_VARARGS,
1104 BPy_IDPropertyUIManager_update_from_doc},
1105 {nullptr, nullptr, 0, nullptr},
1106};
1107
1108#ifdef __GNUC__
1109# ifdef __clang__
1110# pragma clang diagnostic pop
1111# else
1112# pragma GCC diagnostic pop
1113# endif
1114#endif
1115
1117{
1118 return PyUnicode_FromFormat(
1119 "<bpy id prop ui manager: name=\"%s\", address=%p>", self->property->name, self->property);
1120}
1121
1123{
1124 return Py_HashPointer(self->property);
1125}
1126
1128 /* Wrap. */
1129 BPy_IDPropertyUIManager_Type_doc,
1130 "Manages the UI data of an ID property, "
1131 "see :meth:`bpy.types.bpy_struct.id_properties_ui`.");
1132
1134 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
1135 /* For printing, in format `<module>.<name>`. */
1136 /*tp_name*/ "IDPropertyUIManager",
1137 /*tp_basicsize*/ sizeof(BPy_IDPropertyUIManager),
1138 /*tp_itemsize*/ 0,
1139 /*tp_dealloc*/ nullptr,
1140 /*tp_vectorcall_offset*/ 0,
1141 /*tp_getattr*/ nullptr,
1142 /*tp_setattr*/ nullptr,
1143 /*tp_as_async*/ nullptr,
1144 /*tp_repr*/ reinterpret_cast<reprfunc>(BPy_IDPropertyUIManager_repr),
1145 /*tp_as_number*/ nullptr,
1146 /*tp_as_sequence*/ nullptr,
1147 /*tp_as_mapping*/ nullptr,
1148 /*tp_hash*/ reinterpret_cast<hashfunc>(BPy_IDPropertyUIManager_hash),
1149 /*tp_call*/ nullptr,
1150 /*tp_str*/ nullptr,
1151 /*tp_getattro*/ nullptr,
1152 /*tp_setattro*/ nullptr,
1153 /*tp_as_buffer*/ nullptr,
1154 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
1155 /*tp_doc*/ BPy_IDPropertyUIManager_Type_doc,
1156 /*tp_traverse*/ nullptr,
1157 /*tp_clear*/ nullptr,
1158 /*tp_richcompare*/ nullptr,
1159 /*tp_weaklistoffset*/ 0,
1160 /*tp_iter*/ nullptr,
1161 /*tp_iternext*/ nullptr,
1162 /*tp_methods*/ BPy_IDPropertyUIManager_methods,
1163 /*tp_members*/ nullptr,
1164 /*tp_getset*/ nullptr,
1165 /*tp_base*/ nullptr,
1166 /*tp_dict*/ nullptr,
1167 /*tp_descr_get*/ nullptr,
1168 /*tp_descr_set*/ nullptr,
1169 /*tp_dictoffset*/ 0,
1170 /*tp_init*/ nullptr,
1171 /*tp_alloc*/ nullptr,
1172 /*tp_new*/ nullptr,
1173 /*tp_free*/ nullptr,
1174 /*tp_is_gc*/ nullptr,
1175 /*tp_bases*/ nullptr,
1176 /*tp_mro*/ nullptr,
1177 /*tp_cache*/ nullptr,
1178 /*tp_subclasses*/ nullptr,
1179 /*tp_weaklist*/ nullptr,
1180 /*tp_del*/ nullptr,
1181 /*tp_version_tag*/ 0,
1182 /*tp_finalize*/ nullptr,
1183 /*tp_vectorcall*/ nullptr,
1184};
1185
1187{
1188 PyType_Ready(&BPy_IDPropertyUIManager_Type);
1189}
1190
1192
1193} // namespace blender
#define IDP_ID_get(prop)
ID and Library types, which are fundamental for SDNA.
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define ELEM(...)
btScalar max
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
bool IDP_EnumItemsValidate(const IDPropertyUIDataEnumItem *items, int items_num, void(*error_fn)(const char *))
Definition idprop.cc:558
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static PyMethodDef BPy_IDPropertyUIManager_methods[]
int PyC_ParseOptionalDouble(PyObject *o, void *p)
static Py_hash_t BPy_IDPropertyUIManager_hash(BPy_IDPropertyUIManager *self)
const EnumPropertyItem rna_enum_id_type_items[]
Definition rna_ID.cc:33
static bool idprop_ui_data_update_base(IDPropertyUIData *ui_data, const char *rna_subtype, const char *description)
static bool py_long_as_int(PyObject *py_long, int *r_int)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void idprop_ui_data_to_dict_int(IDProperty *property, PyObject *dict)
void IDPropertyUIData_Init_Types()
static PyObject * BPy_IDIDPropertyUIManager_as_dict(BPy_IDPropertyUIManager *self)
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")
const EnumPropertyItem rna_enum_property_subtype_items[]
Definition rna_rna.cc:129
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 bool idprop_ui_data_update_float_default(IDProperty *idprop, IDPropertyUIDataFloat *ui_data, PyObject *default_value)
eIDPropertyUIDataType IDP_ui_data_type(const IDProperty *prop)
Definition idprop.cc:1964
static PyObject * BPy_IDPropertyUIManager_clear(BPy_IDPropertyUIManager *self)
void IDP_ui_data_free(IDProperty *prop)
Definition idprop.cc:1340
static bool idprop_ui_data_update_float(IDProperty *idprop, PyObject *args, PyObject *kwargs)
static bool idprop_ui_data_update_bool_default(IDProperty *idprop, IDPropertyUIDataBool *ui_data, PyObject *default_value)
static bool idprop_ui_data_update_int_default(IDProperty *idprop, IDPropertyUIDataInt *ui_data, PyObject *default_value)
int PyC_ParseOptionalInt(PyObject *o, void *p)
static PyObject * BPy_IDPropertyUIManager_repr(BPy_IDPropertyUIManager *self)
static void idprop_ui_data_to_dict_string(IDProperty *property, PyObject *dict)
static bool idprop_ui_data_update_string(IDProperty *idprop, PyObject *args, PyObject *kwargs)
int PyC_Long_AsBool(PyObject *value)
bool RNA_enum_value_from_identifier(const EnumPropertyItem *item, const char *identifier, int *r_value)
PyTypeObject BPy_IDPropertyUIManager_Type
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
static PyObject * BPy_IDPropertyUIManager_update(BPy_IDPropertyUIManager *self, PyObject *args, PyObject *kwargs)
void IDP_ui_data_free_unique_contents(IDPropertyUIData *ui_data, eIDPropertyUIDataType type, const IDPropertyUIData *other)
Definition idprop.cc:1247
static void idprop_ui_data_to_dict_float(IDProperty *property, PyObject *dict)
static bool idprop_ui_data_update_int(IDProperty *idprop, PyObject *args, PyObject *kwargs)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static bool idprop_ui_data_update_id(IDProperty *idprop, PyObject *args, PyObject *kwargs)
static void idprop_ui_data_to_dict_bool(IDProperty *property, PyObject *dict)
static IDPropertyUIDataEnumItem * idprop_enum_items_from_py(PyObject *seq_fast, int &r_items_num)
static void idprop_ui_data_to_dict_id(IDProperty *property, PyObject *dict)
char * BLI_strdup_null(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_MALLOC
void IDP_EnumItemsFree(IDPropertyUIDataEnumItem *items, int items_num)
Definition idprop.cc:526
bool RNA_enum_identifier(const EnumPropertyItem *item, const int value, const char **r_identifier)
static bool try_parse_enum_item(PyObject *py_item, const int index, IDPropertyUIDataEnumItem &item)
IDPropertyUIData * IDP_ui_data_copy(const IDProperty *prop)
Definition idprop.cc:298
int pyrna_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *r_value, const char *error_prefix)
const EnumPropertyItem rna_enum_icon_items[]
Definition rna_ui_api.cc:30
IDPropertyUIData * IDP_ui_data_ensure(IDProperty *prop)
Definition idprop.cc:2034
@ IDP_UI_DATA_TYPE_ID
@ IDP_UI_DATA_TYPE_STRING
@ IDP_UI_DATA_TYPE_FLOAT
@ IDP_UI_DATA_TYPE_INT
@ IDP_UI_DATA_TYPE_UNSUPPORTED
@ IDP_UI_DATA_TYPE_BOOLEAN
bool IDP_ui_data_supported(const IDProperty *prop)
Definition idprop.cc:1986
static bool idprop_ui_data_update_bool(IDProperty *idprop, PyObject *args, PyObject *kwargs)
static PyObject * BPy_IDPropertyUIManager_update_from(BPy_IDPropertyUIManager *self, PyObject *args)
signed char int8_t
Definition BKE_text.h:76
header-only compatibility defines.
#define Py_HashPointer
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
PyObject_HEAD IDProperty * property
IDPropertyUIData base
Definition DNA_ID.h:86
IDPropertyUIData base
Definition DNA_ID.h:96
IDPropertyUIData base
Definition DNA_ID.h:119
IDPropertyUIData base
Definition DNA_ID.h:69
IDPropertyUIDataEnumItem * enum_items
Definition DNA_ID.h:81
IDPropertyUIData * ui_data
Definition DNA_ID.h:171
eIDPropertyType type
Definition DNA_ID.h:145