Issue with renaming My Storey

Hi,
I am trying to rename the default storey that is created when initializing an IFC project in Blender, and I wish to do this through running a Python script. However, whether this succeeds or not seems to depend on if the script is run from within the Blender UI or if the script is provided when running Blender from the command line.

I have an example file which should reproduce the problem. If the following snippet is run from the Scripting tab in Blender, the storey seems to be renamed properly. What I try to do is to do the renaming, set the storey as the (current) parent colelction, assign ifc classes to some objects, and link the ifc components to the storey:

import bpy

bpy.ops.bim.create_project()
bpy.ops.mesh.primitive_cube_add(
    size=2, enter_editmode=False, align="WORLD", location=(0, 0, 0), scale=(1, 1, 1)
)

storey_name = "Floor 1"

# Rename collection / Storey object
old_collection_name = "IfcBuildingStorey/My Storey"
if old_collection_name in bpy.data.collections and bpy.data.objects:
    collection = bpy.data.collections.get(old_collection_name)
    collection.name = f"IfcBuildingStorey/{storey_name}"

    storey = bpy.data.objects[old_collection_name]
    storey.name = f"IfcBuildingStorey/{storey_name}"
else:
    raise ValueError("Couldn't edit storey name, default storey not found.")

parent_collection_name = f"IfcBuildingStorey/{storey_name}"
parent_collection = bpy.data.collections.get(parent_collection_name)
if not parent_collection:
    raise ValueError(f"Parent collection '{parent_collection_name}' not found.")

# Construct Ifc components
bpy.ops.object.select_all(action="DESELECT")
for obj in bpy.data.objects:
    if obj.type == "MESH":
        obj.select_set(True)

        bpy.ops.bim.assign_class(ifc_class="IfcBuildingElementProxy")

        # Unlink from any other collection (like 'IfcProject/My Project') before adding to storey
        for collection in obj.users_collection:
            collection.objects.unlink(obj)
        parent_collection.objects.link(obj)

        obj.select_set(False)  # Deselect the current object

This seems to work fine, but if I add the following lines to the Python file (saving the code as debug_renaming.py)

# Export the loaded model to IFC format
import sys

argv = sys.argv[sys.argv.index("--") + 1 :]
output_path = argv[0]
bpy.ops.export_ifc.bim(filepath=str(output_path), should_save_as=True, save_as_invoked=True)

and run from the command line

$ Blender -b -P debug_renaming.py -- cube_CLI.ifc

the cube will be linked to IfcBuildingStorey/My Storey instead of IfcBuildingStorey/Floor 1

I don't have any clue what is going on here, so help is much appreciated. If the renaming itself is done incorrectly I would love to hear the better way.

Using Blender 4.0.0, BlenderBIM 0.0.231104

Comments

  • Hi! Changing name of the story is much simpler, just change the name for the object:

    obj = bpy.data.objects["IfcBuildingStorey/My Storey"]
    obj.name = "test"
    obj.name # 'IfcBuildingStorey/test'
    
    steverugipederlh
  • edited December 2023

    @Andrej730
    Nice, this works in the script run from the UI. Still, the code

    storey_name = "Floor 1"
    if "IfcBuildingStorey/My Storey" in bpy.data.objects:
        storey = bpy.data.objects["IfcBuildingStorey/My Storey"]
        storey.name = storey_name
    else:
        raise ValueError("Couldn't edit storey name, default storey not found.")
    
    parent_collection_name = f"IfcBuildingStorey/{storey_name}"
    parent_collection = bpy.data.collections.get(parent_collection_name)
    if not parent_collection:
        raise ValueError(f"Parent collection '{parent_collection_name}' not found.")
    

    raises the last ValueError

    ValueError: Parent collection 'IfcBuildingStorey/Floor 1' not found.
    

    when running the script from the CLI...

  • Might be more suited for a Github issue: #4140

Sign In or Register to comment.