1
$\begingroup$

right now only there's the possibility of creating new view layers, but every time you create a view layer it defaults all layers to visible, and sometimes in the scene there are views that are too similar and when you got a lot of collections it gets painful to adjust the same over and over. Is there a way?

$\endgroup$
1
  • $\begingroup$ It works fine on my blender 2.81 I ran the script and a copy of the wiewlayer was created with its atributes. Thanks a lot $\endgroup$ – DAVID TERES Nov 4 '19 at 19:22
2
$\begingroup$

UPDATE This answer is obsolete, blender has now this functionality

This little script lets you duplicate a view layer with its visibility settings only, render passes and cycles settings, render and selectable are settings that are shared among all layers.

import bpy
context = bpy.context
old_layer = context.window.view_layer
new_layer = context.scene.view_layers.new(old_layer.name)
collection = old_layer.layer_collection
new_collection = new_layer.layer_collection

for prop in dir(new_layer):
    try:
        attr = getattr(old_layer,prop)
        setattr(new_layer, prop, attr)
    except:
        pass

cycles = old_layer.cycles
new_cycles = new_layer.cycles
for prop in dir(new_cycles):
    try:
        attr = getattr(cycles,prop)
        setattr(new_cycles, prop, attr)
    except:
        pass


def recursive_attributes(collection, new_collection):
    new_collection.exclude = collection.exclude
    new_collection.holdout = collection.holdout
    new_collection.indirect_only = collection.indirect_only
    new_collection.hide_viewport = collection.hide_viewport

    for i, _ in enumerate(new_collection.children):
        old_child = collection.children[i]
        new_child = new_collection.children[i]
        recursive_attributes(old_child, new_child)

    for i, _ in enumerate(new_collection.collection.objects):
        tmp = collection.collection.objects[i].hide_get()
        new_collection.collection.objects[i].hide_set(tmp)

    return 0

recursive_attributes(collection, new_collection)
context.window.view_layer = new_layer
$\endgroup$
0
$\begingroup$

I'm trying to copy and paste it again because the formatting (intendation) is wrong.

import bpy

noting which collections are excluded. This is stored in colStatesL1 for children of master collection and colStatesL2 for children of children of master collection. This is for maximum two levels of collections tree.

colStatesL1 = []
colStatesL2 = []
layerCol = bpy.context.view_layer.layer_collection.children
for l in layerCol:
     colStatesL1.append((l.name, l.exclude))
     for x in l.children:
         colStatesL2.append((l.name, x.name, x.exclude))

create new view layer

bpy.ops.scene.view_layer_add()

layerCol = bpy.context.view_layer.layer_collection.children

setting collections that are children of master collection

x = 0
for state in colStatesL1:
    layerCol[colStatesL1[x][0]].exclude = colStatesL1[x][1]
    x = x + 1

setting collections that are children of children of master collection

x = 0
for state in colStatesL2:
    layerCol[colStatesL2[x][0]].children[colStatesL2[x][1]].exclude = colStatesL2[x][2]
    x = x + 1  
$\endgroup$
0
$\begingroup$

This script works for me.

It is for two levels of collections, i.e. collections in master collection and collections in collections of master collection. It has to be modified, if there would be three or more levels of collections, i.e. master collection -> collections level 1 inside master collection > collections level 2 inside collections level 1 > collections level 3 inside collections level 2 > etc…

Script:

import bpy
#noting which collections are excluded. This is stored in colStatesL1 for children of master collection and colStatesL2 for children of children of master collection. This is for maximum two levels of collections tree.
colStatesL1 = []
colStatesL2 = []
layerCol = bpy.context.view_layer.layer_collection.children
for l in layerCol:
    colStatesL1.append((l.name, l.exclude))
    for x in l.children:
        colStatesL2.append((l.name, x.name, x.exclude))

#create new view layer
bpy.ops.scene.view_layer_add()

layerCol = bpy.context.view_layer.layer_collection.children
#setting collections that are children of master collection
x = 0
for state in colStatesL1:
    layerCol[colStatesL1[x][0]].exclude = colStatesL1[x][1]
    x = x + 1
#setting collections that are children of children of master collection 
x = 0
for state in colStatesL2:
    layerCol[colStatesL2[x][0]].children[colStatesL2[x][1]].exclude = colStatesL2[x][2]
    x = x + 1       
$\endgroup$
2
  • $\begingroup$ I don't know how to paste it with proper indentation. Without some python knowledge you will not be able to us my script. $\endgroup$ – maciej Apr 26 '20 at 1:58
  • $\begingroup$ I hope I got the indentation right. Please check. $\endgroup$ – Grimm Apr 26 '20 at 11:19

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.