From ba63ed81a1757f2dcf1b9c3bb4963b0f0c9fb8bf Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Thu, 4 Apr 2024 18:24:07 +0500 Subject: [PATCH] small optimizations 1) stop on first parent collection 2) ensure object's own collection's parent has BIMCollectionProperties.obj during the loop 3) collector._get_collections - returns project's own collection for consistency 4) couple tweaks skipping unnecessary iterations in loops --- src/blenderbim/blenderbim/tool/collector.py | 24 ++++++++++----------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/blenderbim/blenderbim/tool/collector.py b/src/blenderbim/blenderbim/tool/collector.py index e834cbc492..473ac85b97 100644 --- a/src/blenderbim/blenderbim/tool/collector.py +++ b/src/blenderbim/blenderbim/tool/collector.py @@ -57,22 +57,24 @@ class Collector(blenderbim.core.tool.Collector): for collection in obj.users_collection: if parent_collection: break + # skip Types and non-BIM collections if not collection.BIMCollectionProperties.obj: continue + # for objects that own collections we search for the first parent collection if collection.BIMCollectionProperties.obj == obj: + collection_name = collection.name for bpy_collection in bpy.data.collections: - if bpy_collection.children.find(collection.name) != -1: + if bpy_collection.children.get(collection_name) and bpy_collection.BIMCollectionProperties.obj: parent_collection = bpy_collection + parent_obj = bpy_collection.BIMCollectionProperties.obj + break else: parent_collection = collection + parent_obj = collection.BIMCollectionProperties.obj if not parent_collection: return - parent_obj = parent_collection.BIMCollectionProperties.obj - if not parent_obj: - return - parent = tool.Ifc.get_entity(parent_obj) if parent: # This is lazy, but works. One of these will succeed, the other will fail silently. @@ -147,10 +149,8 @@ class Collector(blenderbim.core.tool.Collector): grid_obj = tool.Ifc.get_object(grid) if grid_obj: grid_col = cls._get_own_collection(grid, grid_obj) - axes_col = [c for c in grid_col.children if axes in c.name] - if axes_col: - return axes_col[0] - return bpy.data.collections.new(axes) + axes_col = next((c for c in grid_col.children if axes in c.name), None) + return axes_col or bpy.data.collections.new(axes) if element.is_a("IfcAnnotation") and element.ObjectType == "DRAWING": return cls._create_own_collection(obj) @@ -165,7 +165,7 @@ class Collector(blenderbim.core.tool.Collector): return cls._create_own_collection(obj) if getattr(element, "IsNestedBy", None): - if [e for e in element.IsNestedBy[0].RelatedObjects if not e.is_a("IfcPort")]: + if any(e for e in element.IsNestedBy[0].RelatedObjects if not e.is_a("IfcPort")): return cls._create_own_collection(obj) @classmethod @@ -232,9 +232,7 @@ class Collector(blenderbim.core.tool.Collector): project_obj = tool.Ifc.get_object(tool.Ifc.get().by_type("IfcProject")[0]) if project_obj: - collection = bpy.data.collections.get(project_obj.name) - if collection: - return collection + return project_obj.BIMObjectProperties.collection @classmethod def _create_project_child_collection(cls, name: str) -> bpy.types.Collection: