Skip to content

Instantly share code, notes, and snippets.

@Pullusb
Last active September 7, 2021 14:08
Show Gist options
  • Save Pullusb/7d41d5af3213131b928828d869c9494e to your computer and use it in GitHub Desktop.
Save Pullusb/7d41d5af3213131b928828d869c9494e to your computer and use it in GitHub Desktop.
Blender - Animate visibility of all active collection's objects
import bpy
def animate_visibility(ob, switch=-1):
'''
add 2 keys to keyframe visibility:
switch options:
nohting or -1 = auto (set the inverse of actual state)
0 = visible before
1 = visible after
'''
T = bpy.context.scene.frame_current
if switch == -1:
ob.keyframe_insert(data_path='hide_viewport', frame=T-1)
ob.keyframe_insert(data_path='hide_render', frame=T-1)
ob.hide_viewport = not ob.hide_viewport
ob.hide_render = not ob.hide_render
ob.keyframe_insert(data_path='hide_viewport', frame=T)
ob.keyframe_insert(data_path='hide_render', frame=T)
else:
ob.hide_viewport = switch
ob.hide_render = switch
ob.keyframe_insert(data_path='hide_viewport', frame=T-1)
ob.keyframe_insert(data_path='hide_render', frame=T-1)
ob.hide_viewport = not switch
ob.hide_render = not switch
ob.keyframe_insert(data_path='hide_viewport', frame=T)
ob.keyframe_insert(data_path='hide_render', frame=T)
for ob in bpy.context.scene.view_layers['View Layer'].active_layer_collection.collection.all_objects:
#switch : 0 = visible before / 1 = visible after (-1 automatic)
animate_visibility(ob, switch = -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment