0
$\begingroup$

Trying to get a feeling for the timing of Modal Operators I did some experiments and need some advice on how to interpret them

I experimented with this script

import bpy
import time


class ModalTimerOperator(bpy.types.Operator):
    """Operator which runs its self from a timer"""
    bl_idname = "wm.modal_timer_operator"
    bl_label = "Modal Timer Operator"

    _timer = None

    def modal(self, context, event):
        if event.type in {'RIGHTMOUSE', 'ESC'}:
            self.cancel(context)
            return {'CANCELLED'}

        if event.type == 'TIMER':
            print((time.time_ns() - self.old_time)/1.0e9)
            while self.i < 10:
                self.i += 1
            self.i = 0
            self.old_time = time.time_ns()
        return {'PASS_THROUGH'}

    def execute(self, context):
        wm = context.window_manager
        self._timer = wm.event_timer_add(0.01, window=context.window)
        wm.modal_handler_add(self)
        self.old_time = time.time_ns()
        self.i = 0

        return {'RUNNING_MODAL'}

    def cancel(self, context):
        wm = context.window_manager
        wm.event_timer_remove(self._timer)


def register():
    bpy.utils.register_class(ModalTimerOperator)


def unregister():
    bpy.utils.unregister_class(ModalTimerOperator)


if __name__ == "__main__":
    register()

# test call
bpy.ops.wm.modal_timer_operator()

on my machine the minimum timing for the timer is 10ms

I then timed the while loop in the execute method of the operator

8s for counting to 1 000 000

0.8 for 100 000

0.1 for 10 000

0.01 for 1 000

0.002 for 100

0.001 for 10

OK, consistent

then I put the while loop in the modal method (as in the script above)

as long as the execution time of the while loop is less than the frequency of the timer everything is fine.

But as soon as the execution time is larger than the frequency the print statement starts to print additional 0.0!

WHY?

What is the scheduler doing?

THX Martin

$\endgroup$
2
  • $\begingroup$ Related blender.stackexchange.com/a/194818/15543 The elapsed time can be gleaned from the timer. The timer event fires every 0.01 seconds ( for wm.event_timer_add(0.01,... With the while loop where it is, it will pretty much be instant to iterate / decrement over 10, $\endgroup$
    – batFINGER
    Oct 31, 2020 at 10:19
  • $\begingroup$ @batFINGER of course the iteration to 10 will be almost instantly but as the time for the while loop increases ( increasing the count to let's say 100 000 ) the additional print statements get executed $\endgroup$
    – martburg
    Nov 4, 2020 at 17:05

0

You must log in to answer this question.

Browse other questions tagged .