Hiding a collection
None of the hide properties of the collection are animatable. That is cannot drive or keyframe them.
Can however use a frame change handler to set.
Here is a trivial example that sets the visibility of all immediate children of the scene collection to un hide when frame is in on list, and hide when frame in off list.
import bpy
on = [1, 44, 66]
off = [10, 55, 77]
def frame_change(self):
frame = int(self.frame_current)
for c in self.collection.children:
if frame in on:
c.hide_render = c.hide_viewport = False
elif frame in off:
c.hide_render = c.hide_viewport = True
#bpy.app.handlers.frame_change_pre.clear()
bpy.app.handlers.frame_change_pre.append(frame_change)
Using a keyframed prop
Alternatively can give the scene a custom property. Make it an integer by giving it a value and default that is an integer.
In the graph editor make the extrapolation constant.
Now we have a keyframable switch to turn on off collection switches.
import bpy
def frame_change(self):
frame = int(self.frame_current)
print(frame, self["prop"])
for c in self.collection.children:
c.hide_viewport = c.hide_render = self["prop"]
#bpy.app.handlers.frame_change_pre.clear()
bpy.app.handlers.frame_change_pre.append(frame_change)
Uncommenting (removing hash #) from second last line cleans the previous handlers
See also
How can I exclude a collection from rendering with Python?
How to get all collections of the current scene?
disable all collections with python in blender 2.8
Sorting Collections Alphabetically in the Outliner