2
$\begingroup$

I'm creating a stop motion animation that requires to Insert Keyframes of visibility. I was attempting to work out a script to make this process faster for me, but being new to python this has evaded me. Any help would be appreciated.


My work flow goes as:

  1. Select object (with mouse)
  2. Insert key frame on both viewport and rendering visibility
  3. Hide Viewport bpy.context.object.hide = True
  4. Hide Rendering bpy.context.object.hide_render = True
  5. Move Next Frame
  6. Un-hide Viewport bpy.context.object.hide = False
  7. Un-hide Rendering bpy.context.object.hide_render = False
  8. Insert key frame on both viewport and rendering visibility

Update based on accepted answer

Other then the following comments, I can't find any other issues.

  • If you run the script multiple times It will continue to add buttons.
  • Also I agree with you on Just inserting a keyframe instead of hiding and adding a keyframe.
  • Is there any way to apply the Hide keyframes to multiple items at once? It seems only the last selected item receives the keyframe change.

enter image description here

Also once again a huge thanks, here is a frame of how it turned out! Multiple Instances can be ran if script is ran more then once

$\endgroup$
2
$\begingroup$

This adds a button to the 3D View tool panel, Animation tab:

import bpy

class OBJECT_keyframe_insert_hide(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.keyframe_insert_hide"
    bl_label = "(Un)hide"
    bl_options = {'REGISTER', 'UNDO'}

    hide = bpy.props.BoolProperty(name="Hide")

    @classmethod
    def poll(cls, context):
        return context.object is not None

    def execute(self, context):
        ob = context.object
        ob.hide = self.hide
        ob.hide_render = self.hide
        ob.keyframe_insert("hide")
        ob.keyframe_insert("hide_render")
        return {'FINISHED'}


def draw_func(self, context):
    layout = self.layout
    hidden = context.object.hide

    layout.label("Stop motion:")
    layout.operator(OBJECT_keyframe_insert_hide.bl_idname, text="Unhide" if hidden else "Hide", icon='KEY_HLT').hide = not hidden

def register():
    bpy.utils.register_class(OBJECT_keyframe_insert_hide)
    bpy.types.VIEW3D_PT_tools_animation.append(draw_func)


def unregister():
    bpy.utils.unregister_class(OBJECT_keyframe_insert_hide)
    bpy.types.VIEW3D_PT_tools_animation.remove(draw_func)


if __name__ == "__main__":
    register()

It hides or unhides the active object and keyframes the .hide / .hide_render properties.

Insert Keyframes for hide and hide_viewport (toggle)

$\endgroup$
  • $\begingroup$ This is perfect for what I am looking for. Thank you so much for a quick and thorough answer. $\endgroup$ – Kyle Martin Sep 24 '14 at 14:43
  • $\begingroup$ You're welcome. I would like to hear any issues in usability if there are any. Maybe it would be nicer if it didn't hide the object on the initial button press when there are no keyframes yet, but inserted a keyframe only? $\endgroup$ – CoDEmanX Sep 24 '14 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.