diff --git a/src/ifcblenderexport/blenderbim/__init__.py b/src/ifcblenderexport/blenderbim/__init__.py index 67ed194581..fc75125dbb 100644 --- a/src/ifcblenderexport/blenderbim/__init__.py +++ b/src/ifcblenderexport/blenderbim/__init__.py @@ -57,6 +57,7 @@ classes = ( operator.UnassignClassification, operator.RemoveClassification, operator.FetchLibraryInformation, + operator.FetchExternalMaterial, prop.BIMProperties, prop.BIMLibrary, prop.MapConversion, diff --git a/src/ifcblenderexport/blenderbim/operator.py b/src/ifcblenderexport/blenderbim/operator.py index d8db6720df..e015a70723 100644 --- a/src/ifcblenderexport/blenderbim/operator.py +++ b/src/ifcblenderexport/blenderbim/operator.py @@ -1,3 +1,4 @@ +import os import bpy import time import json @@ -641,6 +642,37 @@ class RemoveClassification(bpy.types.Operator): return {'FINISHED'} +class FetchExternalMaterial(bpy.types.Operator): + bl_idname = 'bim.fetch_external_material' + bl_label = 'Fetch External Material' + + def execute(self, context): + location = bpy.context.active_object.active_material.BIMMaterialProperties.location + if location[-6:] != '.mpass': + return {'FINISHED'} + with open(location) as f: + self.material_pass = json.load(f) + if bpy.context.scene.render.engine == 'BLENDER_EEVEE' \ + and 'eevee' in self.material_pass: + self.fetch_eevee_or_cycles('eevee') + elif bpy.context.scene.render.engine == 'CYCLES' \ + and 'cycles' in self.material_pass: + self.fetch_eevee_or_cycles('cycles') + return {'FINISHED'} + + def fetch_eevee_or_cycles(self, name): + identification = bpy.context.active_object.active_material.BIMMaterialProperties.identification + bpy.ops.wm.link( + filename=identification, + directory=os.path.join(self.material_pass[name]['uri'], 'Material') + ) + for material in bpy.data.materials: + if material.name == identification \ + and material.library: + bpy.context.active_object.material_slots[0].material = material + return + + class FetchLibraryInformation(bpy.types.Operator): bl_idname = 'bim.fetch_library_information' bl_label = 'Fetch Library Information' diff --git a/src/ifcblenderexport/blenderbim/ui.py b/src/ifcblenderexport/blenderbim/ui.py index 3a997ade1e..8844865b35 100644 --- a/src/ifcblenderexport/blenderbim/ui.py +++ b/src/ifcblenderexport/blenderbim/ui.py @@ -7,7 +7,7 @@ class BIM_PT_object(Panel): bl_space_type = 'PROPERTIES' bl_region_type = 'WINDOW' bl_context = 'object' - + @classmethod def poll(cls, context): return context.active_object is not None and hasattr(context.active_object, "BIMObjectProperties") @@ -143,6 +143,9 @@ class BIM_PT_material(Panel): row = layout.row() row.prop(props, 'name') + row = layout.row() + row.operator('bim.fetch_external_material') + class BIM_PT_gis(Panel): bl_label = 'IFC Georeferencing'