Custom Properties: officially support int and float arrays in the UI.

In some rare cases it is convenient to store a short array value
as a custom property, e.g. a vector or color. For example, it may
be helpful when importing/exporting certain formats that support
custom or nonstandard attributes on objects.

The custom property storage already can handle arrays in order to
support properties defined via python. The only thing missing is
UI support (and some bugs), and this patch fixes that:

- Allow editing short array properties via Custom Properties panel.
- Fix a UI layout sizing bug triggered by the previous item.
- Fix a dependency graph bug with drivers using such properties.
- Make RNA_*_get_default_array code robust in case of size mismatch.
- Support custom default values for array properties, allowing
  both an array and a scalar value.

Reviewers: campbellbarton

Differential Revision: https://developer.blender.org/D5457
This commit is contained in:
Alexander Gavrilov 2019-08-10 14:42:14 +03:00
parent 6e7ea807e1
commit aef08fda3a
Notes: blender-bot 2023-02-14 02:22:07 +01:00
Referenced by issue #68831, RNA introspection broken (hard crash) by rBaef08fda3ade
6 changed files with 189 additions and 62 deletions

View File

@ -20,6 +20,15 @@
import bpy
from mathutils import Vector
from idprop.types import IDPropertyArray, IDPropertyGroup
ARRAY_TYPES = (list, tuple, IDPropertyArray, Vector)
# Maximum length of an array property for which a multi-line
# edit field will be displayed in the Custom Properties panel.
MAX_DISPLAY_ROWS = 4
def rna_idprop_ui_get(item, create=True):
try:
@ -101,23 +110,47 @@ def rna_idprop_has_properties(rna_item):
return (nbr_props > 1) or (nbr_props and '_RNA_UI' not in keys)
def rna_idprop_value_to_python(value):
if isinstance(value, IDPropertyArray):
return value.to_list()
elif isinstance(value, IDPropertyGroup):
return value.to_dict()
else:
return value
def rna_idprop_value_item_type(value):
is_array = isinstance(value, ARRAY_TYPES) and len(value) > 0
item_value = value[0] if is_array else value
return type(item_value), is_array
def rna_idprop_ui_prop_default_set(item, prop, value):
defvalue = None
try:
prop_type = type(item[prop])
prop_type, is_array = rna_idprop_value_item_type(item[prop])
if prop_type in {int, float}:
defvalue = prop_type(value)
if is_array and isinstance(value, ARRAY_TYPES):
value = [prop_type(item) for item in value]
if any(value):
defvalue = value
else:
defvalue = prop_type(value)
except KeyError:
pass
except ValueError:
pass
if defvalue:
rna_ui = rna_idprop_ui_prop_get(item, prop, True)
rna_ui["default"] = defvalue
else:
rna_ui = rna_idprop_ui_prop_get(item, prop)
if rna_ui and "default" in rna_ui:
del rna_ui["default"]
if rna_ui:
rna_ui.pop("default", None)
return defvalue
def rna_idprop_ui_create(
@ -129,7 +162,7 @@ def rna_idprop_ui_create(
):
"""Create and initialize a custom property with limits, defaults and other settings."""
proptype = type(default)
proptype, is_array = rna_idprop_value_item_type(default)
# Sanitize limits
if proptype is bool:
@ -159,7 +192,7 @@ def rna_idprop_ui_create(
rna_ui["max"] = proptype(max)
rna_ui["soft_max"] = proptype(soft_max)
if default:
if default and (not is_array or any(default)):
rna_ui["default"] = default
# Assign other settings
@ -252,7 +285,11 @@ def draw(layout, context, context_member, property_type, use_edit=True):
row.label(text=key, translate=False)
# explicit exception for arrays.
if to_dict or to_list:
show_array_ui = to_list and not is_rna and 0 < len(val) <= MAX_DISPLAY_ROWS
if show_array_ui and isinstance(val[0], (int, float)):
row.prop(rna_item, '["%s"]' % escape_identifier(key), text="")
elif to_dict or to_list:
row.label(text=val_draw, translate=False)
else:
if is_rna:

View File

@ -1139,6 +1139,8 @@ class WM_OT_properties_edit(Operator):
rna_idprop_ui_prop_get,
rna_idprop_ui_prop_clear,
rna_idprop_ui_prop_update,
rna_idprop_ui_prop_default_set,
rna_idprop_value_item_type,
)
data_path = self.data_path
@ -1174,15 +1176,15 @@ class WM_OT_properties_edit(Operator):
self._last_prop[:] = [prop]
prop_type = type(item[prop])
prop_value = item[prop]
prop_type_new = type(prop_value)
prop_type, is_array = rna_idprop_value_item_type(prop_value)
prop_ui = rna_idprop_ui_prop_get(item, prop)
if prop_type in {float, int}:
prop_ui["min"] = prop_type(self.min)
prop_ui["max"] = prop_type(self.max)
if type(default_eval) in {float, int} and default_eval != 0:
prop_ui["default"] = prop_type(default_eval)
if self.use_soft_limits:
prop_ui["soft_min"] = prop_type(self.soft_min)
@ -1193,8 +1195,10 @@ class WM_OT_properties_edit(Operator):
prop_ui["description"] = self.description
rna_idprop_ui_prop_default_set(item, prop, default_eval)
# If we have changed the type of the property, update its potential anim curves!
if prop_type_old != prop_type:
if prop_type_old != prop_type_new:
data_path = '["%s"]' % bpy.utils.escape_identifier(prop)
done = set()
@ -1231,7 +1235,7 @@ class WM_OT_properties_edit(Operator):
return {'FINISHED'}
def invoke(self, context, _event):
from rna_prop_ui import rna_idprop_ui_prop_get
from rna_prop_ui import rna_idprop_ui_prop_get, rna_idprop_value_to_python
data_path = self.data_path
@ -1263,7 +1267,7 @@ class WM_OT_properties_edit(Operator):
defval = prop_ui.get("default", None)
if defval is not None:
self.default = str(defval)
self.default = str(rna_idprop_value_to_python(defval))
self.soft_min = prop_ui.get("soft_min", self.min)
self.soft_max = prop_ui.get("soft_max", self.max)
@ -1307,12 +1311,19 @@ class WM_OT_properties_edit(Operator):
return changed
def draw(self, _context):
from rna_prop_ui import (
rna_idprop_value_item_type,
)
layout = self.layout
layout.prop(self, "property")
layout.prop(self, "value")
value = self.get_value_eval()
proptype, is_array = rna_idprop_value_item_type(value)
row = layout.row()
row.enabled = type(self.get_value_eval()) in {int, float}
row.enabled = proptype in {int, float}
row.prop(self, "default")
row = layout.row(align=True)

View File

@ -944,8 +944,9 @@ void DepsgraphNodeBuilder::build_driver_id_property(ID *id, const char *rna_path
}
PointerRNA id_ptr, ptr;
PropertyRNA *prop;
int index;
RNA_id_pointer_create(id, &id_ptr);
if (!RNA_path_resolve_full(&id_ptr, rna_path, &ptr, &prop, NULL)) {
if (!RNA_path_resolve_full(&id_ptr, rna_path, &ptr, &prop, &index)) {
return;
}
if (prop == NULL) {

View File

@ -1531,8 +1531,9 @@ void DepsgraphRelationBuilder::build_driver_id_property(ID *id, const char *rna_
}
PointerRNA id_ptr, ptr;
PropertyRNA *prop;
int index;
RNA_id_pointer_create(id, &id_ptr);
if (!RNA_path_resolve_full(&id_ptr, rna_path, &ptr, &prop, NULL)) {
if (!RNA_path_resolve_full(&id_ptr, rna_path, &ptr, &prop, &index)) {
return;
}
if (prop == NULL) {

View File

@ -1832,7 +1832,9 @@ static void ui_item_rna_size(uiLayout *layout,
h += len * UI_UNIT_Y;
}
}
else if (ui_layout_variable_size(layout)) {
/* Increase width requirement if in a variable size layout. */
if (ui_layout_variable_size(layout)) {
if (type == PROP_BOOLEAN && name[0]) {
w += UI_UNIT_X / 5;
}

View File

@ -2522,18 +2522,31 @@ void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
}
}
static void rna_property_boolean_get_default_array_values(BoolPropertyRNA *bprop, bool *values)
static void rna_property_boolean_fill_default_array_values(
const bool *defarr, int defarr_length, bool defvalue, int out_length, bool *r_values)
{
unsigned int length = bprop->property.totarraylength;
if (bprop->defaultarray) {
memcpy(values, bprop->defaultarray, sizeof(bool) * length);
if (defarr && defarr_length > 0) {
defarr_length = MIN2(defarr_length, out_length);
memcpy(r_values, defarr, sizeof(bool) * defarr_length);
}
else {
for (unsigned int i = 0; i < length; i++) {
values[i] = bprop->defaultvalue;
}
defarr_length = 0;
}
for (int i = defarr_length; i < out_length; i++) {
r_values[i] = defvalue;
}
}
static void rna_property_boolean_get_default_array_values(PointerRNA *ptr,
BoolPropertyRNA *bprop,
bool *r_values)
{
int length = bprop->property.totarraylength;
int out_length = RNA_property_array_length(ptr, (PropertyRNA *)bprop);
rna_property_boolean_fill_default_array_values(
bprop->defaultarray, length, bprop->defaultvalue, out_length, r_values);
}
void RNA_property_boolean_get_array(PointerRNA *ptr, PropertyRNA *prop, bool *values)
@ -2565,7 +2578,7 @@ void RNA_property_boolean_get_array(PointerRNA *ptr, PropertyRNA *prop, bool *va
bprop->getarray_ex(ptr, prop, values);
}
else {
rna_property_boolean_get_default_array_values(bprop, values);
rna_property_boolean_get_default_array_values(ptr, bprop, values);
}
}
@ -2684,9 +2697,7 @@ bool RNA_property_boolean_get_default(PointerRNA *UNUSED(ptr), PropertyRNA *prop
return bprop->defaultvalue;
}
void RNA_property_boolean_get_default_array(PointerRNA *UNUSED(ptr),
PropertyRNA *prop,
bool *values)
void RNA_property_boolean_get_default_array(PointerRNA *ptr, PropertyRNA *prop, bool *values)
{
BoolPropertyRNA *bprop = (BoolPropertyRNA *)rna_ensure_property(prop);
@ -2697,7 +2708,7 @@ void RNA_property_boolean_get_default_array(PointerRNA *UNUSED(ptr),
values[0] = bprop->defaultvalue;
}
else {
rna_property_boolean_get_default_array_values(bprop, values);
rna_property_boolean_get_default_array_values(ptr, bprop, values);
}
}
@ -2709,7 +2720,7 @@ bool RNA_property_boolean_get_default_index(PointerRNA *ptr, PropertyRNA *prop,
BLI_assert(RNA_property_type(prop) == PROP_BOOLEAN);
BLI_assert(RNA_property_array_check(prop) != false);
BLI_assert(index >= 0);
BLI_assert(index < prop->totarraylength);
BLI_assert(index < len);
if (len <= RNA_MAX_ARRAY_LENGTH) {
RNA_property_boolean_get_default_array(ptr, prop, tmp);
@ -2785,18 +2796,31 @@ void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
}
}
static void rna_property_int_get_default_array_values(IntPropertyRNA *iprop, int *values)
static void rna_property_int_fill_default_array_values(
const int *defarr, int defarr_length, int defvalue, int out_length, int *r_values)
{
unsigned int length = iprop->property.totarraylength;
if (iprop->defaultarray) {
memcpy(values, iprop->defaultarray, sizeof(int) * length);
if (defarr && defarr_length > 0) {
defarr_length = MIN2(defarr_length, out_length);
memcpy(r_values, defarr, sizeof(int) * defarr_length);
}
else {
for (unsigned int i = 0; i < length; i++) {
values[i] = iprop->defaultvalue;
}
defarr_length = 0;
}
for (int i = defarr_length; i < out_length; i++) {
r_values[i] = defvalue;
}
}
static void rna_property_int_get_default_array_values(PointerRNA *ptr,
IntPropertyRNA *iprop,
int *r_values)
{
int length = iprop->property.totarraylength;
int out_length = RNA_property_array_length(ptr, (PropertyRNA *)iprop);
rna_property_int_fill_default_array_values(
iprop->defaultarray, length, iprop->defaultvalue, out_length, r_values);
}
void RNA_property_int_get_array(PointerRNA *ptr, PropertyRNA *prop, int *values)
@ -2827,7 +2851,7 @@ void RNA_property_int_get_array(PointerRNA *ptr, PropertyRNA *prop, int *values)
iprop->getarray_ex(ptr, prop, values);
}
else {
rna_property_int_get_default_array_values(iprop, values);
rna_property_int_get_default_array_values(ptr, iprop, values);
}
}
@ -2999,18 +3023,34 @@ bool RNA_property_int_set_default(PointerRNA *ptr, PropertyRNA *prop, int value)
}
}
void RNA_property_int_get_default_array(PointerRNA *UNUSED(ptr), PropertyRNA *prop, int *values)
void RNA_property_int_get_default_array(PointerRNA *ptr, PropertyRNA *prop, int *values)
{
IntPropertyRNA *iprop = (IntPropertyRNA *)rna_ensure_property(prop);
BLI_assert(RNA_property_type(prop) == PROP_INT);
BLI_assert(RNA_property_array_check(prop) != false);
if (prop->arraydimension == 0) {
if (prop->magic != RNA_MAGIC) {
int length = rna_ensure_property_array_length(ptr, prop);
IDProperty *idp_ui = rna_idproperty_ui(prop);
IDProperty *item = idp_ui ? IDP_GetPropertyFromGroup(idp_ui, "default") : NULL;
int defval = (item && item->type == IDP_INT) ? IDP_Int(item) : iprop->defaultvalue;
if (item && item->type == IDP_ARRAY && item->subtype == IDP_INT) {
rna_property_int_fill_default_array_values(
IDP_Array(item), item->len, defval, length, values);
}
else {
rna_property_int_fill_default_array_values(NULL, 0, defval, length, values);
}
}
else if (prop->arraydimension == 0) {
values[0] = iprop->defaultvalue;
}
else {
rna_property_int_get_default_array_values(iprop, values);
rna_property_int_get_default_array_values(ptr, iprop, values);
}
}
@ -3022,7 +3062,7 @@ int RNA_property_int_get_default_index(PointerRNA *ptr, PropertyRNA *prop, int i
BLI_assert(RNA_property_type(prop) == PROP_INT);
BLI_assert(RNA_property_array_check(prop) != false);
BLI_assert(index >= 0);
BLI_assert(index < prop->totarraylength);
BLI_assert(index < len);
if (len <= RNA_MAX_ARRAY_LENGTH) {
RNA_property_int_get_default_array(ptr, prop, tmp);
@ -3109,18 +3149,31 @@ void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
}
}
static void rna_property_float_get_default_array_values(FloatPropertyRNA *fprop, float *values)
static void rna_property_float_fill_default_array_values(
const float *defarr, int defarr_length, float defvalue, int out_length, float *r_values)
{
unsigned int length = fprop->property.totarraylength;
if (fprop->defaultarray) {
memcpy(values, fprop->defaultarray, sizeof(float) * length);
if (defarr && defarr_length > 0) {
defarr_length = MIN2(defarr_length, out_length);
memcpy(r_values, defarr, sizeof(float) * defarr_length);
}
else {
for (unsigned int i = 0; i < length; i++) {
values[i] = fprop->defaultvalue;
}
defarr_length = 0;
}
for (int i = defarr_length; i < out_length; i++) {
r_values[i] = defvalue;
}
}
static void rna_property_float_get_default_array_values(PointerRNA *ptr,
FloatPropertyRNA *fprop,
float *r_values)
{
int length = fprop->property.totarraylength;
int out_length = RNA_property_array_length(ptr, (PropertyRNA *)fprop);
rna_property_float_fill_default_array_values(
fprop->defaultarray, length, fprop->defaultvalue, out_length, r_values);
}
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
@ -3157,7 +3210,7 @@ void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *val
fprop->getarray_ex(ptr, prop, values);
}
else {
rna_property_float_get_default_array_values(fprop, values);
rna_property_float_get_default_array_values(ptr, fprop, values);
}
}
@ -3343,20 +3396,40 @@ bool RNA_property_float_set_default(PointerRNA *ptr, PropertyRNA *prop, float va
}
}
void RNA_property_float_get_default_array(PointerRNA *UNUSED(ptr),
PropertyRNA *prop,
float *values)
void RNA_property_float_get_default_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
{
FloatPropertyRNA *fprop = (FloatPropertyRNA *)rna_ensure_property(prop);
BLI_assert(RNA_property_type(prop) == PROP_FLOAT);
BLI_assert(RNA_property_array_check(prop) != false);
if (prop->arraydimension == 0) {
if (prop->magic != RNA_MAGIC) {
int length = rna_ensure_property_array_length(ptr, prop);
IDProperty *idp_ui = rna_idproperty_ui(prop);
IDProperty *item = idp_ui ? IDP_GetPropertyFromGroup(idp_ui, "default") : NULL;
float defval = (item && item->type == IDP_DOUBLE) ? IDP_Double(item) : fprop->defaultvalue;
if (item && item->type == IDP_ARRAY && item->subtype == IDP_DOUBLE) {
double *defarr = IDP_Array(item);
for (int i = 0; i < length; i++) {
values[i] = (i < item->len) ? (float)defarr[i] : defval;
}
}
else if (item && item->type == IDP_ARRAY && item->subtype == IDP_FLOAT) {
rna_property_float_fill_default_array_values(
IDP_Array(item), item->len, defval, length, values);
}
else {
rna_property_float_fill_default_array_values(NULL, 0, defval, length, values);
}
}
else if (prop->arraydimension == 0) {
values[0] = fprop->defaultvalue;
}
else {
rna_property_float_get_default_array_values(fprop, values);
rna_property_float_get_default_array_values(ptr, fprop, values);
}
}
@ -3368,7 +3441,7 @@ float RNA_property_float_get_default_index(PointerRNA *ptr, PropertyRNA *prop, i
BLI_assert(RNA_property_type(prop) == PROP_FLOAT);
BLI_assert(RNA_property_array_check(prop) != false);
BLI_assert(index >= 0);
BLI_assert(index < prop->totarraylength);
BLI_assert(index < len);
if (len <= RNA_MAX_ARRAY_LENGTH) {
RNA_property_float_get_default_array(ptr, prop, tmp);
@ -7083,6 +7156,7 @@ ParameterList *RNA_parameter_list_create(ParameterList *parms,
FunctionRNA *func)
{
PropertyRNA *parm;
PointerRNA null_ptr = PointerRNA_NULL;
void *data;
int alloc_size = 0, size;
@ -7122,7 +7196,8 @@ ParameterList *RNA_parameter_list_create(ParameterList *parms,
switch (parm->type) {
case PROP_BOOLEAN:
if (parm->arraydimension) {
rna_property_boolean_get_default_array_values((BoolPropertyRNA *)parm, data);
rna_property_boolean_get_default_array_values(
&null_ptr, (BoolPropertyRNA *)parm, data);
}
else {
memcpy(data, &((BoolPropertyRNA *)parm)->defaultvalue, size);
@ -7130,7 +7205,7 @@ ParameterList *RNA_parameter_list_create(ParameterList *parms,
break;
case PROP_INT:
if (parm->arraydimension) {
rna_property_int_get_default_array_values((IntPropertyRNA *)parm, data);
rna_property_int_get_default_array_values(&null_ptr, (IntPropertyRNA *)parm, data);
}
else {
memcpy(data, &((IntPropertyRNA *)parm)->defaultvalue, size);
@ -7138,7 +7213,7 @@ ParameterList *RNA_parameter_list_create(ParameterList *parms,
break;
case PROP_FLOAT:
if (parm->arraydimension) {
rna_property_float_get_default_array_values((FloatPropertyRNA *)parm, data);
rna_property_float_get_default_array_values(&null_ptr, (FloatPropertyRNA *)parm, data);
}
else {
memcpy(data, &((FloatPropertyRNA *)parm)->defaultvalue, size);