2020from .importer import ShapeTreeNode
2121
2222GLOBAL_FILE_CACHE = {}
23+ ROOT_PARENT = - 1
2324
2425
2526class HierarchyType (IntEnum ):
@@ -262,6 +263,10 @@ def build_mesh(
262263 trimesh .filter_zero_area ()
263264 trimesh .filter_same_face ()
264265 trimesh .fill_empty_color ()
266+
267+ # Temporary fix. Original addon was apparently smarter at replacing only the rebuilt faces
268+ obj .data .clear_geometry ()
269+
265270 trimesh .add_to_mesh (obj .data )
266271 obj .data .update ()
267272
@@ -310,7 +315,9 @@ def node_parse(
310315 # link objects to tree
311316 if len (hierarchy_collections .items ()) > 0 :
312317 for obj in created_objs :
313- hierarchy_collections [obj ["STEP_parent" ]].objects .link (obj )
318+ parent_col_uuid = obj .get ("STEP_parent" , ROOT_PARENT )
319+ parent_col = hierarchy_collections .get (parent_col_uuid , hierarchy_collections [- 1 ])
320+ parent_col .objects .link (obj )
314321 global_t = tree .nodes [obj ["STEP_tree_location" ]].global_transform
315322 set_obj_matrix_world (obj , global_t )
316323
@@ -389,7 +396,7 @@ def build_collection_instances(instanced_objects, hierarchy_collections, scale):
389396 component_col .objects .link (source_obj )
390397
391398 # instance original
392- empty = bpy .data .objects .new (source_obj .name + "_instance" , None )
399+ empty = bpy .data .objects .new (source_obj .name , None )
393400 empty .instance_type = "COLLECTION"
394401 empty .instance_collection = component_col
395402 empty .matrix_world = trsf
@@ -400,13 +407,13 @@ def build_collection_instances(instanced_objects, hierarchy_collections, scale):
400407 # instance copies
401408 for shape_name , local_t , global_t , parent_uuid in instances_info :
402409 # create instance
403- empty = bpy .data .objects .new (shape_name + "_instance" , None )
410+ empty = bpy .data .objects .new (source_obj . name + "_instance" , None )
404411 empty .instance_type = "COLLECTION"
405412 empty .instance_collection = component_col
406413 empty .empty_display_size = scale
407414 scale_translation (global_t , scale )
408415 set_obj_matrix_world (empty , global_t )
409- parent_col = hierarchy_collections [ parent_uuid ]
416+ parent_col = hierarchy_collections . get ( parent_uuid , hierarchy_collections [ - 1 ])
410417 parent_col .objects .link (empty )
411418
412419
@@ -422,8 +429,9 @@ def load_step(
422429):
423430 # Find file
424431 from . import importer
425-
426432 filename = "" .join (ntpath .basename (filepath ).split ("." )[:- 1 ])
433+
434+ # Try retrieve step_reader from cache
427435 if filepath not in GLOBAL_FILE_CACHE :
428436 try :
429437 step_reader = importer .ReadSTEP (filepath )
@@ -458,7 +466,7 @@ def load_step(
458466 all_shapes = tree .get_shapes ()
459467 total = len (all_shapes )
460468
461- # Build shapes
469+ # Build shapes objects
462470 wm .progress_begin (0 , total )
463471 for i , (shp , node_index ) in enumerate (all_shapes ):
464472 parent_uuid , self_uuid , tag , obj_name , _ , local_t , global_t = tree .nodes [
@@ -488,12 +496,14 @@ def load_step(
488496 source_obj = created_names [shape_name ]
489497 if htypes == HierarchyType .COLLECTION_INSTANCES :
490498 if source_obj in instanced_objects :
499+ parent_for_instance = parent_uuid if parent_uuid != 0 else ROOT_PARENT
491500 instanced_objects [source_obj ].append (
492- (shape_name , local_t , global_t , parent_uuid )
501+ (shape_name , local_t , global_t , parent_for_instance )
493502 )
494503 else :
504+ parent_for_instance = parent_uuid if parent_uuid != 0 else ROOT_PARENT
495505 instanced_objects [source_obj ] = [
496- (shape_name , local_t , global_t , parent_uuid )
506+ (shape_name , local_t , global_t , parent_for_instance )
497507 ]
498508 else :
499509 obj = source_obj .copy ()
@@ -526,7 +536,8 @@ def load_step(
526536 if obj :
527537 # assign property to obj
528538 obj ["STEP_tag" ] = tag
529- obj ["STEP_parent" ] = parent_uuid
539+ obj_parent = parent_uuid if parent_uuid != 0 else ROOT_PARENT
540+ obj ["STEP_parent" ] = obj_parent
530541 obj ["STEP_uuid" ] = self_uuid
531542 obj ["STEP_file" ] = filepath
532543 obj ["STEP_name" ] = obj_name
0 commit comments