Modify the visibility of collections locally to the viewport with python We have a project with 2 viewports in the same interface. We need an operator that can click on one of the viewports and change the visibility of some collections locally in the viewport that has been clicked.

We have find the following code for set the view of the viewport in local mode:

bpy.context.space_data.use_local_collections = True


And the only method we have found to change the visibility of a collection:

bpy.ops.object.hide_collection(context, collection_index=i, toggle=True)


That inverts the visibility of the current collection.

The problem is, that this method changes visibility globally and we want to change it locally.

We know that the execution context is important, but since we are executing it from an operator in the tools window of the viewport, we believe that we are doing that correctly.

Other variables as disabled in viewport, have a direct access to the variable via python, example:

bpy.data.collections["Collection"].hide_viewport = False


But the variable that we need in local does not have it.

Manually, in the interface, it is simple to do, as seen in the image


The final question is: "Is there any way to edit via python, the visibility of the collections locally to the viewport?"
We have been reviewing it and we have realized that the problem was in the context overwrite. Although we have already done this, we did it by selecting the wrong area

ctx_override = context.copy()
ctx_override['area'] = get_area(context, 'VIEW_3D')


The function get_area (…) returned the area of ​​the viewport in which to make the changes, but did not select the correct viewport, since having two in the interface always takes the first one located in the vector, which was a viewport with local visibility of collections deactivated.

Now we differentiate the viewports by their width to make the changes in the correct viewport