3
$\begingroup$

I have 10 layers and every time I make a new collection I have to click on each layer to exclude collection from every layer. For example, I created collection on layer 1 and need it to be enabled only on layer 1. Is there an option or hotkey to exclude collection from all layers except this one?

enter image description here

$\endgroup$
  • $\begingroup$ You can just click and drag down/up and it will select/deselect multiple at a time. $\endgroup$ – crazycourier Feb 7 at 20:50
  • 1
    $\begingroup$ I need smth like this blender.stackexchange.com/a/160759/60139 but i don't understand anything in coding and when copy-paste the code it does nothing $\endgroup$ – Anna Feb 7 at 21:02
3
$\begingroup$

The following Add-on code can do this for you. Save the code as oco.py and install that file as an Add-on in Blender. You can then activate it when searching for Collection Ops in the Add-ons section of Blender.

Once done, right-click in the Outliner on the collection you would like to exclude for all other View Layers and select the command on the bottom:

Menu entry

Here's the Add-on code:

'''
Created on 09.07.2019

@author: r.trummer
'''
bl_info = {
    "name": "Collection Ops",
    "author": "Rainer Trummer",
    "version": (0, 1, 0),
    "blender": (2, 80, 0),
    "description": "provides functions in the Outliner context menu to work with collections",
    "category": "Object"
}

import bpy
from bpy.props import BoolProperty


#===============================================================================
#    Operators
#===============================================================================
def get_flattened_layer_collections(layer_collection):
    for c in layer_collection.children:
        yield c
        yield from get_flattened_layer_collections(c)


def find_layer_collection(view_layer, layer_collection, **kwargs):
    pool = kwargs.setdefault('pool', view_layer.layer_collection)

    colls = get_flattened_layer_collections(pool)

    for c in colls:
        if c.collection == layer_collection.collection:
            return c


class COLLECTION_OT_Unset_For_Other_View_Layers(bpy.types.Operator):
    bl_idname = "collection.unset_for_other_view_layers"
    bl_label = "Exclude In Other View Layers"
    bl_description = 'excludes collection in all other view layers'
    bl_options = {'REGISTER', 'UNDO'}

    deactivate: BoolProperty(default = True)

    def execute(self, context):
        for vl in context.scene.view_layers:
            if vl != context.view_layer:
                vl_collection = find_layer_collection(vl, context.view_layer.active_layer_collection)
                vl_collection.exclude = True

        context.view_layer.depsgraph.update()

        return {'FINISHED'}


#===============================================================================
#    put the commands into menus
#===============================================================================
def draw_outliner_collection_commands(self, context:bpy.context):
    layout = self.layout
    layout.separator()
    layout.operator(COLLECTION_OT_Unset_For_Other_View_Layers.bl_idname)


#===============================================================================
#    registration code
#===============================================================================
__classes__ = (
    COLLECTION_OT_Unset_For_Other_View_Layers,
)


def register():
    for cl in __classes__:
        bpy.utils.register_class(cl)

    bpy.types.OUTLINER_MT_collection.append(draw_outliner_collection_commands)


def unregister():
    for cl in reversed(__classes__):
        bpy.utils.unregister_class(cl)

    bpy.types.OUTLINER_MT_collection.remove(draw_outliner_collection_commands)


if __name__ == "__main__":
    register()
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.