# How to Toggle Selected Objects Visibility with H shortcut as in Maya

I want to be able to toggle the visibility of selected objects with a shortcut (H) the way you can do in Maya and XSI, shown in the GIF. I'm guessing scrippting is needed. I've tried to do it my self but I'm new to Python and Blender and as soon as the objects are hidden they seem to no longer be in a selection state/list. Eg if I manually select the hidden object/s in the Outliner and try to print the selected object to the console, none seem to be selected. Is this possible to do with the current state of Blender 2.8?

iceythe provided a solution over at https://blenderartists.org/t/how-to-get-selection-objects-list-in-outliner/1184175/3?u=joelart

# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

import bpy
bl_info = {
"name": "Toggle Hide",
"location": "3D View / Outliner, (Hotkey J)",
"version": (0, 1, 0),
"blender": (2, 81, 0),
"description": "Toggle object visibility of outliner selection",
"author": "kaio",
"category": "Object",
}

class OUTLINER_OT_toggle_hide(bpy.types.Operator):
bl_idname = "outliner.toggle_hide"
bl_label = "Toggle Hide"
bl_options = {'REGISTER', 'UNDO'}

@classmethod
def poll(self, context):
ar = context.screen.areas
__class__.area = next(
(a for a in ar if a.type == 'OUTLINER'), None)
return __class__.area

def execute(self, context):
objs = context.view_layer.objects
sel_objs = {o for o in objs if o.select_get()}
hid_objs = {o for o in objs if o.hide_get()}

# Hide selected
for o in sel_objs:
o.hide_set(True)

# Unhide hidden
for o in hid_objs:
o.hide_set(False)

# Select objects marked in outliner
bpy.ops.outliner.object_operation(
{'area': __class__.area}, type='SELECT')

# Re-hide others
for o in hid_objs:
if not o.select_get():
o.hide_set(True)
return {'FINISHED'}

def register():
bpy.utils.register_class(OUTLINER_OT_toggle_hide)
wm = bpy.context.window_manager
km = kc.get("Object Mode")
if not km:
km = kc.new("Object Mode")
kmi = km.keymap_items.new("outliner.toggle_hide", "J", "PRESS")

km = kc.get("Outliner")
if not km:
km = kc.new("Outliner", space_type="OUTLINER")
kmi = km.keymap_items.new("outliner.toggle_hide", "J", "PRESS")

def unregister():
bpy.utils.unregister_class(OUTLINER_OT_toggle_hide)

km.keymap_items.remove(kmi)