I need a quick way to hide thousands of objects in the view layer via python. I have tried running the following code, but it takes 10 seconds to run with 10,000 objects:
objs_to_hide = bpy.context.scene.collection.all_objects
for obj in objs_to_hide:
if obj.visible_get():
obj.hide_set(True)
Is there a way to do this operation faster? I also tried using the 'hide_view_set' operator with the following code:
objs_to_hide = bpy.context.scene.collection.all_objects
for obj in objs_to_hide:
obj.select_set(True)
bpy.context.view_layer.objects.active = objs_to_hide[0]
bpy.ops.object.hide_view_set(unselected=False)
But I got the following error when running that: RuntimeError: Operator bpy.ops.object.hide_view_set.poll() failed, context is incorrect
I have a feeling the operator would not be the most efficient path forwards anyways – operators seemingly rarely are. I did try hiding 10,000 objects from the viewport interface using the 'h' key and it seems to run instantly... which leads me to believe it must be possible to hide massive amounts of objects much faster than the first code snippet above. Any help would be greatly appreciated!