Setting frame to specific value using a UI panel.

I am trying to write a script that allows me to set the frame to a specific number when I press a button. However I can’t work out how to make the UI button into a button rather than a slider. Here’s my current script:

import bpy

class SetFrame (bpy.types.Panel):
    bl_label = "Set Frame"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_context = "objectmode"
    
    def draw(self, context):
        layout = self.layout
        col = layout.column(align=True)
        col.prop(bpy.context.scene, "frame_current", index=30,)
        
        
bpy.utils.register_class(SetFrame)

        
    
    

What I want it to do is create a button that I can press to set the frame number to X (say 30 for example), rather than having to input the number with a slider or something (as happens currently).

Any ideas?

Create an operator and use bpy.context.scene.frame_set(frame_number).

Thanks bdancer. I’m a big fan of your vray exporter by the way.

Here’s the finshed script. Super basic (I’ve only just started learning python), but it solved the problem I needed to solve, which made me rather happy.

import bpy
    
class SetFrame (bpy.types.Panel):
    bl_label = "Set Frame"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_context = "objectmode"
    
    def draw(self, context):
        layout = self.layout
        col = layout.column(align=True)
        col.prop(bpy.context.scene, "frame_current",)
        col.operator("setframe.thirty", text = "Set Frame 30")
     
class OBJECT_OT_setframe(bpy.types.Operator):
    bl_label = "setframe.thirty"
    bl_idname = "setframe.thirty"
    bl_description = "sets frame to frame 30"
    
    def invoke(self, context, event):
          bpy.ops.anim.change_frame(frame=30)
          return{'FINISHED'}        
            
bpy.utils.register_class(OBJECT_OT_setframe)             
bpy.utils.register_class(SetFrame)

Hi ben,

Here’s another way to do this… may be useful if you want to change to a frame other than 30.


import bpy
    
class SetFrame (bpy.types.Panel):
    bl_label = "Set Frame"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_context = "objectmode"
    
    def draw(self, context):
        layout = self.layout
        col = layout.column(align=True)
        col.prop(bpy.context.scene, "frame_current",)
        col.operator("anim.change_frame", text = "Set Frame 30").frame=30  #Not working???
        col.operator("setframe.thirty", text = "Set Frame 30").frame=30
        
class OBJECT_OT_setframe(bpy.types.Operator):
    bl_label = "setframe.thirty"
    bl_idname = "setframe.thirty"
    bl_description = "sets frame to frame 30"
    frame = bpy.props.IntProperty(default=0) 
    
    def execute(self,context):
        context.scene.frame_set(self.frame)
        #could also return bpy.ops.anim.change_frame(frame=self.frame)
        return{'FINISHED'}     
            
bpy.utils.register_class(OBJECT_OT_setframe)   
           
bpy.utils.register_class(SetFrame)

Not sure why the “col.operator(“anim.change_frame”, text = “Set Frame 30”).frame=30” isn’t working.