2
$\begingroup$

Want to make a button in Blender which requires to jump in a specific frame and then again jump into another frame according to the requirement.

$\endgroup$
1
  • 5
    $\begingroup$ Your question is confusing. Please use the edit link at the bottom of your post (i.stack.imgur.com/lXFuK.png) and try to clarify your question. Make what jump? Is your question how to move to the next keyframe? Is this to make an object jump interactively on the game engine? what do you mean by "according to the requirement"? $\endgroup$
    – user1853
    Mar 23, 2018 at 17:52

1 Answer 1

4
$\begingroup$

All you really need to do is change the current frame (via frame_set).

import bpy

C = bpy.context
jump = 5 #however many frames you want to jump
C.scene.frame_set(C.scene.frame_current + jump)

These few lines will move the playhead 5 frames ahead. That is all it takes to do that. To make the button takes a bit more python.


This script creates a property to store how many frames you want to jump. It creates an operator, which does nothing more then the script above. Finally it adds a button and number input to the header of the timeline.

New UI added to the timeline header

import bpy

class FrameOperator(bpy.types.Operator):
    """Jumps from the curent frame the amount set in scene.frame_jump"""
    bl_idname = "frame.jump"
    bl_label = "Frame Jump Operator"

    bpy.types.Scene.frame_jump = bpy.props.IntProperty(name="Frame Jump Count", default=5, min=-499999, max=499999, description="Amount to jump from the current frame. Can be negative. Used in bpy.ops.frame.jump()")

    def execute(self, context):
        bpy.context.scene.frame_set(bpy.context.scene.frame_current + bpy.context.scene.frame_jump)
        return {'FINISHED'}


def draw_frame_jump(self, context):
    scene = context.scene
    layout = self.layout

    layout.prop(scene, "frame_jump", text="")
    if scene.frame_jump > 0:
        icon = "TRIA_RIGHT_BAR"
    else:
        icon = "TRIA_LEFT_BAR"
    layout.operator("frame.jump", text="Jump " + str(scene.frame_jump) + " Frames", icon=icon)

def register():
    bpy.types.TIME_HT_header.append(draw_frame_jump)
    bpy.utils.register_class(FrameOperator)

def unregister():  
    # remove header menu
    bpy.types.INFO_HT_header.remove(draw_frame_jump)
    bpy.utils.unregister_class(FrameOperator)

if __name__ == "__main__":
    register()

A more detailed walk through of this script.
class FrameOperator this is where I'm defining the new operator. The doc string (first line triple quoted) is the tooltip. The bl_idname is how blender access it, in this case bpy.ops.frame.jump().

The bpy.types.Scene.frame_jump line is very important. This creates a new integer property, it is this property that hold the amount bpy.ops.frame.jump() will use to jump.
The execute function is what gets run when the bpy.ops.frame.jump() operator is called. It is in here that you can see the same code from the first script.

The draw_frame_jump function holds the code that will get added to the timeline header. First I add the property I created earlier layout.prop(scene, "frame_jump", text=""). The if statement is just changes the icon in the button. The layout.operator line adds the button to run the operator. Notice the first argument "frame.jump" is the same as defined in the bl_idname.

Now we get down to the register function. This simply adds the interface code to the timeline header TIME_HT_header.append(draw_frame_jump), and registers the frame.jump operator. (Without registering an operator you can not use it in blender.)

Then it is only a matter of adding the bl_info and you have a full blown addon.

$\endgroup$
1
  • $\begingroup$ Seems like TIME_HT_header. does not exist in Blender 2.83. I used TIME_MT_editor_menus instead. $\endgroup$ Jun 14, 2020 at 4:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .