From 76e4ec006920fdad2144d67796eff20b062284b3 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Fri, 11 Jul 2025 15:37:34 +0500 Subject: [PATCH] bim.select_uri_attribute - simplify data_path eval --- src/bonsai/bonsai/bim/helper.py | 2 +- src/bonsai/bonsai/bim/module/pset/ui.py | 2 +- src/bonsai/bonsai/bim/operator.py | 31 ++++++++++--------------- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/bonsai/bonsai/bim/helper.py b/src/bonsai/bonsai/bim/helper.py index 8c683607df..7d6b7b14b1 100644 --- a/src/bonsai/bonsai/bim/helper.py +++ b/src/bonsai/bonsai/bim/helper.py @@ -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") diff --git a/src/bonsai/bonsai/bim/module/pset/ui.py b/src/bonsai/bonsai/bim/module/pset/ui.py index c1215d48a0..d09bfe072d 100644 --- a/src/bonsai/bonsai/bim/module/pset/ui.py +++ b/src/bonsai/bonsai/bim/module/pset/ui.py @@ -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: diff --git a/src/bonsai/bonsai/bim/operator.py b/src/bonsai/bonsai/bim/operator.py index 45e27d31ec..301431a146 100644 --- a/src/bonsai/bonsai/bim/operator.py +++ b/src/bonsai/bonsai/bim/operator.py @@ -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"}