code: def maintained_selection():
      
  
      previous_selection = hou.selectedNodes()
      try:
          yield
      finally:
          if previous_selection:
              for node in previous_selection:
                  node.setSelected(on=True)
          else:
              for node in previous_selection:
                  node.setSelected(on=False)
docs: Maintain selection during context
  
      Example:
          >>> with maintained_selection():
          ...     # Modify selection
          ...     node.setSelected(on=False, clear_all_selected=True)
          >>> # Selection restored