How to loop through components, delete non-solid components

Right. I did not address this. Perhaps another boolean arg to decide whether to purge or not ?

Well with regard to selective erasing … perhaps change …

      cdef.instances.to_a {|i| i.erase! }

… to …

      cdef.instances.to_a {|i|
        owner = i.parent
        next i.erase! if owner == i.model
        next if owner.count_used_instances == 0
        i.erase! 
      }

Note: This isn’t likely 100% foolproof either. There could be a situation where a definition has a mixture of used and unused instances.


      next if ( groups ? !cdef.group? : cdef.group? )

… is a bit confusing. It can be rewritten as …

      if groups
        next unless cdef.group?
      else # only component instances
        next if cdef.group?
      end
1 Like

In terms of performance, avoid entity.erase! inside a loop. Collect references to all entities in an array then use entities.erase_entities to perform a bulk erase as that’s much faster.

Similar, avoid modifying the selection within an loop, updating the selection updates the UI which in turns slows things down. Collect references and add/remove to the selection in bulk.

3 Likes

Makes sense. Thank you