ID properties: Support enum values with items

Add support for enum values in ID properties.

This is needed for the "Menu Switch" node implementation (#113445) which
relies on ID properties for the top-level modifier UI.

Enums items can optionally be added to the UI data of integer
properties. Each property stores a full set of the enum items to keep
things simple.

Enum items can be added to properties using the `id_properties_ui`
function in the python API. A detailed example can be found in the
`bl_pyapi_idprop.py` test.

There is currently no support yet for editing enum items through the UI.
This is because the "Edit Property" feature is implemented entirely
through a single operator (`WM_OT_properties_edit`) and its properties.
Buttons to add/remove/move items would be operators changing another
operator's properties. A refactor of the custom properties UI is likely
required to make this work.

Pull Request: https://projects.blender.org/blender/blender/pulls/114362
This commit is contained in:
Lukas Tönne
2023-12-15 10:20:44 +01:00
parent 7346727cfc
commit 92cf9dd2f2
11 changed files with 416 additions and 8 deletions

View File

@@ -69,6 +69,11 @@ class TestIdPropertyCreation(TestHelper, unittest.TestCase):
self.assertEqual(self.id["a"], b"Hello World")
self.assertTrue(isinstance(self.id["a"], bytes))
def test_enum(self):
self.id["a"] = 5
self.assertEqual(self.id["a"], 5)
self.assertTrue(isinstance(self.id["a"], int))
def test_sequence_double_list(self):
mylist = [1.2, 3.4, 5.6]
self.id["a"] = mylist
@@ -349,6 +354,24 @@ class TestRNAData(TestHelper, unittest.TestCase):
rna_data = ui_data_test_prop_array.as_dict()
self.assertEqual(rna_data["default"], [1, 2])
# Test RNA data for enum property.
test_object.id_properties_clear()
test_object["test_enum_prop"] = 2
ui_data_test_prop_enum = test_object.id_properties_ui("test_enum_prop")
ui_data_test_prop_enum_items = [
("TOMATOES", "Tomatoes", "Solanum lycopersicum"),
("CUCUMBERS", "Cucumbers", "Cucumis sativus"),
("RADISHES", "Radishes", "Raphanus raphanistrum"),
]
ui_data_test_prop_enum.update(items=ui_data_test_prop_enum_items)
ui_data_test_prop_enum_items_full = [
("TOMATOES", "Tomatoes", "Solanum lycopersicum", 0, 0),
("CUCUMBERS", "Cucumbers", "Cucumis sativus", 0, 1),
("RADISHES", "Radishes", "Raphanus raphanistrum", 0, 2),
]
rna_data = ui_data_test_prop_enum.as_dict()
self.assertEqual(rna_data["items"], ui_data_test_prop_enum_items_full)
if __name__ == '__main__':
import sys