reload_representation to support multple objects and optimize the process

This commit is contained in:
Andrej730
2024-05-29 15:00:10 +05:00
parent cf9f792a3b
commit 3679fccfbd
2 changed files with 32 additions and 24 deletions
+30 -16
View File
@@ -40,7 +40,7 @@ import blenderbim.bim.import_ifc
from math import radians, pi from math import radians, pi
from mathutils import Vector, Matrix from mathutils import Vector, Matrix
from blenderbim.bim.ifc import IfcStore from blenderbim.bim.ifc import IfcStore
from typing import Union from typing import Union, Iterable
class Geometry(blenderbim.core.tool.Geometry): class Geometry(blenderbim.core.tool.Geometry):
@@ -755,21 +755,35 @@ class Geometry(blenderbim.core.tool.Geometry):
bpy.context.view_layer.update() bpy.context.view_layer.update()
@classmethod @classmethod
def reload_representation(cls, obj): def reload_representation(cls, obj_or_objs: Union[bpy.types.Object, Iterable[bpy.types.Object]]) -> None:
"""reload `obj` active representation""" """Reload object/objects active representation.
if not obj.data:
return Ensures that same representations won't be reloaded multiple times.
representation = tool.Ifc.get().by_id(obj.data.BIMMeshProperties.ifc_definition_id) """
blenderbim.core.geometry.switch_representation( objs = obj_or_objs if isinstance(obj_or_objs, Iterable) else [obj_or_objs]
tool.Ifc,
tool.Geometry, # Filter out unique meshes to avoid
obj=obj, # reloading the same representation multiple times.
representation=representation, meshes_to_objects: dict[bpy.types.Mesh, bpy.types.Object]
should_reload=True, meshes_to_objects = dict()
is_global=True, for obj in objs:
should_sync_changes_first=False, mesh = obj.data
apply_openings=True, if not mesh:
) continue
meshes_to_objects.setdefault(mesh, obj)
for obj in meshes_to_objects.values():
representation = tool.Ifc.get().by_id(obj.data.BIMMeshProperties.ifc_definition_id)
blenderbim.core.geometry.switch_representation(
tool.Ifc,
tool.Geometry,
obj=obj,
representation=representation,
should_reload=True,
is_global=True,
should_sync_changes_first=False,
apply_openings=True,
)
@classmethod @classmethod
def remove_representation_item(cls, representation_item): def remove_representation_item(cls, representation_item):
+2 -8
View File
@@ -228,14 +228,8 @@ class Material(blenderbim.core.tool.Material):
def update_elements_using_material(cls, material): def update_elements_using_material(cls, material):
# update elements that are using this material # update elements that are using this material
elements = ifcopenshell.util.element.get_elements_by_material(tool.Ifc.get(), material) elements = ifcopenshell.util.element.get_elements_by_material(tool.Ifc.get(), material)
# filter only unique representations to avoid reloading same representations multiple times objects = [tool.Ifc.get_object(e) for e in elements]
meshes_to_objects = dict() tool.Geometry.reload_representation(objects)
for e in elements:
obj = tool.Ifc.get_object(e)
mesh = obj.data
meshes_to_objects[mesh] = obj
for obj in meshes_to_objects.values():
tool.Geometry.reload_representation(obj)
@classmethod @classmethod
def sync_blender_material_name(cls, material): def sync_blender_material_name(cls, material):