Inserting visibility keyframe?

Hello everybody. I am writing a script and what I want it to do is insert a keyframe on the visibility of the object. The visibility is the little eye icon in the outliner to the right of the object name.

It’s the .hide property, render visibility is .hide_render

Simple way to keyframe the visibility of the active object:

bpy.context.object.keyframe_insert(data_path=“hide”, frame=1)

So what is the command to make it visible in the render again?

Or alternately…


import bpy

ob = bpy.data.objects.get("Cube")
ob.hide_render = True # or False to NOT hide.

I want to keyframe the object being hidden and visible. Here is what I have.

bpy.context.object.keyframe_insert(data_path=“hide_render = False”, frame=i)
bpy.context.object.keyframe_insert(data_path=“hide_render = True”, frame=i+1)

On frame i, I want the object to be visible to the render. On the next frame, I don’t want it to be visible. For some reason, this code doesn’t work.

The data_path parameter expects a data_path, not an expression. keyframe_insert() only keyframes the current state, so set it beforehand:

ob = bpy.context.object
ob.hide_render = False
ob.keyframe_insert(data_path="hide_render, frame=i)
ob.hide_render = True
ob.keyframe_insert(data_path="hide_render, frame=i+1)

http://www.blender.org/documentation/blender_python_api_2_70a_release/info_quickstart.html#animation

1 Like

Hi, I found this thread having been searching for how to do this… thanks to the previous poster, the code snippet still works in Blender 3.1, except having tried to get it to work I noticed that the code snippet is missing closing quotes on the data_path variables, ie it should be:

ob = bpy.context.object
ob.hide_render = False
ob.keyframe_insert(data_path="hide_render", frame=i)
ob.hide_render = True
ob.keyframe_insert(data_path="hide_render", frame=i+1)

Thanks, Pete.

1 Like