Fix T91054: Editing group custom property gives error

This commit fixes the custom property edit operator for the the case of
editing group properties. Currently this isn't supported very well, the
data is converted to a string, but the operator shouldn't fail anyway.

This allows editing properties created like this:
C.object['abuse'] = {'parent' : ['child1', 'child2']}

These changes reflect some issues with the design of the operator.
Requiring guessing the type of the data does not work well at all, and
makes code more complicated. In the future this operator can be updated
to use a type drop-down.

Differential Revision: https://developer.blender.org/D12364
This commit is contained in:
Hans Goudey 2021-09-01 11:47:59 -05:00
parent 99c981fd06
commit 42546db490
Notes: blender-bot 2023-02-14 08:45:09 +01:00
Referenced by issue #91092, Render crash
Referenced by issue #91054, List of strings custom property cannot be edited
1 changed files with 13 additions and 8 deletions

View File

@ -1367,24 +1367,28 @@ class WM_OT_properties_edit(Operator):
}
def get_value_eval(self):
failed = False
try:
value_eval = eval(self.value)
# assert else None -> None, not "None", see T33431.
assert(type(value_eval) in {str, float, int, bool, tuple, list})
except:
failed = True
value_eval = self.value
return value_eval
return value_eval, failed
def get_default_eval(self):
failed = False
try:
default_eval = eval(self.default)
# assert else None -> None, not "None", see T33431.
assert(type(default_eval) in {str, float, int, bool, tuple, list})
except:
failed = True
default_eval = self.default
return default_eval
return default_eval, failed
def execute(self, context):
from rna_prop_ui import (
@ -1402,8 +1406,8 @@ class WM_OT_properties_edit(Operator):
self.report({'ERROR'}, "Direct execution not supported")
return {'CANCELLED'}
value_eval = self.get_value_eval()
default_eval = self.get_default_eval()
value_eval, value_failed = self.get_value_eval()
default_eval, default_failed = self.get_default_eval()
# First remove
item = eval("context.%s" % data_path)
@ -1414,7 +1418,7 @@ class WM_OT_properties_edit(Operator):
prop_type_old = type(item[prop_old])
# Deleting the property will also remove the UI data
# Deleting the property will also remove the UI data.
del item[prop_old]
# Reassign
@ -1458,7 +1462,7 @@ class WM_OT_properties_edit(Operator):
subtype=self.subtype,
description=self.description
)
elif prop_type == str and not is_array: # String arrays do not support UI data.
elif prop_type == str and not is_array and not default_failed: # String arrays do not support UI data.
ui_data = item.id_properties_ui(prop)
ui_data.update(
default=self.default,
@ -1531,7 +1535,8 @@ class WM_OT_properties_edit(Operator):
self.is_overridable_library = bool(is_overridable)
# default default value
prop_type, is_array = rna_idprop_value_item_type(self.get_value_eval())
value, value_failed = self.get_value_eval()
prop_type, is_array = rna_idprop_value_item_type(value)
if prop_type in {int, float}:
self.default = str(prop_type(0))
else:
@ -1551,7 +1556,7 @@ class WM_OT_properties_edit(Operator):
self.max != self.soft_max
)
self.default = str(rna_data["default"])
if prop_type == str and not is_array: # String arrays do not support UI data.
if prop_type == str and not is_array and not value_failed: # String arrays do not support UI data.
ui_data = item.id_properties_ui(prop)
rna_data = ui_data.as_dict()
self.subtype = rna_data["subtype"]