[Bf-blender-cvs] [8b9a3b94fc1] master: Refactor IDProperty UI data storage

Hans Goudey noreply at git.blender.org
Fri Aug 27 15:27:33 CEST 2021


Commit: 8b9a3b94fc148d197b137aa892855c60db2783e6
Author: Hans Goudey
Date:   Fri Aug 27 08:27:24 2021 -0500
Branches: master
https://developer.blender.org/rB8b9a3b94fc148d197b137aa892855c60db2783e6

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

===================================================================

M	release/scripts/modules/rna_prop_ui.py
M	release/scripts/startup/bl_operators/object.py
M	release/scripts/startup/bl_operators/wm.py
M	release/scripts/startup/keyingsets_builtins.py
M	source/blender/blenkernel/BKE_idprop.h
M	source/blender/blenkernel/intern/idprop.c
M	source/blender/blenloader/intern/versioning_300.c
M	source/blender/io/alembic/exporter/abc_custom_props.cc
M	source/blender/makesdna/DNA_ID.h
M	source/blender/makesrna/RNA_access.h
M	source/blender/makesrna/intern/rna_access.c
M	source/blender/makesrna/intern/rna_internal_types.h
M	source/blender/modifiers/intern/MOD_nodes.cc
M	source/blender/python/generic/CMakeLists.txt
M	source/blender/python/generic/idprop_py_api.c
A	source/blender/python/generic/idprop_py_ui_api.c
A	source/blender/python/generic/idprop_py_ui_api.h
M	source/blender/python/intern/bpy.c
M	source/blender/python/intern/bpy_rna.c
M	tests/python/bl_pyapi_idprop.py

===================================================================

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
diff --git a/release/scripts/startup/bl_operators/object.py b/release/scripts/startup/bl_operators/object.py
index d61bed71cab..df37db40102 100644
--- a/release/scripts/startup/bl_operators/object.py
+++ b/release/scripts/startup/bl_operators/object.py
@@ -970,7 +970,7 @@ class OBJECT_OT_assign_property_defaults(Operator):
     def assign_defaults(obj):
         from rna_prop_ui import rna_idprop_ui_prop_default_set
 
-        rna_properties = {'_RNA_UI'} | {prop.identifier for prop in obj.bl_rna.properties if prop.is_runtime}
+        rna_properties = {prop.identifier for prop in obj.bl_rna.properties if prop.is_runtime}
 
         for prop, value in obj.items():
             if prop not in rna_properties:
diff --git a/release/scripts/startup/bl_operators/wm.py b/release/scripts/startup/bl_operators/wm.py
index 6a3830ad1e4..989c067efec 100644
--- a/release/scripts/startup/bl_operators/wm.py
+++ b/release/scripts/startup/bl_operators/wm.py
@@ -1388,10 +1388,8 @@ class WM_OT_properties_edit(Operator):
 
     def execute(self, context):
         from rna_prop_ui import (
-            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,
         )
 
@@ -1431,27 +1429,35 @@ class WM_OT_properties_edit(Operator):
         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 self.use_soft_limits:
-                prop_ui["soft_min"] = prop_type(self.soft_min)
-                prop_ui["soft_max"] = prop_type(self.soft_max)
-            else:
-                prop_ui["soft_min"] = prop_type(self.min)
-                prop_ui["soft_max"] = prop_type(self.max)
-
-        if prop_type == float and is_array and self.subtype != 'NONE':
-            prop_ui["subtype"] = self.subtype
-        else:
-            prop_ui.pop("subtype", None)
-
-        prop_ui["description"] = self.description
-
-        rna_idprop_ui_prop_default_set(item, prop, default_eval)
+        ui_data = item.id_properties_ui(prop)
+        ui_data.update(subtype=self.subtype, description=self.description)
+
+        if prop_type == int:
+            if type(default_eval) == str:
+                self.report({'WARNING'}, "Could not evaluate number from default value")
+                default_eval = None
+            elif hasattr(default_eval, "__len__"):
+                default_eval = [int(round(value)) for value in default_eval]
+            ui_data.update(
+                min=int(round(self.min)),
+                max=int(round(self.max)),
+                soft_min=int(round(self.soft_min)),
+                soft_max=int(round(self.soft_max)),
+                default=default_eval,
+            )
+        elif prop_type == float:
+            if type(default_eval) == str:
+                self.report({'WARNING'}, "Could not evaluate number from default value")
+                default_eval = None
+            ui_data.update(
+                min=self.min,
+                max=self.max,
+                soft_min=self.soft_min,
+                soft_max=self.soft_max,
+                default=default_eval,
+            )
+        elif prop_type == str:
+            ui_data.update(default=self.default)
 
         # If we have changed the type of the property, update its potential anim curves!
         if prop_type_old != prop_type_new:
@@ -1492,7 +1498,6 @@ class WM_OT_properties_edit(Operator):
 
     def invoke(self, context, _event):
         from rna_prop_ui import (
-            rna_idprop_ui_prop_get,
             rna_idprop_value_to_python,
             rna_idprop_value_item_type
         )
@@ -1526,28 +1531,22 @@ class WM_OT_properties_edit(Operator):
             self.default = ""
 
         # setup defaults
-        prop_ui = rna_idprop_ui_prop_get(item, prop, create=False)
-        if prop_ui:
-            self.min = prop_ui.get("min", -1000000000)
-            self.max = prop_ui.get("max", 1000000000)
-            self.description = prop_ui.get("description", "")
-
-            defval = prop_ui.get("default", None)
-            if defval is not None:
-                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)
+        ui_data = item.id_properties_ui(prop)
+        rna_data = ui_data.as_dict()
+        self.subtype =  rna_data["subtype"]
+        if prop_type in {int, float}:
+            self.min = rna_data["min"]
+            self.max = rna_data["max"]
+            self.soft_min = rna_data["soft_min"]
+            self.soft_max = rna_data["soft_max"]
             self.use_soft_limits = (
                 self.min != self.soft_min or
                 self.max != self.soft_max
             )
+        if prop_type in {int, float, str}:
+            self.default = str(rna_data["default"])
 
-            subtype = prop_ui.get("subtype", None)
-        else:
-            subtype = None
-
-        

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list