Custom Properties: Add boolean type

A proper boolean custom property type is commonly requested. This
commit simply adds a new `IDP_BOOLEAN` type that can be used for
boolean and boolean array custom properties. This can also be used
for exposing boolean node sockets in the geometry nodes modifier.

I've just extended the places existing IDProperty types are used, and
tested with the custom property edit operator and the python console.
Adding another IDProperty type is a straightforward extension of the
existing design.

Differential Revision: https://developer.blender.org/D12815
This commit is contained in:
2023-01-13 12:31:27 -06:00
parent 4961e5f91d
commit ef68a37e5d
16 changed files with 457 additions and 39 deletions

View File

@@ -75,6 +75,11 @@ static PyObject *idprop_py_from_idp_double(const IDProperty *prop)
return PyFloat_FromDouble(IDP_Double(prop));
}
static PyObject *idprop_py_from_idp_bool(const IDProperty *prop)
{
return PyBool_FromLong(IDP_Bool(prop));
}
static PyObject *idprop_py_from_idp_group(ID *id, IDProperty *prop, IDProperty *parent)
{
BPy_IDProperty *group = PyObject_New(BPy_IDProperty, &BPy_IDGroup_Type);
@@ -155,6 +160,8 @@ PyObject *BPy_IDGroup_WrapData(ID *id, IDProperty *prop, IDProperty *parent)
return idprop_py_from_idp_float(prop);
case IDP_DOUBLE:
return idprop_py_from_idp_double(prop);
case IDP_BOOLEAN:
return idprop_py_from_idp_bool(prop);
case IDP_GROUP:
return idprop_py_from_idp_group(id, prop, parent);
case IDP_ARRAY:
@@ -333,6 +340,12 @@ static char idp_sequence_type(PyObject *seq_fast)
}
type = IDP_DOUBLE;
}
else if (PyBool_Check(item)) {
if (i != 0 && (type != IDP_BOOLEAN)) {
return -1;
}
type = IDP_BOOLEAN;
}
else if (PyLong_Check(item)) {
if (type == IDP_IDPARRAY) { /* mixed dict/int */
return -1;
@@ -396,6 +409,13 @@ static IDProperty *idp_from_PyFloat(const char *name, PyObject *ob)
return IDP_New(IDP_DOUBLE, &val, name);
}
static IDProperty *idp_from_PyBool(const char *name, PyObject *ob)
{
IDPropertyTemplate val = {0};
val.i = PyC_Long_AsBool(ob);
return IDP_New(IDP_BOOLEAN, &val, name);
}
static IDProperty *idp_from_PyLong(const char *name, PyObject *ob)
{
IDPropertyTemplate val = {0};
@@ -466,6 +486,9 @@ static const char *idp_format_from_array_type(int type)
if (type == IDP_DOUBLE) {
return "d";
}
if (type == IDP_BOOLEAN) {
return "b";
}
return NULL;
}
@@ -549,6 +572,20 @@ static IDProperty *idp_from_PySequence_Fast(const char *name, PyObject *ob)
}
break;
}
case IDP_BOOLEAN: {
prop = IDP_New(IDP_ARRAY, &val, name);
bool *prop_data = IDP_Array(prop);
for (i = 0; i < val.array.len; i++) {
item = ob_seq_fast_items[i];
const int value = PyC_Long_AsBool(item);
if ((value == -1) && PyErr_Occurred()) {
IDP_FreeProperty(prop);
return NULL;
}
prop_data[i] = (value != 0);
}
break;
}
default:
/* should never happen */
PyErr_SetString(PyExc_RuntimeError, "internal error with idp array.type");
@@ -642,6 +679,9 @@ static IDProperty *idp_from_PyObject(PyObject *name_obj, PyObject *ob)
if (PyFloat_Check(ob)) {
return idp_from_PyFloat(name, ob);
}
if (PyBool_Check(ob)) {
return idp_from_PyBool(name, ob);
}
if (PyLong_Check(ob)) {
return idp_from_PyLong(name, ob);
}
@@ -779,6 +819,8 @@ PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
return idprop_py_from_idp_float(prop);
case IDP_DOUBLE:
return idprop_py_from_idp_double(prop);
case IDP_BOOLEAN:
return idprop_py_from_idp_bool(prop);
case IDP_ID:
return idprop_py_from_idp_id(prop);
case IDP_ARRAY: {
@@ -813,6 +855,13 @@ PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
}
break;
}
case IDP_BOOLEAN: {
const int8_t *array = (const int8_t *)IDP_Array(prop);
for (i = 0; i < prop->len; i++) {
PyList_SET_ITEM(seq, i, PyBool_FromLong(array[i]));
}
break;
}
default:
PyErr_Format(
PyExc_RuntimeError, "%s: invalid/corrupt array type '%d'!", __func__, prop->subtype);
@@ -1629,20 +1678,23 @@ PyTypeObject BPy_IDGroup_Type = {
/** \name ID Array Methods
* \{ */
static PyTypeObject *idp_array_py_type(BPy_IDArray *self, bool *r_is_double)
static PyTypeObject *idp_array_py_type(BPy_IDArray *self, size_t *elem_size)
{
switch (self->prop->subtype) {
case IDP_FLOAT:
*r_is_double = false;
*elem_size = sizeof(float);
return &PyFloat_Type;
case IDP_DOUBLE:
*r_is_double = true;
*elem_size = sizeof(double);
return &PyFloat_Type;
case IDP_BOOLEAN:
*elem_size = sizeof(int8_t);
return &PyBool_Type;
case IDP_INT:
*r_is_double = false;
*elem_size = sizeof(int);
return &PyLong_Type;
default:
*r_is_double = false;
*elem_size = 0;
return NULL;
}
}
@@ -1653,7 +1705,7 @@ static PyObject *BPy_IDArray_repr(BPy_IDArray *self)
}
PyDoc_STRVAR(BPy_IDArray_get_typecode_doc,
"The type of the data in the array {'f': float, 'd': double, 'i': int}.");
"The type of the data in the array {'f': float, 'd': double, 'i': int, 'b': bool}.");
static PyObject *BPy_IDArray_get_typecode(BPy_IDArray *self)
{
switch (self->prop->subtype) {
@@ -1663,6 +1715,8 @@ static PyObject *BPy_IDArray_get_typecode(BPy_IDArray *self)
return PyUnicode_FromString("d");
case IDP_INT:
return PyUnicode_FromString("i");
case IDP_BOOLEAN:
return PyUnicode_FromString("b");
}
PyErr_Format(
@@ -1714,6 +1768,8 @@ static PyObject *BPy_IDArray_GetItem(BPy_IDArray *self, Py_ssize_t index)
return PyFloat_FromDouble(((double *)IDP_Array(self->prop))[index]);
case IDP_INT:
return PyLong_FromLong((long)((int *)IDP_Array(self->prop))[index]);
case IDP_BOOLEAN:
return PyBool_FromLong((long)((int8_t *)IDP_Array(self->prop))[index]);
}
PyErr_Format(
@@ -1755,6 +1811,15 @@ static int BPy_IDArray_SetItem(BPy_IDArray *self, Py_ssize_t index, PyObject *va
((int *)IDP_Array(self->prop))[index] = i;
break;
}
case IDP_BOOLEAN: {
const int i = PyC_Long_AsBool(value);
if (i == -1 && PyErr_Occurred()) {
return -1;
}
((int8_t *)IDP_Array(self->prop))[index] = i;
break;
}
}
return 0;
}
@@ -1810,6 +1875,13 @@ static PyObject *BPy_IDArray_slice(BPy_IDArray *self, int begin, int end)
}
break;
}
case IDP_BOOLEAN: {
const int8_t *array = (const int8_t *)IDP_Array(prop);
for (count = begin; count < end; count++) {
PyTuple_SET_ITEM(tuple, count - begin, PyBool_FromLong((long)array[count]));
}
break;
}
}
return tuple;
@@ -1818,9 +1890,8 @@ static PyObject *BPy_IDArray_slice(BPy_IDArray *self, int begin, int end)
static int BPy_IDArray_ass_slice(BPy_IDArray *self, int begin, int end, PyObject *seq)
{
IDProperty *prop = self->prop;
bool is_double;
const PyTypeObject *py_type = idp_array_py_type(self, &is_double);
const size_t elem_size = is_double ? sizeof(double) : sizeof(float);
size_t elem_size;
const PyTypeObject *py_type = idp_array_py_type(self, &elem_size);
size_t alloc_len;
size_t size;
void *vec;
@@ -1933,6 +2004,9 @@ static int itemsize_by_idarray_type(int array_type)
if (array_type == IDP_DOUBLE) {
return sizeof(double);
}
if (array_type == IDP_BOOLEAN) {
return sizeof(bool);
}
return -1; /* should never happen */
}