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"
@classmethod
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:
collections_recursively(child)
# Recursivly transverse layer_collection
def recurLayerCollection(layerColl):
found = None
for layer in layerColl.children:
# print(">>>",layer.name)
if layer.exclude == True:
print(layer.name,"true")
print("collection unchecked :" + layer.name)
f.write("layer>>" + layer.name + "\r")
elif layer.exclude == False:
print(layer.name,"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)
else:
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)
collections_recursively(collection)
# collection unchecked or not
for layer_collection in view_layer.layer_collection.children:
if layer_collection.exclude == True:
print(layer_collection.name,"true")
print("collection unchecked :" + layer_collection.name)
f.write("layer>>" + layer_collection.name + "\r")
elif layer_collection.exclude == False:
print(layer_collection.name,"false")
layerColl = recurLayerCollection(layer_collection)
f.closed
return {'FINISHED'}
def register():
bpy.utils.register_class(ChrisMemoriserInvisible)
def unregister():
bpy.utils.unregister_class(ChrisMemoriserInvisible)
if __name__ == "__main__":
register()
bpy.ops.wm.memorise_invisible()
UPDATE: the error I get from the console, the script I call is named cs_append_get_hidden_collections.py