Migrate MEPConnectElements args from object names to IFC GUIDs

MEPConnectElements took obj1_name/obj2_name (Blender object names),
which break when objects are renamed or replicated by array
duplication. Switch to obj1_guid/obj2_guid resolved via
ifc_file.by_guid, with by_guid RuntimeError surfaced as an operator
error rather than a stack trace. DrawPolylineProfile (the sole
in-tree caller) updates to pass GlobalIds.

Generated with the assistance of an AI coding tool.
This commit is contained in:
Gorgious56
2026-05-22 13:32:08 +02:00
parent ba5321fdfa
commit 6bde619fe6
2 changed files with 17 additions and 6 deletions
@@ -1157,7 +1157,8 @@ class DrawPolylineProfile(bpy.types.Operator, PolylineOperator, tool.Ifc.Operato
DumbProfileJoiner().join_V(profile2["obj"], profile1["obj"])
if connect_IfcFlowSegments:
bpy.ops.bim.mep_connect_elements(
obj1_name=profile1["obj"].name, obj2_name=profile2["obj"].name
obj1_guid=tool.Ifc.get_entity(profile1["obj"]).GlobalId,
obj2_guid=tool.Ifc.get_entity(profile2["obj"]).GlobalId,
)
def modal(self, context, event):
@@ -317,13 +317,23 @@ class MEPConnectElements(bpy.types.Operator, tool.Ifc.Operator):
bl_label = "Connect MEP Elements"
bl_description = "Connects two selected elements by their closest located ports and adjusts them"
bl_options = {"REGISTER", "UNDO"}
obj1_name: bpy.props.StringProperty(name="Object 1")
obj2_name: bpy.props.StringProperty(name="Object 2")
obj1_guid: bpy.props.StringProperty(name="Object 1 GlobalId")
obj2_guid: bpy.props.StringProperty(name="Object 2 GlobalId")
def _execute(self, context):
if self.obj1_name and self.obj2_name:
obj1 = bpy.data.objects.get(self.obj1_name)
obj2 = bpy.data.objects.get(self.obj2_name)
if self.obj1_guid and self.obj2_guid:
ifc_file = tool.Ifc.get()
try:
el1_lookup = ifc_file.by_guid(self.obj1_guid)
el2_lookup = ifc_file.by_guid(self.obj2_guid)
except RuntimeError:
self.report({"ERROR"}, "Could not resolve MEP elements from supplied GlobalIds.")
return {"CANCELLED"}
obj1 = tool.Ifc.get_object(el1_lookup)
obj2 = tool.Ifc.get_object(el2_lookup)
if not obj1 or not obj2:
self.report({"ERROR"}, "Supplied MEP elements have no Blender object bound.")
return {"CANCELLED"}
else:
if not context.selected_objects or len(context.selected_objects) != 2:
self.report({"ERROR"}, "Need to select 2 objects.")