[Bf-blender-cvs] [aef08fda3ad] master: Custom Properties: officially support int and float arrays in the UI.

Alexander Gavrilov noreply at git.blender.org
Tue Aug 13 16:15:10 CEST 2019


Commit: aef08fda3ade2d0223b77d4c9c0dd5e9fcabe7b2
Author: Alexander Gavrilov
Date:   Sat Aug 10 14:42:14 2019 +0300
Branches: master
https://developer.blender.org/rBaef08fda3ade2d0223b77d4c9c0dd5e9fcabe7b2

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

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

M	release/scripts/modules/rna_prop_ui.py
M	release/scripts/startup/bl_operators/wm.py
M	source/blender/depsgraph/intern/builder/deg_builder_nodes.cc
M	source/blender/depsgraph/intern/builder/deg_builder_relations.cc
M	source/blender/editors/interface/interface_layout.c
M	source/blender/makesrna/intern/rna_access.c

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

diff --git a/release/scripts/modules/rna_prop_ui.py b/release/scripts/modules/rna_prop_ui.py
index 9d1ba035b3e..b568f9835a0 100644
--- a/release/scripts/modules/rna_prop_ui.py
+++ b/release/scripts/modules/rna_prop_ui.py
@@ -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:
diff --git a/release/scripts/startup/bl_operators/wm.py b/release/scripts/startup/bl_operators/wm.py
index 5cc4b773b54..97a242772a4 100644
--- a/release/scripts/startup/bl_operators/wm.py
+++ b/release/scripts/startup/bl_operators/wm.py
@@ -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)
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_propert

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list