Refactor Attribute class to provide agnostic value access

This commit is contained in:
Gorgious
2021-08-05 13:55:06 +02:00
parent e5f3b65e93
commit 5e3da807c3
+50 -27
View File
@@ -126,28 +126,8 @@ class StrProperty(PropertyGroup):
pass
def updateAttributeStringValue(self, context):
updateAttributeValue(self, self.string_value)
def updateAttributeBoolValue(self, context):
updateAttributeValue(self, self.bool_value)
def updateAttributeIntValue(self, context):
updateAttributeValue(self, self.int_value)
def updateAttributeFloatValue(self, context):
updateAttributeValue(self, self.float_value)
def updateAttributeEnumValue(self, context):
updateAttributeValue(self, self.enum_value)
def updateAttributeValue(self, value):
if value:
def updateAttributeValue(self, context):
if getattr(self, self.get_value_attr(), None): # Do not use get_value since it returns None if is_null is True
self.is_null = False
@@ -155,15 +135,58 @@ class Attribute(PropertyGroup):
name: StringProperty(name="Name")
description: StringProperty(name="Description")
data_type: StringProperty(name="Data Type")
string_value: StringProperty(name="Value", update=updateAttributeStringValue)
bool_value: BoolProperty(name="Value", update=updateAttributeBoolValue)
int_value: IntProperty(name="Value", update=updateAttributeIntValue)
float_value: FloatProperty(name="Value", update=updateAttributeFloatValue)
string_value: StringProperty(name="Value", update=updateAttributeValue)
bool_value: BoolProperty(name="Value", update=updateAttributeValue)
int_value: IntProperty(name="Value", update=updateAttributeValue)
float_value: FloatProperty(name="Value", update=updateAttributeValue)
is_null: BoolProperty(name="Is Null")
is_optional: BoolProperty(name="Is Optional")
enum_items: StringProperty(name="Value")
enum_value: EnumProperty(items=getAttributeEnumValues, name="Value", update=updateAttributeEnumValue)
enum_value: EnumProperty(items=getAttributeEnumValues, name="Value", update=updateAttributeValue)
def get_value(self):
if self.is_null:
return None
return getattr(self, self.get_value_attr(), None)
def get_value_default(self):
if self.data_type == "string":
return ""
elif self.data_type == "integer":
return 0
elif self.data_type == "float":
return 0.0
elif self.data_type == "boolean":
return False
elif self.data_type == "enum":
return "0"
def get_value_attr(self):
if self.data_type == "string":
return "string_value"
elif self.data_type == "boolean":
return "bool_value"
elif self.data_type == "integer":
return "int_value"
elif self.data_type == "float":
return "float_value"
elif self.data_type == "enum":
return "enum_value"
def set_value(self, value):
if isinstance(value, str):
self.data_type = "string"
elif isinstance(value, bool): # Make sure this is evaluated BEFORE integer
self.data_type = "boolean"
elif isinstance(value, int):
self.data_type = "integer"
elif isinstance(value, float):
self.data_type = "float"
else:
self.data_type = "enum"
value = str(value)
setattr(self, self.get_value_attr(), value)
class BIMProperties(PropertyGroup):
schema_dir: StringProperty(