Selection.remove_observer

Hello
I want to remove Selectionobsever after a certain operation, but I can’t do it.
Thanks!

class MySelectionObserver < Sketchup::SelectionObserver

  def onSelectionBulkChange(selection)
        face = selection.grep(Sketchup::Face)[0]
        p face.persistent_id
        
        Sketchup.active_model.selection.remove_observer(selection) 
  end 
end
# Attach the observer.
Sketchup.active_model.selection.add_observer(MySelectionObserver.new)

here is the gif:
1636812329797 00_00_00-00_00_30

The remove_ takes an observer, so try something like

 @@obs = MySelectionObserver.new()
 Sketchup.active_model.selection.add_observer(@@obs)

and in the class itself

selection.remove_observer(@@obs)

BUT once you’ve added the observer why do you want/need to remove it ?

4 Likes

Thanks a lot! Here is the logic i want to achieve:
Use the Selectionobsever to pass the persistent_id of the face to the plugin page when the face is selected, and close the Selectionobsever when the plugin page changes.