When I execute the following code within Blender, it works well regardless of whether it's version 3.x or 4.x. However, if I try to use it by launching Blender with a specific argument to open a Blender file containing this script (e.g., blender --python myscript.py myfile.blend), the file opens correctly but the script triggers an error after Blender's addons load:

KeyError: 'bpy_prop_collection[key]: key "View Layer" not found'

I tried to override the context to force it to the Outliner, but encountered errors with that approach as well. Any thoughts?

import bpy
class ChrisMemoriserInvisible(bpy.types.Operator):
    bl_idname = "wm.memorise_invisible"
    bl_label = "Minimal Operator"
    def description(cls, context, properties):
        return "get meshes and collection visibility"
    def execute(self, context):
        print("\n\r---- S T A R T I N G ---------------------\n\r")
        # Recursivly transverse collection
        def collections_recursively(collection):
            print(">>collection child: " + collection.name + " - invisible : " + str( collection.hide_viewport))
            if collection.hide_viewport == True:
                print("collection invisible :" + collection.name)
                f.write("collection>>" + collection.name +  "\r")
            for child in collection.children:
        # Recursivly transverse layer_collection
        def recurLayerCollection(layerColl):
            found = None
            for layer in layerColl.children:
        #        print(">>>",layer.name)
                if layer.exclude == True:
                    print("collection unchecked :" + layer.name)
                    f.write("layer>>" + layer.name +  "\r")
                elif layer.exclude == False:
                found = recurLayerCollection(layer)
                if found:
                    return found
        view_layer = bpy.context.scene.view_layers["View Layer"]
        obj_visibles = []
        with open("D:/_Chris/- SCRIPTS -/import_collection_invisible_mesh.ini","w") as f:              
            for o in bpy.context.scene.objects:
                if o.hide_get() == 0:
                    print ("visible : " + o.name)
                    print ("invisible : " + o.name )
                    f.write("mesh>>" + o.name +  "\r")  
                if o.hide_viewport == True:
                    print ("invisible : " + o.name )
                    f.write("meshview>>" + o.name +  "\r")  
            # collection with eye closed or not
            for collection in bpy.context.view_layer.layer_collection.children:
                print ("----------------------------------------")
                print(collection.name, ' - ', collection.hide_viewport)  
            # collection unchecked or not
            for layer_collection in view_layer.layer_collection.children:
                if layer_collection.exclude == True:
                    print("collection unchecked :" + layer_collection.name)
                    f.write("layer>>" + layer_collection.name +  "\r")
                elif layer_collection.exclude == False:
                layerColl = recurLayerCollection(layer_collection)
        return {'FINISHED'} 

def register():
def unregister():

if __name__ == "__main__":

UPDATE: the error I get from the console, the script I call is named cs_append_get_hidden_collections.py

enter image description here

enter image description here

  • $\begingroup$ @Chris : good question and yes if I go in the outliner in Display Mode > Scenes > View Layers > View Layer is present. So I have created an other new test file with 3.6.9 version and by default the view layer is named ViewLayer without space in it. If I fire my script again changing "View Layer" to "ViewLayer" I don't get any error but the list of objects is as if it was a virgin file without anything inside : it only list a camera and a light with a collection. It looks like blender is not seeing the loaded scene but a brand new one $\endgroup$
    – cscholl
    Commented Jul 9 at 9:59

1 Answer 1


Notice that the error appears before it reads the blend file. That is because your command should specify the blend file first and then the python script argument like this:

blender myfile.blend --python myscript.py

The issue arose because the command was structured with the --python script argument first, followed by the .blend file specification. In Blender's background mode, this sequence initially executes the Python script with a default setup where the view layer is named "ViewLayer" without a space. Consequently, the error occurs before Blender reads the .blend file specified. To resolve this, ensure that your command specifies the blend file first and then the Python script argument, like this:

blender myfile.blend --python myscript.py

enter image description here


You must log in to answer this question.

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