mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 17:58:20 +00:00
prioritize external styles on ifc import
also found a way to switch shade graph for two materials without juggling active materials and active objects - it was necessary to make it work on import without adding temporary active object
This commit is contained in:
@@ -1485,22 +1485,11 @@ class IfcImporter:
|
|||||||
blender_material.BIMMaterialProperties.ifc_style_id = style.id()
|
blender_material.BIMMaterialProperties.ifc_style_id = style.id()
|
||||||
self.material_creator.styles[style.id()] = blender_material
|
self.material_creator.styles[style.id()] = blender_material
|
||||||
|
|
||||||
rendering_style = None
|
style_elements = tool.Style.get_style_elements(blender_material)
|
||||||
texture_style = None
|
if tool.Style.has_blender_external_style(style_elements):
|
||||||
|
blender_material.BIMStyleProperties.active_style_type = "External"
|
||||||
for surface_style in style.Styles:
|
else:
|
||||||
if surface_style.is_a() == "IfcSurfaceStyleShading":
|
blender_material.BIMStyleProperties.active_style_type = "Shading"
|
||||||
tool.Loader.create_surface_style_shading(blender_material, surface_style)
|
|
||||||
elif surface_style.is_a("IfcSurfaceStyleRendering"):
|
|
||||||
rendering_style = surface_style
|
|
||||||
tool.Loader.create_surface_style_rendering(blender_material, surface_style)
|
|
||||||
elif surface_style.is_a("IfcSurfaceStyleWithTextures"):
|
|
||||||
texture_style = surface_style
|
|
||||||
|
|
||||||
if rendering_style and texture_style:
|
|
||||||
tool.Loader.create_surface_style_with_textures(blender_material, rendering_style, texture_style)
|
|
||||||
|
|
||||||
tool.Style.record_shading(blender_material)
|
|
||||||
|
|
||||||
def place_objects_in_collections(self):
|
def place_objects_in_collections(self):
|
||||||
for ifc_definition_id, obj in self.added_data.items():
|
for ifc_definition_id, obj in self.added_data.items():
|
||||||
|
|||||||
@@ -267,8 +267,13 @@ class ActivateExternalStyle(bpy.types.Operator, tool.Ifc.Operator):
|
|||||||
bl_label = "Activate External Style"
|
bl_label = "Activate External Style"
|
||||||
bl_options = {"REGISTER", "UNDO", "INTERNAL"}
|
bl_options = {"REGISTER", "UNDO", "INTERNAL"}
|
||||||
|
|
||||||
|
material_name: bpy.props.StringProperty(name="Material Name", default="")
|
||||||
|
|
||||||
def _execute(self, context):
|
def _execute(self, context):
|
||||||
material = context.active_object.active_material
|
if not self.material_name:
|
||||||
|
material = context.active_object.active_material
|
||||||
|
else:
|
||||||
|
material = bpy.data.materials[self.material_name]
|
||||||
external_style = tool.Style.get_style_elements(material)["IfcExternallyDefinedSurfaceStyle"]
|
external_style = tool.Style.get_style_elements(material)["IfcExternallyDefinedSurfaceStyle"]
|
||||||
data_block_type, data_block = external_style.Identification.split("/")
|
data_block_type, data_block = external_style.Identification.split("/")
|
||||||
style_path = Path(tool.Ifc.resolve_uri(external_style.Location))
|
style_path = Path(tool.Ifc.resolve_uri(external_style.Location))
|
||||||
@@ -297,7 +302,7 @@ class ActivateExternalStyle(bpy.types.Operator, tool.Ifc.Operator):
|
|||||||
setattr(material, prop_name, getattr(db["data_block"], prop_name))
|
setattr(material, prop_name, getattr(db["data_block"], prop_name))
|
||||||
|
|
||||||
if material.use_nodes:
|
if material.use_nodes:
|
||||||
tool.Blender.copy_node_graph_to_active_object(context, db["data_block"])
|
tool.Blender.copy_node_graph(material, db["data_block"])
|
||||||
bpy.data.materials.remove(db["data_block"])
|
bpy.data.materials.remove(db["data_block"])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ def update_shading_styles(self, context):
|
|||||||
materials_to_objects[blender_material] = obj
|
materials_to_objects[blender_material] = obj
|
||||||
|
|
||||||
for mat, obj in materials_to_objects.items():
|
for mat, obj in materials_to_objects.items():
|
||||||
tool.Style.change_current_style_type(context, obj, mat, self.active_style_type)
|
tool.Style.change_current_style_type(mat, self.active_style_type)
|
||||||
|
|
||||||
|
|
||||||
class BIMStylesProperties(PropertyGroup):
|
class BIMStylesProperties(PropertyGroup):
|
||||||
@@ -79,11 +79,11 @@ class BIMStylesProperties(PropertyGroup):
|
|||||||
|
|
||||||
|
|
||||||
def update_shading_style(self, context):
|
def update_shading_style(self, context):
|
||||||
blender_material = context.active_object.active_material
|
blender_material = self.id_data
|
||||||
style_elements = tool.Style.get_style_elements(blender_material)
|
style_elements = tool.Style.get_style_elements(blender_material)
|
||||||
if self.active_style_type == "External":
|
if self.active_style_type == "External":
|
||||||
if "IfcExternallyDefinedSurfaceStyle" in style_elements:
|
if tool.Style.has_blender_external_style(style_elements):
|
||||||
bpy.ops.bim.activate_external_style()
|
bpy.ops.bim.activate_external_style(material_name=blender_material.name)
|
||||||
|
|
||||||
elif self.active_style_type == "Shading":
|
elif self.active_style_type == "Shading":
|
||||||
style_elements = tool.Style.get_style_elements(blender_material)
|
style_elements = tool.Style.get_style_elements(blender_material)
|
||||||
@@ -101,6 +101,7 @@ def update_shading_style(self, context):
|
|||||||
|
|
||||||
if rendering_style and texture_style:
|
if rendering_style and texture_style:
|
||||||
tool.Loader.create_surface_style_with_textures(blender_material, rendering_style, texture_style)
|
tool.Loader.create_surface_style_with_textures(blender_material, rendering_style, texture_style)
|
||||||
|
tool.Style.record_shading(blender_material)
|
||||||
|
|
||||||
|
|
||||||
class BIMStyleProperties(PropertyGroup):
|
class BIMStyleProperties(PropertyGroup):
|
||||||
|
|||||||
@@ -118,32 +118,31 @@ class Blender:
|
|||||||
return context_override
|
return context_override
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def copy_node_graph_to_active_object(cls, context, material):
|
def copy_node_graph(cls, material_to, material_from):
|
||||||
"""make sure to override `context.active_object`
|
|
||||||
to you want change node graph for the object that's not actually active
|
|
||||||
"""
|
|
||||||
temp_override = cls.get_shader_editor_context()
|
temp_override = cls.get_shader_editor_context()
|
||||||
old_material = context.active_object.active_material
|
shader_editor = temp_override["space"]
|
||||||
new_material = material
|
|
||||||
|
|
||||||
# remove all nodes from the current material
|
# remove all nodes from the current material
|
||||||
for n in old_material.node_tree.nodes[:]:
|
for n in material_to.node_tree.nodes[:]:
|
||||||
old_material.node_tree.nodes.remove(n)
|
material_to.node_tree.nodes.remove(n)
|
||||||
|
|
||||||
# change current material and make other window's shader editor is updated
|
previous_pin_setting = shader_editor.pin
|
||||||
context.active_object.active_material = new_material
|
# required to be able to change material to something else
|
||||||
temp_override["space"].node_tree = new_material.node_tree
|
shader_editor.pin = True
|
||||||
|
shader_editor.node_tree = material_from.node_tree
|
||||||
|
|
||||||
# select all nodes and copy them to clipboard
|
# select all nodes and copy them to clipboard
|
||||||
for node in new_material.node_tree.nodes:
|
for node in material_from.node_tree.nodes:
|
||||||
node.select = True
|
node.select = True
|
||||||
bpy.ops.node.clipboard_copy(temp_override)
|
bpy.ops.node.clipboard_copy(temp_override)
|
||||||
|
|
||||||
# back to original material
|
# back to original material
|
||||||
context.active_object.active_material = old_material
|
shader_editor.node_tree = material_to.node_tree
|
||||||
temp_override["space"].node_tree = old_material.node_tree
|
|
||||||
bpy.ops.node.clipboard_paste(temp_override, offset=(0, 0))
|
bpy.ops.node.clipboard_paste(temp_override, offset=(0, 0))
|
||||||
|
|
||||||
|
# restore shader editor settings
|
||||||
|
shader_editor.pin = previous_pin_setting
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update_screen(cls):
|
def update_screen(cls):
|
||||||
bpy.ops.wm.redraw_timer(type="DRAW_WIN_SWAP", iterations=1)
|
bpy.ops.wm.redraw_timer(type="DRAW_WIN_SWAP", iterations=1)
|
||||||
|
|||||||
@@ -227,6 +227,11 @@ class Style(blenderbim.core.tool.Style):
|
|||||||
attributes.clear()
|
attributes.clear()
|
||||||
blenderbim.bim.helper.import_attributes2(style, attributes)
|
blenderbim.bim.helper.import_attributes2(style, attributes)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def has_blender_external_style(cls, style_elements):
|
||||||
|
external_style = style_elements.get("IfcExternallyDefinedSurfaceStyle", None)
|
||||||
|
return bool(external_style and external_style.Location.endswith(".blend"))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def is_editing_styles(cls):
|
def is_editing_styles(cls):
|
||||||
return bpy.context.scene.BIMStylesProperties.is_editing
|
return bpy.context.scene.BIMStylesProperties.is_editing
|
||||||
@@ -243,6 +248,5 @@ class Style(blenderbim.core.tool.Style):
|
|||||||
obj.select_set(True)
|
obj.select_set(True)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def change_current_style_type(cls, context, obj, blender_material, style_type):
|
def change_current_style_type(cls, blender_material, style_type):
|
||||||
with context.temp_override(active_object=obj):
|
blender_material.BIMStyleProperties.active_style_type = style_type
|
||||||
blender_material.BIMStyleProperties.active_style_type = style_type
|
|
||||||
|
|||||||
Reference in New Issue
Block a user