OSArch Community

Simple Wall Naming Script in Blender/Bbim

  1. J

    Hi,

    Below is a simple script my bff (chatgpt) put together to rename IfcWalls from standard Blender "Wall" and especially "Wall.01" etc as the .01+ suffix isn't reproduced in queries. It iterates through the walls list and identifies orientation from Blender's X and Y coordinates, and renames them either as NS (North South) or EW (East West). It also calculates the centroid, to the nearest metre, of each wall and suffixes the X and Y dimensions from project 0,0,0. Hence a wall may be renamed NS2_5x4y meaning it's wall number 2, it runs North-South and its centroid is 5m East and 4m North from project base. Copy and paste it into the Text Editor in Blender and run it. Hope it removes some drudgery of renaming when experimenting.

    import bpy
    
    import mathutils
    
    
    
    def calculate_centroid(obj):
    
        # Ensure the object is a mesh
    
        if obj.type != 'MESH':
    
            return None
    
    
    
        # Get the global coordinates of the vertices
    
        vertices = [obj.matrix_world @ vert.co for vert in obj.data.vertices]
    
    
    
        # Calculate the centroid
    
        centroid = mathutils.Vector((0.0, 0.0, 0.0))
    
        for vert in vertices:
    
            centroid += vert
    
        centroid /= len(vertices)
    
    
    
        return centroid
    
    
    
    def rename_ifc_walls():
    
        # Filter to get only IfcWall objects by name inspection
    
        ifc_walls = [obj for obj in bpy.context.scene.objects if "IfcWall" in obj.name]
    
    
    
        print(f"Found {len(ifc_walls)} IFC walls.")
    
    
    
        ew_counter = 1
    
        ns_counter = 1
    
    
    
        for wall in ifc_walls:
    
            if wall.type != 'MESH':
    
                print(f"Skipping non-mesh object: {wall.name}")
    
                continue
    
    
    
            # Calculate the centroid without modifying the object's origin
    
            centroid = calculate_centroid(wall)
    
            if centroid is None:
    
                print(f"Skipping object without vertices: {wall.name}")
    
                continue
    
    
    
            # Round coordinates to nearest whole meter
    
            x_rounded = round(centroid.x)
    
            y_rounded = round(centroid.y)
    
    
    
            # Calculate the range of coordinates for orientation
    
            vertices = [wall.matrix_world @ vert.co for vert in wall.data.vertices]
    
            x_coords = [vert.x for vert in vertices]
    
            y_coords = [vert.y for vert in vertices]
    
            x_range = max(x_coords) - min(x_coords)
    
            y_range = max(y_coords) - min(y_coords)
    
    
    
            # Determine the orientation and assign the new name
    
            if x_range > y_range:
    
                orientation = "EW"
    
                new_name = f"{orientation}{ew_counter}_{x_rounded}x{y_rounded}y"
    
                ew_counter += 1
    
            else:
    
                orientation = "NS"
    
                new_name = f"{orientation}{ns_counter}_{x_rounded}x{y_rounded}y"
    
                ns_counter += 1
    
    
    
            # Debugging: Print old and new names
    
            print(f"Renaming {wall.name} to {new_name}")
    
    
    
            # Rename the wall
    
            wall.name = new_name
    
    
    
        print("Walls have been renamed successfully.")
    
    
    
    # Run the renaming function
    
    rename_ifc_walls()
    
    
    
    # Force the Outliner to refresh
    
    for area in bpy.context.screen.areas:
    
        if area.type == 'OUTLINER':
    
            area.tag_redraw()
  2. K

    Expectations. I thought it would be hard to think about and enter the names of the walls each time so that they would be neat and orderly.

    Maybe I should include the IfcBuilding and IfcBuildingStory names, as if there are multiple buildings and stories.

  3. J

    @KoAra said:

    Expectations. I thought it would be hard to think about and enter the names of the walls each time so that they would be neat and orderly.

    Maybe I should include the IfcBuilding and IfcBuildingStory names, as if there are multiple buildings and stories.

    Those would be a good addition. I'm also tempted to add WallType just for easy reference, especially in the 3D Viewport top left corner. And maybe IfcSpace. It does seem that renaming like this is very customisable. Today, however, I need to puzzle out IfcPaint - it doesn't exist and I'm yet to find a good way to handle paint/finishes, especially for Qto. If anyone has a solution, I'd be most grateful to hear it.

Login or Register to reply.