0
$\begingroup$

I want, for example, to move the context.object every 10 minutes automatically. So no action needed, if I run my script the first time it will activate this incremental action, and if I run it a second time it will stop listening.

Is it even possible to do those kind of loop actions ?

I know we can get time information with

import datetime
time = datetime.datetime.now()
date= time.strftime(" %Y-%m-%d_%HH%MM%S")

Thanks to RUben for this answer, so i had another problem, once i launch a timer, impossible to stop it with another run of any code.

this for example

def every_1_seconds_word():
    print("World")
    return 1.0

bpy.app.timers.register(every_1_seconds_word)

but i can break the timer if it respect a condition

def every_1_seconds_word():
    print("World")
    if it.respect.the.condition.im.writing.right.now == n:
        return None
    return 1.0

bpy.app.timers.register(every_1_seconds_word)

but i don't know yet how to do this because i need to create a new condition line within blender itself, it need to be accessible on every run of the code, wso why not using an existing one ? for example with:

def every_1_seconds_word():
    print("World")
    if bpy.context.scene.cycles.preview_start_resolution == 9999:
        return None
    return 1.0

bpy.app.timers.register(every_1_seconds_word)

by using the start resolution parameters for example, i can create another code that can play on this to break my timer.

A = bpy.context.scene.cycles.preview_start_resolution
bpy.context.scene.cycles.preview_start_resolution = 9999

def in_1_seconds():
    bpy.context.scene.cycles.preview_start_resolution = A
bpy.app.timers.register(in_1_seconds, first_interval=1)

this is quite a macgyver solution but well it work

$\endgroup$
1
$\begingroup$

According to Application Timers documentation for Blender 2.80 it quite simple:

def in_1_seconds(obj):
    if obj.location.y > 0.5:
        return None
    else:
        obj.location += Vector((0.0, 0.1, 0.0))
        return 1

bpy.app.timers.register(functools.partial(in_1_seconds, bpy.context.active_object))

functools used to anchor active object to timer

timer function returns a float with seconds to next delay which may be changed dynamically

return None to stop function execution in test purpose it can be Keyboard Interrupted

$\endgroup$
  • $\begingroup$ Thanks for the link and explanation ! how do we unregister a perpetual timer once it is register ? $\endgroup$ – DB3D May 20 at 22:27
  • $\begingroup$ existing API implementation Timer cannot be cleared apart from itself $\endgroup$ – RUben May 21 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.