# Python path for allowing animation of viewport and render visibility of objects

EDIT: It turns out I formulated this question out of a misunderstanding of my own old code. See @batFINGER and @scurest answers.

I'm working on a script which automates some animation, which includes turning on and off objects' viewport and render visibility with Object.hide_viewport and Object.hide_render. These objects will be created with the script.

In the Object Properties tab under 'Visibility', there is a button besides the 'Show in Viewports' and 'Show in Renders' buttons that allows these settings to show up in the Dope Sheet and be animated. I've attached a photo of these buttons, highlighted in red. How can I access these 'Animate property.' buttons with Python? As in what value do they change? Nothing shows up when hovering over this button with Python Tooltips on, nor does anything show up in the scripting info view when I click this button. Or is there an alternate way to do this?

Thanks!

• If you are creating an object in Python please indicate so and show a small Python script sample. Thus at creation time you would have the variable reference for the newly created object. Object creation is really a separate question. This comment was motivated by a comment below which can be moved to the original question. – atomicbezierslinger Jan 17 at 22:05
• I know how to create objects. I'm asking if there is a Python pathway for the Animate property buttons in the Object Properties -> Visibility tab. – Ventricosa Jan 17 at 22:07
• Was the newly created was linked into the scene? Can you show your sample? If you are just changing a value then the Blender UI should update without your assistance. If you feel you must reach the UI button I will let some other reader assist you. I tend to change values and curves points not UI buttons. – atomicbezierslinger Jan 17 at 22:09
• If your question is about [keyframe] CRUD create, read, update, delete please add that to your original question. – atomicbezierslinger Jan 17 at 22:16
• The data_path you need to use for an fcurve is 'hide_viewport' and 'hide_render' if that's what you're asking. – scurest Jan 17 at 23:20

Those are called decorators

Clicking on the decorator dot icon will add a Keyframe to that property. Clicking the rhombus icon again will remove the keyframe. A solid rhombus icon indicates there is a keyframe on the current frame, while a non-solid rhombus icon indicates that the property has a keyframe on another frame. Clicking the non-solid rhombus icon will add a keyframe to the current property value and frame.

They reflect the state of the assigned action, so instead of "using them" from Python, you can just change the action. For example

import bpy
ob = bpy.context.active_object
assert ob.animation_data is None  #assume it's not animated yet
ob.animation_data_create()
ac = bpy.data.actions.new('Visibility Action')
ob.animation_data.action = ac
fc = ac.fcurves.new(data_path='hide_viewport')
fc.keyframe_points.foreach_set('co', [0,0,10,1])  #frame 0 = show, frame 10 = hide

• This answers my question, thanks! I've been going about it a totally different way, making adjustments to an object's properties, then inserting a keyframe for that property with my_ob.keyframe_insert(data_path="name_of_property_path", frame=num_of_frame). – Ventricosa Jan 18 at 2:16
• I noticed with this method, If I add more than 2 keyframe points, I have to add the line fc.update() to make it function predictably. If not, looking in the graph editor, the tangents/"Bézier handles" of the keyframe points will all extend to [0, 0], warping the shape of the curves. – Ventricosa Jan 20 at 4:55

Keyframe Insert / Delete.

Further to the answer of @scurest

Mouse over the property after setting to refresh the panel

Every object in blender has a keyframe_insert and keyframe_delete method. It does everything required, eg if the object has no animation data or action it adds them, then adds the keyframe.

Set the value, insert the keyframe. eg hide the object at frame 10

>>> C.object.hide_viewport = True
>>> C.object.keyframe_insert('hide_viewport', frame=10)
True


Can pass the frame, or it defaults to the current frame of the scene.

• This is actually the method I was using before asking this question. However, keyframe_insert was not working to animate the visibility of objects unless - what I now know is called a decorator - was clicked. At least in Blender 2.9 for Mac OS X, I couldn't find the python path to 'press' the decorator/activate the animateability of visibility properties through script. – Ventricosa Jan 18 at 2:46
• No issue on Ubuntu The edit added hides the object at frame 10. – batFINGER Jan 18 at 2:56
• Well I realized my mistake and keyframe_insert is in fact working now... It was a dumb mistake. – Ventricosa Jan 18 at 2:57
• single keyframe? once off always off? – batFINGER Jan 18 at 2:58
• I thought my keyframe_insert method was inserting a keyframe for any changed data paths rather than just one specific data path. So I didn't make a keyframe_insert for hide_viewport/hide_render specifically... I put them in my code a while ago for other data paths but kind of forgot how it worked, I guess. – Ventricosa Jan 18 at 3:02

This indicates whether or not a keyframe exists for this property, and is also a toggle to create or delete a keyframe on the current frame in the timeline.

'Animate property' is maybe not the most helpful tooltip.

bpy.context.object.hide_viewport = True
bpy.context.object.hide_render = False

#Python Console
>>> bpy.context.object
bpy.data.objects['Text.023']

bpy.data.objects['Text.023'].hide_viewport
True

>>> bpy.data.objects['Text.023'].hide_viewport = False


Of course in the script you would refer to a particular object or sequence of objects by name or as part of a collection

Strongly related. Selecting an object via scripting?

• I know how to turn on and off the visibility of an object with scripting, but apparently this cannot be keyframed by default. All with a script, I need to create an object, then allow the visibility to be keyframed with the 'Animate property.' button shown in the image, and then the visibility settings can be recorded on the timeline. But I can't find a path to activate the Visibility -> Viewports/Renders -> 'Animate property.' button via script. – Ventricosa Jan 17 at 21:58
• You seemed to have changed the question. If you move your comment to your original question others can more easily see it. I will comment on your original question. – atomicbezierslinger Jan 17 at 22:03
• I edited for (hopefully) clarity, but the question is the same. But I can add my comment here to my main question too. – Ventricosa Jan 17 at 22:10