diff options
author | Hans Goudey <h.goudey@me.com> | 2021-08-27 16:27:24 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2021-08-27 16:27:24 +0300 |
commit | 8b9a3b94fc148d197b137aa892855c60db2783e6 (patch) | |
tree | 0afd1a3538e76a029c0d6365f5bc06d681da92d9 /release/scripts/modules/rna_prop_ui.py | |
parent | 3f5e0f7d91e0e13870d98fe721a871e6be210489 (diff) |
Refactor IDProperty UI data storage
The storage of IDProperty UI data (min, max, default value, etc) is
quite complicated. For every property, retrieving a single one of these
values involves three string lookups. First for the "_RNA_UI" group
property, then another for a group with the property's name, then for
the data value name. Not only is this inefficient, it's hard to reason
about, unintuitive, and not at all self-explanatory.
This commit replaces that system with a UI data struct directly in the
IDProperty. If it's not used, the only cost is of a NULL pointer. Beyond
storing the description, name, and RNA subtype, derived structs are used
to store type specific UI data like min and max.
Note that this means that addons using (abusing) the `_RNA_UI` custom
property will have to be changed. A few places in the addons repository
will be changed after this commit with D9919.
**Before**
Before, first the _RNA_UI subgroup is retrieved the _RNA_UI group,
then the subgroup for the original property, then specific UI data
is accessed like any other IDProperty.
```
prop = rna_idprop_ui_prop_get(idproperties_owner, "prop_name", create=True)
prop["min"] = 1.0
```
**After**
After, the `id_properties_ui` function for RNA structs returns a python
object specifically for managing an IDProperty's UI data.
```
ui_data = idproperties_owner.id_properties_ui("prop_name")
ui_data.update(min=1.0)
```
In addition to `update`, there are now other functions:
- `as_dict`: Returns a dictionary of the property's UI data.
- `clear`: Removes the property's UI data.
- `update_from`: Copy UI data between properties,
even if they have different owners.
Differential Revision: https://developer.blender.org/D9697
Diffstat (limited to 'release/scripts/modules/rna_prop_ui.py')
-rw-r--r-- | release/scripts/modules/rna_prop_ui.py | 118 |
1 files changed, 18 insertions, 100 deletions
diff --git a/release/scripts/modules/rna_prop_ui.py b/release/scripts/modules/rna_prop_ui.py index bafa2b28bbf..26a2f9ad89b 100644 --- a/release/scripts/modules/rna_prop_ui.py +++ b/release/scripts/modules/rna_prop_ui.py @@ -30,24 +30,6 @@ ARRAY_TYPES = (list, tuple, IDPropertyArray, Vector) MAX_DISPLAY_ROWS = 4 -def rna_idprop_ui_get(item, *, create=True): - try: - return item['_RNA_UI'] - except: - if create: - item['_RNA_UI'] = {} - return item['_RNA_UI'] - else: - return None - - -def rna_idprop_ui_del(item): - try: - del item['_RNA_UI'] - except KeyError: - pass - - def rna_idprop_quote_path(prop): return "[\"%s\"]" % bpy.utils.escape_identifier(prop) @@ -59,32 +41,9 @@ def rna_idprop_ui_prop_update(item, prop): prop_rna.update() -def rna_idprop_ui_prop_get(item, prop, *, create=True): - - rna_ui = rna_idprop_ui_get(item, create=create) - - if rna_ui is None: - return None - - try: - return rna_ui[prop] - except: - rna_ui[prop] = {} - return rna_ui[prop] - - -def rna_idprop_ui_prop_clear(item, prop, *, remove=True): - rna_ui = rna_idprop_ui_get(item, create=False) - - if rna_ui is None: - return - - try: - del rna_ui[prop] - except KeyError: - pass - if remove and len(item.keys()) == 1: - rna_idprop_ui_del(item) +def rna_idprop_ui_prop_clear(item, prop): + ui_data = item.id_properties_ui(prop) + ui_data.clear() def rna_idprop_context_value(context, context_member, property_type): @@ -106,8 +65,7 @@ def rna_idprop_context_value(context, context_member, property_type): def rna_idprop_has_properties(rna_item): keys = rna_item.keys() - nbr_props = len(keys) - return (nbr_props > 1) or (nbr_props and '_RNA_UI' not in keys) + return bool(keys) def rna_idprop_value_to_python(value): @@ -126,31 +84,8 @@ def rna_idprop_value_item_type(value): def rna_idprop_ui_prop_default_set(item, prop, value): - defvalue = None - try: - prop_type, is_array = rna_idprop_value_item_type(item[prop]) - - if prop_type in {int, float, str}: - 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, create=True) - rna_ui["default"] = defvalue - else: - rna_ui = rna_idprop_ui_prop_get(item, prop) - if rna_ui: - rna_ui.pop("default", None) - - return defvalue + ui_data = item.id_properties_ui(prop) + ui_data.update(default=value) def rna_idprop_ui_create( @@ -163,7 +98,7 @@ def rna_idprop_ui_create( ): """Create and initialize a custom property with limits, defaults and other settings.""" - proptype, is_array = rna_idprop_value_item_type(default) + proptype, _ = rna_idprop_value_item_type(default) # Sanitize limits if proptype is bool: @@ -180,35 +115,22 @@ def rna_idprop_ui_create( rna_idprop_ui_prop_update(item, prop) - # Clear the UI settings - rna_ui_group = rna_idprop_ui_get(item, create=True) - rna_ui_group[prop] = {} - rna_ui = rna_ui_group[prop] - - # Assign limits and default - if proptype in {int, float, bool}: - # The type must be exactly the same - rna_ui["min"] = proptype(min) - rna_ui["soft_min"] = proptype(soft_min) - rna_ui["max"] = proptype(max) - rna_ui["soft_max"] = proptype(soft_max) - - if default and (not is_array or any(default)): - rna_ui["default"] = default - - if is_array and subtype and subtype != 'NONE': - rna_ui["subtype"] = subtype - - # Assign other settings - if description is not None: - rna_ui["description"] = description + # Update the UI settings. + ui_data = item.id_properties_ui(prop) + ui_data.update( + subtype=subtype, + min=min, + max=max, + soft_min=soft_min, + soft_max=soft_max, + description=description, + default=default, + ) prop_path = rna_idprop_quote_path(prop) item.property_overridable_library_set(prop_path, overridable) - return rna_ui - def draw(layout, context, context_member, property_type, *, use_edit=True): @@ -254,10 +176,6 @@ def draw(layout, context, context_member, property_type, *, use_edit=True): flow = layout.grid_flow(row_major=False, columns=0, even_columns=True, even_rows=False, align=True) for key, val in items: - - if key == '_RNA_UI': - continue - is_rna = (key in rna_properties) # only show API defined for developers |