Deep Select/Selection Tools: Can we select every object in the model that has a the same material as selection?

Experimental! Not really tested. Use your own risk! Backup…backup…backup…
However it should work… :wink:

Copy-paste this snippet to Ruby Console and hit Enter (Return)
You will have a right click (context) menu if you select only one group or instance which have a material applied on:

"Delete all objects with same material."
You can undo it!

module Dezmo
  module DezmoDelSameMatObj_test

    extend self
    @@loaded = false unless defined?(@@loaded)
    
    def get_same_mat_instances(ents, instances = [])
      ents.each{|e|
        case e
        when Sketchup::ComponentInstance, Sketchup::Group
          instances<<e if e.material == @mat
          get_same_mat_instances(e.definition.entities, instances)
        end
      }
      instances
    end

    def del_same_mat
      model = Sketchup.active_model
      sel = model.selection
      @mat =  sel.first.material
      entities = model.entities
      to_erase = get_same_mat_instances(entities)
      count = to_erase.size
      model.start_operation('Delete same material objects', true)
      to_erase.to_a.each{ |e| e.erase! if e.valid?}
      model.commit_operation
      UI.messagebox("Object with #{@mat.name} material deleted: #{count}")
    end
    
    unless @@loaded
      UI.add_context_menu_handler{|menu|
        sel = Sketchup.active_model.selection
        if sel.single_object? && sel.first.material
          menu.add_item("Delete all objects with same material."){del_same_mat()}
        end
      }
      @@loaded = true
    end
    
  end
end

delsamematobj

5 Likes