bim.select_uri_attribute - simplify data_path eval

This commit is contained in:
Andrej730
2025-07-11 15:37:34 +05:00
parent df18124bec
commit 76e4ec0069
3 changed files with 14 additions and 21 deletions
+1 -1
View File
@@ -125,7 +125,7 @@ def draw_attribute(
if attribute.special_type == "URI":
op = layout.operator("bim.select_uri_attribute", text="", icon="FILE_FOLDER")
op.data_path = attribute.path_from_id("string_value")
op.attribute_data_path = tool.Blender.get_full_data_path(attribute)
elif attribute.special_type in ("DATE", "DATETIME"):
op = layout.operator("bim.datepicker", text="", icon="TIME")
op.target_prop = attribute.path_from_id("string_value")
+1 -1
View File
@@ -62,7 +62,7 @@ def draw_single_property(prop: IfcProperty, layout: bpy.types.UILayout, copy_ope
)
if prop.metadata.special_type == "URI":
op = layout.operator("bim.select_uri_attribute", text="", icon="FILE_FOLDER")
op.data_path = prop.metadata.path_from_id("string_value")
op.attribute_data_path = tool.Blender.get_full_data_path(prop.metadata)
if prop.metadata.is_optional:
layout.prop(prop.metadata, "is_null", icon="RADIOBUT_OFF" if prop.metadata.is_null else "RADIOBUT_ON", text="")
if copy_operator:
+12 -19
View File
@@ -156,27 +156,20 @@ class SelectURIAttribute(bpy.types.Operator, ImportHelper):
bl_label = "Select URI Attribute"
bl_options = {"REGISTER", "UNDO"}
bl_description = "Select a local file"
data_path: bpy.props.StringProperty(name="Data Path")
use_relative_path: bpy.props.BoolProperty(name="Use Relative Path", default=False)
attribute_data_path: bpy.props.StringProperty(name="Data Path") # pyright: ignore[reportRedeclaration]
"""Full data path to Attribute."""
use_relative_path: bpy.props.BoolProperty( # pyright: ignore[reportRedeclaration]
name="Use Relative Path",
default=False,
)
if TYPE_CHECKING:
attribute_data_path: str
use_relative_path: bool
def execute(self, context):
# data_path contains the latter half of the path to the string_value property
# I have no idea how to find out the former half, so let's just use brute force.
data_path = self.data_path.replace(".string_value", "")
attribute = None
try:
attribute = eval(f"bpy.context.scene.{data_path}")
except:
try:
attribute = eval(f"bpy.context.active_object.{data_path}")
except:
try:
attribute = eval(f"bpy.context.active_object.active_material.{data_path}")
except:
# Do you know a better way?
pass
if attribute:
attribute.string_value = tool.Ifc.get_uri(self.filepath, use_relative_path=self.use_relative_path)
attribute: Attribute = eval(self.attribute_data_path)
attribute.string_value = tool.Ifc.get_uri(self.filepath, use_relative_path=self.use_relative_path)
return {"FINISHED"}