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
path: root/tests
diff options
context:
space:
mode:
authorHans Goudey <h.goudey@me.com>2021-08-27 16:27:24 +0300
committerHans Goudey <h.goudey@me.com>2021-08-27 16:27:24 +0300
commit8b9a3b94fc148d197b137aa892855c60db2783e6 (patch)
tree0afd1a3538e76a029c0d6365f5bc06d681da92d9 /tests
parent3f5e0f7d91e0e13870d98fe721a871e6be210489 (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 'tests')
-rw-r--r--tests/python/bl_pyapi_idprop.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/python/bl_pyapi_idprop.py b/tests/python/bl_pyapi_idprop.py
index 38cd9d04a6b..bdf625c1c2e 100644
--- a/tests/python/bl_pyapi_idprop.py
+++ b/tests/python/bl_pyapi_idprop.py
@@ -246,6 +246,69 @@ class TestBufferProtocol(TestHelper, unittest.TestCase):
self.assertEqual(list(view1), list(view2))
self.assertEqual(view1.tobytes(), view2.tobytes())
+class TestRNAData(TestHelper, unittest.TestCase):
+
+ def test_custom_properties_none(self):
+ bpy.data.objects.new("test", None)
+ test_object = bpy.data.objects["test"]
+
+ # Access default RNA data values.
+ test_object.id_properties_clear()
+ test_object["test_prop"] = 0.5
+ ui_data_test_prop = test_object.id_properties_ui("test_prop")
+
+ rna_data = ui_data_test_prop.as_dict()
+ self.assertTrue("min" in rna_data)
+ self.assertLess(rna_data["min"], -10000.0)
+ self.assertEqual(rna_data["subtype"], "NONE")
+ self.assertGreater(rna_data["soft_max"], 10000.0)
+
+ # Change RNA data values.
+ ui_data_test_prop.update(subtype="TEMPERATURE", min=0, soft_min=0.1)
+ rna_data = ui_data_test_prop.as_dict()
+ self.assertEqual(rna_data["min"], 0)
+ self.assertEqual(rna_data["soft_min"], 0.1)
+ self.assertEqual(rna_data["subtype"], "TEMPERATURE")
+
+ # Copy RNA data values from one property to another.
+ test_object["test_prop_2"] = 11.7
+ ui_data_test_prop_2 = test_object.id_properties_ui("test_prop_2")
+ ui_data_test_prop_2.update_from(ui_data_test_prop)
+ rna_data = ui_data_test_prop_2.as_dict()
+ self.assertEqual(rna_data["min"], 0)
+ self.assertEqual(rna_data["soft_min"], 0.1)
+ self.assertEqual(rna_data["subtype"], "TEMPERATURE")
+ self.assertGreater(rna_data["soft_max"], 10000.0)
+
+ # Copy RNA data values to another object's property.
+ bpy.data.objects.new("test_2", None)
+ test_object_2 = bpy.data.objects["test_2"]
+ test_object_2["test_prop_3"] = 20.1
+ ui_data_test_prop_3 = test_object_2.id_properties_ui("test_prop_3")
+ ui_data_test_prop_3.update_from(ui_data_test_prop_2)
+ rna_data = ui_data_test_prop_3.as_dict()
+ self.assertEqual(rna_data["min"], 0)
+ self.assertEqual(rna_data["soft_min"], 0.1)
+ self.assertEqual(rna_data["subtype"], "TEMPERATURE")
+ self.assertGreater(rna_data["soft_max"], 10000.0)
+
+ # Test RNA data for string property.
+ test_object.id_properties_clear()
+ test_object["test_string_prop"] = "Hello there!"
+ ui_data_test_prop_string = test_object.id_properties_ui("test_string_prop")
+ ui_data_test_prop_string.update(default="Goodbye where?")
+ rna_data = ui_data_test_prop_string.as_dict()
+ self.assertEqual(rna_data["default"], "Goodbye where?")
+
+ # Test RNA data for array property.
+ test_object.id_properties_clear()
+ test_object["test_array_prop"] = [1, 2, 3]
+ ui_data_test_prop_array = test_object.id_properties_ui("test_array_prop")
+ ui_data_test_prop_array.update(default=[1, 2])
+ rna_data = ui_data_test_prop_array.as_dict()
+ self.assertEqual(rna_data["default"], [1, 2])
+
+
if __name__ == '__main__':
import sys