Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

July 21, 2011

scripted keyframing of hide / hide render setting on object

###### place at t = T_START-1 a keyframe with hide set to True
current_frame = T_START-1
bpy.ops.anim.change_frame(frame=current_frame)
bpy.context.active_object.hide = True
bpy.context.active_object.keyframe_insert(  data_path="hide", 
                                            index=-1, 
                                            frame=current_frame)    
    
###### place at T_START a keyframe with hide set to False
current_frame = T_START
bpy.ops.anim.change_frame(frame=current_frame)
bpy.context.active_object.hide = False
bpy.context.active_object.keyframe_insert(  data_path="hide", 
                                            index=-1, 
                                            frame=current_frame)
It's handy to make keyframes first as experiment for lateron when you try to script something, if you can't get the basics going manually don't expect to be able to script it without understanding the sequence of operations required.

If you have a property that you want to keyframe, then you will need to know the data_path. In the code example below i choose to keyframe the 'hide' property. If you are unsure about the string name needed for data_path, then rightclick the property/icon and select "copy data_path". Then paste that into ( console / TextEditor / your code ) to see the path.

if you need to remove all your animation data use action-data-how-to-purge.html

you might set up a hide animation like this
( side note: data_path='hide' hides from view, not from render.. that's data_path='hide_render' ).

# For instance you first define T_START, T_END and LAST_FRAME
# T_START and T_END as the first and last frames that the object will be visible in.
# Having an additional keyframe on either side of those frames allows you to play
# the sequence backwards without messing up the hide duration.  
# "hide" ( hides from view ) use "hide_render" to make it apply to render.
    time_and_state_settings = ( (0, True), 
                                (T_START-1, True),
                                (T_START, False),
                                (T_END, False),
                                (T_END+1, True),
                                (LAST_FRAME, True))

    for time_val in time_and_state_settings:    
        current_frame = time_val[0]
        bpy.ops.anim.change_frame(frame=current_frame)
        bpy.context.active_object.hide = time_val[1]
        bpy.context.active_object.keyframe_insert(  data_path="hide", 
                                                    index=-1, 
                                                    frame=current_frame)
    

7 comments:

  1. Thank you so so much for sharing this code! I'd been stuck at it for weeks!

    ReplyDelete
  2. My pleasure Priyanka, i'd be happy to see what you can make from it.

    ReplyDelete
  3. I was working on this human armature, and I basically created a different one for each frame so I was looking for a hide setting that would make it seem like a continuous motion. And it did work, the animation looked perfect.

    But because blender doesn't render armature, I had to create a mesh around my bones, and now that I did that, the hide setting doesn't seem to work anymore.

    This might be too assumptious of me but I already emailed you my current code. I would really appreciate some help!

    ReplyDelete
  4. This is perfect for my needs. However I'm having a big of an issue. In Blender 2.61.0, I adapted your first code example a little bit.

    Basically, the lines:
    (1): "bpy.context.active_object.hide = True" and
    (2): "bpy.context.active_object.hide_render = True"

    are not behaving the same. Line (1) behaves as you have described, inserting a keyframe where necessary. Line (2) simply turns renderability off entirely.

    Is there a solution to this? Any recommendations of what to do correctly?

    ReplyDelete
  5. Write the shortest amount of code that you can, in order to demonstrate the problem. This will show you and anyone following this that you have understood the problem. Use primitives to make the concepts visible. Then if you wish I will take a look at that code :). Use a service like pasteall/python or pastebin to copy the code.

    ReplyDelete
  6. Dealga, I figured it out. It turns out the issue was completely unrelated to this. I figured that out when I was writing a snippet to post here.

    Sometimes I get fixated on fixing something that isn't broken, because I don't realize that it something else is the culprit. Reminds me of the quote: "During the space race, the US invested in making a pen that would work in zero gravity. They eventually succeeded. Meanwhile the Russians used a pencil."

    Anyways, thanks for posting this here. Toggling renderability will make my script *much* more efficient in terms of rendering.

    ReplyDelete
    Replies
    1. You realized something more important than the solution to your original dilemma! A method by which to attack all failing code. Good :)

      Delete

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.