Introduce defaultdict instead of checks

This commit is contained in:
Krzysztof Trzciński
2019-07-16 18:30:03 +01:00
parent a851a25ddc
commit 6284b3960d
@@ -43,6 +43,7 @@ if "bpy" in locals():
import bpy import bpy
import mathutils import mathutils
from collections import defaultdict
from bpy.props import StringProperty, IntProperty, BoolProperty from bpy.props import StringProperty, IntProperty, BoolProperty
from bpy_extras.io_utils import ImportHelper from bpy_extras.io_utils import ImportHelper
@@ -74,7 +75,7 @@ def import_ifc(filename, use_names, process_relations, blender_booleans):
if not valid_file: if not valid_file:
return False return False
print("Done reading file") print("Done reading file")
id_to_object = {} id_to_object = defaultdict(list)
id_to_parent = {} id_to_parent = {}
id_to_matrix = {} id_to_matrix = {}
openings = [] openings = []
@@ -82,6 +83,7 @@ def import_ifc(filename, use_names, process_relations, blender_booleans):
print("Creating geometry...") print("Creating geometry...")
collection = bpy.data.collections.new(f"{bpy.path.basename(filename)}") collection = bpy.data.collections.new(f"{bpy.path.basename(filename)}")
bpy.context.scene.collection.children.link(collection) bpy.context.scene.collection.children.link(collection)
if process_relations: if process_relations:
rel_collection = bpy.data.collections.new("Relations") rel_collection = bpy.data.collections.new("Relations")
collection.children.link(rel_collection) collection.children.link(rel_collection)
@@ -178,8 +180,6 @@ def import_ifc(filename, use_names, process_relations, blender_booleans):
bob.hide_viewport = bob.hide_render = True bob.hide_viewport = bob.hide_render = True
bob.display_type = 'WIRE' bob.display_type = 'WIRE'
if ob.id not in id_to_object:
id_to_object[ob.id] = []
id_to_object[ob.id].append(bob) id_to_object[ob.id].append(bob)
if ob.parent_id > 0: if ob.parent_id > 0:
@@ -201,8 +201,7 @@ def import_ifc(filename, use_names, process_relations, blender_booleans):
if process_relations: if process_relations:
print("Processing relations...") print("Processing relations...")
while len(id_to_parent_temp):
while len(id_to_parent_temp) and process_relations:
id, parent_id = id_to_parent_temp.popitem() id, parent_id = id_to_parent_temp.popitem()
if parent_id in id_to_object: if parent_id in id_to_object:
@@ -235,7 +234,6 @@ def import_ifc(filename, use_names, process_relations, blender_booleans):
if parent_ob.parent_id > 0: if parent_ob.parent_id > 0:
id_to_parent[parent_id] = parent_ob.parent_id id_to_parent[parent_id] = parent_ob.parent_id
id_to_parent_temp[parent_id] = parent_ob.parent_id id_to_parent_temp[parent_id] = parent_ob.parent_id
if parent_id not in id_to_object: id_to_object[parent_id] = []
id_to_object[parent_id].append(bob) id_to_object[parent_id].append(bob)
if bob: if bob:
for ob in id_to_object[id]: for ob in id_to_object[id]: