Fix regression where you couldn't unlink an object that already had a broken element link, and bug where you couldn't unlink a spatial element or similar with its own collection.

This commit is contained in:
Dion Moult
2024-01-23 12:27:09 +11:00
parent 29b84078ac
commit b5261e029e
@@ -177,48 +177,45 @@ class UnlinkObject(bpy.types.Operator):
bl_label = "Unlink Object" bl_label = "Unlink Object"
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
obj: bpy.props.StringProperty() obj: bpy.props.StringProperty()
should_delete: bpy.props.BoolProperty(name="Should Delete", default=True) should_delete: bpy.props.BoolProperty(name="Delete IFC Element", default=True)
def execute(self, context): def execute(self, context):
return IfcStore.execute_ifc_operator(self, context) return IfcStore.execute_ifc_operator(self, context)
def _execute(self, context): def _execute(self, context):
self.file = IfcStore.get_file()
if self.obj: if self.obj:
objects = [bpy.data.objects.get(self.obj)] objects = [bpy.data.objects.get(self.obj)]
else: else:
objects = context.selected_objects objects = context.selected_objects
for obj in objects: for obj in objects:
was_active_object = obj == context.active_object was_active_object = obj == context.active_object
object_name = obj.name
if obj in IfcStore.edited_objs:
IfcStore.edited_objs.remove(obj)
element = tool.Ifc.get_entity(obj) element = tool.Ifc.get_entity(obj)
if element: if element and self.should_delete:
if self.should_delete: object_name = obj.name
obj_copy = obj.copy()
# copy object data, so it won't be removed by `delete_ifc_object` # Copy object, so it won't be removed by `delete_ifc_object`
if obj.data: obj_copy = obj.copy()
obj_copy.data = obj.data.copy() if obj.data:
if obj.type == "MESH": obj_copy.data = obj.data.copy()
obj_copy.data.BIMMeshProperties.ifc_definition_id = 0
for collection in obj.users_collection: tool.Geometry.delete_ifc_object(obj)
collection.objects.link(obj_copy)
tool.Geometry.delete_ifc_object(obj)
obj = obj_copy
if obj in IfcStore.edited_objs:
IfcStore.edited_objs.remove(obj)
tool.Ifc.unlink(obj=obj)
for material_slot in obj.material_slots: obj = obj_copy
if material_slot.material: obj.name = object_name
material_slot.material = material_slot.material.copy()
blenderbim.core.style.unlink_style(tool.Ifc, tool.Style, obj=material_slot.material) tool.Root.unlink_object(obj)
blenderbim.core.material.unlink_material(tool.Ifc, obj=material_slot.material) for collection in obj.users_collection:
if "Ifc" in object_name and "/" in object_name: # Reset collection because its original collection may be removed too.
obj.name = object_name.split("/", 1)[1] collection.objects.unlink(obj)
if was_active_object: bpy.context.scene.collection.objects.link(obj)
tool.Blender.set_active_object(obj)
if was_active_object:
tool.Blender.set_active_object(obj)
return {"FINISHED"} return {"FINISHED"}
def draw(self, context): def draw(self, context):