3
$\begingroup$

зThere is an operator “A_TimeSincePriorHotkey” in the autohokey program. It returns the time elapsed since the last call of this function. With this operator it is very easy to work with double clicks or repeated function calls in a short period (in fact, in any period you set). In Blender I've seen descriptions of timers, but no similar operators. Can you suggest some analog for Blender please ?

#IfWinActive, ahk_class ZBrush
2::
If (A_PriorHotKey = "2" AND A_TimeSincePriorHotkey < 200)
{   
    print("Hello")
    return
}
print("World")
return

In this short code snippet, if the user presses button "2" and less than 200 milliseconds have elapsed since the last press of that button, the word "Hello" will be printed, if more than 200 milliseconds have elapsed, the word "World" will be printed. I am looking for an analog of a mechanism that counts down between presses or function calls.

$\endgroup$
2
  • 1
    $\begingroup$ Hello, could you be a bit more specific about what exact function you want to time ? There are dozens (if not hundreds) of functions that are called every second in Blender $\endgroup$
    – Gorgious
    Commented Jun 9 at 18:56
  • $\begingroup$ I think the closest to it would be searching through the "INFO" area contents. $\endgroup$ Commented Jun 9 at 20:54

1 Answer 1

1
$\begingroup$

I haven't found a function like that yet. Here is a working variant using the time module. We get the current time in ms and store it in the operator attribute.

import bpy
import time    
class KEY_Dopesheet(bpy.types.Operator):
    """Toggle Dopesheet,ActionEditor,ShapeKeyEditor,Timeline"""
    bl_idname = "object.key_dopesheet"
    bl_label = "KEY_Dopesheet"
    key_dopesheet_switch = 0
    lasttime = 0
    
    def execute(self, context):
        op = bpy.types.OBJECT_OT_key_dopesheet      
        # current time in ms
        epoch_time = round(time.time() * 1000)
        uitype = bpy.context.area.ui_type
        if(epoch_time - op.lasttime < 700):
            if (op.key_dopesheet_switch == 0):
                op.key_dopesheet_switch = 1
                bpy.context.space_data.mode = 'DOPESHEET'
            elif(op.key_dopesheet_switch == 1):
                op.key_dopesheet_switch = 2
                bpy.context.space_data.mode = 'ACTION'
            elif(op.key_dopesheet_switch == 2):
                op.key_dopesheet_switch = 3
                bpy.context.space_data.mode = 'SHAPEKEY'
            elif(op.key_dopesheet_switch == 3):
                op.key_dopesheet_switch = 4
                bpy.ops.screen.space_type_set_or_cycle(space_type='NLA_EDITOR')
            else:
                op.key_dopesheet_switch = 0
                bpy.context.area.ui_type = 'TIMELINE'
        else:
            bpy.context.area.ui_type = 'DOPESHEET'
            bpy.context.space_data.mode = 'DOPESHEET'
            op.key_dopesheet_switch = 1

        op.lasttime = epoch_time
        return {'FINISHED'}
$\endgroup$
1
  • $\begingroup$ Keep in mind since you're not using Blender serializable properties this may yield funky results if you undo / redo or save / load $\endgroup$
    – Gorgious
    Commented Jul 16 at 6:48

You must log in to answer this question.

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