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
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: