1
$\begingroup$

I selected some objects in outliner, all of them are hidden. Now I want to know how to get these objects? I tried using bpy.context.selected_objects , but it prints an empty array [].

I know I can use bpy.context.object to get one object, but I don’t know if there is a way to get all hidden objects?

$\endgroup$
2
  • $\begingroup$ You can use docs.blender.org/api/current/… but it's only available in the context of the outliner. (your button must be clicked in the outliner) $\endgroup$
    – Gorgious
    Commented Apr 11, 2023 at 10:12
  • $\begingroup$ @Gorgious thanks! $\endgroup$
    – XIUYI GU
    Commented Apr 12, 2023 at 6:00

4 Answers 4

1
$\begingroup$

I haven't tested this one out, but how about selecting all objects in the scene, and then only keep objects with state set to hidden?

import bpy 
# get all objects
all_objects = bpy.data.objects[:]

# keep only objects that are hidden
hidden_objects = [obj for obj in all_objects if obj.hide_viewport == True]
$\endgroup$
1
$\begingroup$

context.selected_objects and context.selectable_objects return only objects that are visible in viewport because an invisible object is no longer valid as selected in the data, even if it´s marked darker in the Outliner.

When a selected object is made invisible (hidden or disabled) and switched visible again, it seems to be selected in Outliner but the yellow (selection) border in the 3DView is missing. The object can´t be modified (e.g. hold, rotate, scale) until it has been reselected.

In Outliner there are two ways to make an object invisible:

Hide in Viewport (Symbol Eye) and Disable in Viewports (Symbol Monitor - filter disabled by default)

Note that 'Hide in Viewport' (Eye) is an attribute of LayerCollection and 'Disable in Viewports' (Monitor) an attribute of (Data)Collection. See the first two entries here: link

There are different ways to get invisible, hidden or disabled objects, depending on what is desired:

import bpy 

# get invisible objs (hidden or disabled) by different of context.visible_objects
invisible_objs = [o for o in bpy.data.objects if not o in bpy.context.visible_objects]
print("invisible (eye or monitor):", invisible_objs)

# get hidden objs by viewlayer.objects
hidden_objs = [o for o in bpy.context.view_layer.objects if o.hide_get() is True]
print("hidden (eye):", hidden_objs)

# get disabled objs by data.objects
disabled_objs = [o for o in bpy.data.objects if o.hide_viewport == True]
print("disabled (monitor):", disabled_objs) 
$\endgroup$
0
$\begingroup$

I figured out how to get all selected objects, whether they are hidden or not.

def GetAllSelectedObjects():
    visibles = bpy.context.visible_objects
    bpy.ops.object.hide_view_clear()
    selecteds = bpy.context.selected_objects
    for o in bpy.data.objects:
        if not o in visibles:
            o.hide_set(True)
    return selecteds
$\endgroup$
0
$\begingroup$

In case anyone is still struggling with this, this is the solution I got in Blender 4.0.

It is used for hidden objects, collections and any other object selected in the context of the outliner.

import bpy

def getSelectedObjects():
    '''Returns all the selected objects, included the hidden ones'''

    selectedObjects = []

    screen = bpy.context.screen
    areas = [area for area in screen.areas if area.type == 'OUTLINER']
    regions = [region for region in areas[0].regions if region.type == 'WINDOW']

    with bpy.context.temp_override(area=areas[0], region=regions[0], screen=screen):
        for obj in bpy.context.selected_ids:
            selectedObjects.append(obj.name)

    return selectedObjects


print(getSelectedObjects())
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .