My task was to create a Python script using IfcOpenShell in Bonsai/Blender. The colours of walls change based on the volumes of the walls. The wall with the smallest volume is green and the wall with the largest volume is red.
The first thing I did was break down the task. Instead of randomly assigning colors, the script needed to find the volume of each wall and assign colors from red to green based on the volume.
- Loading the IFC file into the script using IfcStore.get_file()
- I used the method model.by_type("IfcWallStandardCase") to extract all wall objects from the IFC model.
-
For each wall, I retrieved its property sets (psets) using ifcopenshell.util.element.get_psets().
-
From these property sets, we accessed the 'Dimensions' aribute to retrieve the volume of each wall.
- I created two lists: one to store the volumes of each wall and another to store the Blender object along with its corresponding volume. This made to later assign colors based on volume.
- I identified the minimum and maximum volumes. These two values provided the range for mapping the wall volumes to a color gradient (from 0 to 1 = from green to red, color gradient is in middle)
-
With function, volume_to_color(), that normalized each wall’s volume between the smallest and largest volume. This function returned a color where green represented the smallest volume, and red represented the largest.
-
Min-max feature scaling: Feature scaling is used to bring all values into the range [0,1]. This is also called unity-based normalization. This can be generalized to restrict the range of values in the dataset between any arbitrary points a and b.
-
References: https://en.wikipedia.org/wiki/Normalization_(statistics)
- For each wall object, if the wall already had a material, I reused it. Otherwise, I created a new material and assigned it to the wall. The wall’s color was then set using mat.diffuse_color.
- Finally, I updated the scene in Blender to reflect the changes made to the wall materials using bpy.context.view_layer.update()
-
The walls with the smallest volumes were displayed in green, while the walls with the largest volumes were displayed in red.
-
And intermediate volumes were represented by colors transitioning from green to red.