mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-19 06:39:13 +00:00
Refactor material creation to follow IFC data, not rely on ifcopenshell geom, since we want to retain a bunch of smart IFC data later
This commit is contained in:
@@ -3,6 +3,55 @@ from .ifcopenshell import geom
|
|||||||
import bpy
|
import bpy
|
||||||
import mathutils
|
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():
|
class IfcImporter():
|
||||||
def __init__(self, ifc_import_settings):
|
def __init__(self, ifc_import_settings):
|
||||||
self.ifc_import_settings = ifc_import_settings
|
self.ifc_import_settings = ifc_import_settings
|
||||||
@@ -13,6 +62,8 @@ class IfcImporter():
|
|||||||
self.elements = {}
|
self.elements = {}
|
||||||
self.meshes = {}
|
self.meshes = {}
|
||||||
|
|
||||||
|
self.material_creator = MaterialCreator()
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
self.load_file()
|
self.load_file()
|
||||||
self.create_project()
|
self.create_project()
|
||||||
@@ -69,13 +120,10 @@ class IfcImporter():
|
|||||||
else:
|
else:
|
||||||
print('we reused a mesh!')
|
print('we reused a mesh!')
|
||||||
|
|
||||||
materials = shape.geometry.materials
|
for association in element.HasAssociations:
|
||||||
for material in materials:
|
if association.is_a('IfcRelAssociatesMaterial'):
|
||||||
if material.has_diffuse:
|
self.material_creator.set_mesh(mesh)
|
||||||
alpha = 1.
|
self.material_creator.create(association.RelatingMaterial)
|
||||||
blender_material = bpy.data.materials.new('mymaterial')
|
|
||||||
blender_material.diffuse_color = material.diffuse + (alpha,)
|
|
||||||
mesh.materials.append(blender_material)
|
|
||||||
|
|
||||||
object = bpy.data.objects.new(self.get_name(element), mesh)
|
object = bpy.data.objects.new(self.get_name(element), mesh)
|
||||||
m = shape.transformation.matrix.data
|
m = shape.transformation.matrix.data
|
||||||
|
|||||||
Reference in New Issue
Block a user