Check/uncheck Collections with python

Oh Hi there,
I am looking for a way to include/exclude (check/uncheck) Collections via a script-

I found this:

But its not clear to me how this works.

I tried this:

import bpy
bpy.ops.outliner.collection_enable_render(“collection_name”) and it isnt working.

When I click the checkboxes in the Outliner the command is always context sensitive.

Any Idea how this could work.

The correct operator would probably, or at least should be, named something to do with ‘excluding’.
Operators only work in very specific contexts. You’ll likely need to give it a context override to work, check the api documentation on operators for information on this.

The better way to set the exclusion is to manipulate the variables in the view_layer context like the following.

bpy.context.view_layer.layer_collection.children['Stuff'].exclude = True

Holy Shit, this is it!
It works! This is 100% what i needed!

Thanks so much!

Hello. How could I adapt this type of script to run on multiple selected collections at once?