1
$\begingroup$

Using python how would I hide one level? Like shown below.

enter image description here

$\endgroup$

1 Answer 1

3
$\begingroup$

Override the context.

enter image description here Test run, collapses "Collection 1"

To hide one level the operator used is bpy.ops.outliner.show_one_level(open=False) enable developer tools in prefs to display [find link]

If we invoke an operator in outliner from mouse or button then the context.area is an outliner, if we run it from the text editor, for example, it's a text editor area. The operator knows naught of the text editor area.

Overriding the context is a way to "fool" the operator into thinking it has been run from elsewhere or on a collection that is not context [add link]

Below is a test script. I have hard coded in the default "Collection 1" to be collapsed.

import bpy

context = bpy.context
screen = context.screen
collection = context.collection
collection = bpy.data.collections["Collection 1"]
view_layer = context.view_layer

outliners = [a for a in screen.areas if a.type == 'OUTLINER']
c = context.copy()
c["collection"] = collection
for ol in outliners:
    c["area"] = ol

    bpy.ops.outliner.show_one_level(c, open=False)
    ol.tag_redraw()

#view_layer.update()
$\endgroup$

You must log in to answer this question.

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