How to keyframe all the objects viewport/render visibility inside the selected collection via python code? How to keyframe all the objects viewport/render visibility inside the selected collection via python code? And there's a boolean there that you can just type &quot;False&quot; or &quot;True&quot; to decide whether unhide or not. I only have a little bit of knowledge of scripting, especially in blender python library. I was thinking of this after knowing collections viewport/render visibility can't be keyframed, because I want to keyframe the object collections that aren't needed in certain camera shots. But trying to make it myself takes long time now, that I have to meet a certain deadline to pass my project.
After several moments I just got it, but I know there maybe a cleaner approach
import bpy

for ob in bpy.context.collection.all_objects:
    
    ob.keyframe_insert(data_path=&quot;hide_render&quot;)
    ob.keyframe_insert(data_path=&quot;hide_viewport&quot;)
    if ob.hide_render == True:
        ob.hide_render = False
    else:
        ob.hide_render = True
    if ob.hide_viewport == True:
        ob.hide_viewport = False
    else:
        ob.hide_viewport = True