From a9397e23badee42aa3eb6bb5f74d25779a654301 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Sat, 12 Oct 2019 20:09:41 +1100 Subject: [PATCH] Refactor material creation to follow IFC data, not rely on ifcopenshell geom, since we want to retain a bunch of smart IFC data later --- .../io_export_ifc/import_ifc.py | 62 ++++++++++++++++--- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/src/ifcblenderexport/io_export_ifc/import_ifc.py b/src/ifcblenderexport/io_export_ifc/import_ifc.py index da6f146113..10413283f8 100644 --- a/src/ifcblenderexport/io_export_ifc/import_ifc.py +++ b/src/ifcblenderexport/io_export_ifc/import_ifc.py @@ -3,6 +3,55 @@ from .ifcopenshell import geom import bpy import mathutils +class MaterialCreator(): + def __init__(self): + self.mesh = None + self.materials = {} + + def set_mesh(self, mesh): + self.mesh = mesh + + def create(self, material_select): + if material_select.is_a('IfcMaterialDefinition'): + return self.create_definition(material_select) + + def create_definition(self, material): + if material.is_a('IfcMaterial'): + self.create_single(material) + + def create_single(self, material): + if material.Name not in self.materials: + self.create_new_single(material) + return self.assign_material_to_mesh(self.materials[material.Name]) + + def create_new_single(self, material): + self.materials[material.Name] = bpy.data.materials.new(material.Name) + if not material.HasRepresentation \ + or not material.HasRepresentation[0].Representations: + return + for representation in material.HasRepresentation[0].Representations: + if not representation.Items: + continue + for item in representation.Items: + if not item.is_a('IfcStyledItem'): + continue + for style in item.Styles: + if not style.is_a('IfcSurfaceStyle'): + continue + for surface_style in style.Styles: + if surface_style.is_a('IfcSurfaceStyleShading'): + alpha = 1. + if surface_style.Transparency: + alpha = 1 - surface_style.Transparency + self.materials[material.Name].diffuse_color = ( + surface_style.SurfaceColour.Red, + surface_style.SurfaceColour.Green, + surface_style.SurfaceColour.Blue, + alpha) + + def assign_material_to_mesh(self, material): + self.mesh.materials.append(material) + class IfcImporter(): def __init__(self, ifc_import_settings): self.ifc_import_settings = ifc_import_settings @@ -13,6 +62,8 @@ class IfcImporter(): self.elements = {} self.meshes = {} + self.material_creator = MaterialCreator() + def execute(self): self.load_file() self.create_project() @@ -69,13 +120,10 @@ class IfcImporter(): else: print('we reused a mesh!') - materials = shape.geometry.materials - for material in materials: - if material.has_diffuse: - alpha = 1. - blender_material = bpy.data.materials.new('mymaterial') - blender_material.diffuse_color = material.diffuse + (alpha,) - mesh.materials.append(blender_material) + for association in element.HasAssociations: + if association.is_a('IfcRelAssociatesMaterial'): + self.material_creator.set_mesh(mesh) + self.material_creator.create(association.RelatingMaterial) object = bpy.data.objects.new(self.get_name(element), mesh) m = shape.transformation.matrix.data