diff options
author | Alexander Gavrilov <angavrilov@gmail.com> | 2019-08-10 14:42:14 +0300 |
---|---|---|
committer | Alexander Gavrilov <angavrilov@gmail.com> | 2019-08-13 17:13:19 +0300 |
commit | aef08fda3ade2d0223b77d4c9c0dd5e9fcabe7b2 (patch) | |
tree | ddde62d4cdf7d9896f0c8ac8fe694cf197c3ce7b /source | |
parent | 6e7ea807e1e8aa0791a3c1d8875e74d64481973c (diff) |
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
Diffstat (limited to 'source')
4 files changed, 127 insertions, 48 deletions
diff --git a/source/blender/depsgraph/intern/builder/deg_builder_nodes.cc b/source/blender/depsgraph/intern/builder/deg_builder_nodes.cc index 986f65df3fc..fd4c1e251e4 100644 --- a/source/blender/depsgraph/intern/builder/deg_builder_nodes.cc +++ b/source/blender/depsgraph/intern/builder/deg_builder_nodes.cc @@ -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) { diff --git a/source/blender/depsgraph/intern/builder/deg_builder_relations.cc b/source/blender/depsgraph/intern/builder/deg_builder_relations.cc index 27b519a8551..3c226338bfd 100644 --- a/source/blender/depsgraph/intern/builder/deg_builder_relations.cc +++ b/source/blender/depsgraph/intern/builder/deg_builder_relations.cc @@ -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) { diff --git a/source/blender/editors/interface/interface_layout.c b/source/blender/editors/interface/interface_layout.c index 25fedf8519a..530689f2d27 100644 --- a/source/blender/editors/interface/interface_layout.c +++ b/source/blender/editors/interface/interface_layout.c @@ -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; } diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c index 731f549b497..6d02bfa0987 100644 --- a/source/blender/makesrna/intern/rna_access.c +++ b/source/blender/makesrna/intern/rna_access.c @@ -2522,20 +2522,33 @@ 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) { BoolPropertyRNA *bprop = (BoolPropertyRNA *)prop; @@ -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,20 +2796,33 @@ 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) { IntPropertyRNA *iprop = (IntPropertyRNA *)prop; @@ -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,20 +3149,33 @@ 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) { FloatPropertyRNA *fprop = (FloatPropertyRNA *)prop; @@ -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); |