Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Gavrilov <angavrilov@gmail.com>2019-08-10 14:42:14 +0300
committerAlexander Gavrilov <angavrilov@gmail.com>2019-08-13 17:13:19 +0300
commitaef08fda3ade2d0223b77d4c9c0dd5e9fcabe7b2 (patch)
treeddde62d4cdf7d9896f0c8ac8fe694cf197c3ce7b /release
parent6e7ea807e1e8aa0791a3c1d8875e74d64481973c (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 'release')
-rw-r--r--release/scripts/modules/rna_prop_ui.py51
-rw-r--r--release/scripts/startup/bl_operators/wm.py25
2 files changed, 62 insertions, 14 deletions
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)