0
$\begingroup$

I need to start animation and stop it in 30 sec command to stop animation is the same as start, so I'm activating animation before setting delay, and I want it to stop in 30 sec, but its not working for me. Any help would be highly appreciated

from threading import Timer

def  play_animation():
    bpy.ops.screen.animation_play()

play_animation()


t = Timer(30.0, play_animation)
t.start()
$\endgroup$
4
  • $\begingroup$ blender.stackexchange.com/questions/135970/… $\endgroup$ – batFINGER Jul 23 '19 at 17:29
  • $\begingroup$ It gives an error: Traceback (most recent call last): File "<blender_console>", line 3, in <module> AttributeError: 'bpy.app' object has no attribute 'timers' $\endgroup$ – cxnt Jul 24 '19 at 10:44
  • $\begingroup$ Could it be possible that you are using version 2.7x or prior. Timers were added in 2.80. $\endgroup$ – batFINGER Jul 24 '19 at 12:43
  • $\begingroup$ I switched to 2.80, but its still not working. UPD: im not getting this error anymor, but this script isnt doing what I need $\endgroup$ – cxnt Jul 24 '19 at 12:47
1
$\begingroup$

Override the context for timer funcs

As discussed here Context is incorrect when calling from a timer the timer is outside the context of the main thread of blender.

To get around this pass the objects to the function when defined, and use these in a context override of any operators.

A quick test reveals that the animation cancell operator requires context members "window" and "screen"

import time, bpy, functools
from bpy import context

def stop(w):
    print("STOP")
    c = {"window" : w,
         "screen" : w.screen
         }
    bpy.ops.screen.animation_cancel(c, restore_frame=False)
    return None


#f = functools.partial(loop, bpy.context.screen.name)

if __name__ == '__main__':
    window = bpy.context.window
    bpy.ops.screen.animation_play()

    bpy.app.timers.register(functools.partial(stop, window), first_interval=30)

The same method could probably be employed with threading.Timer As a rule of thumb, threading and blender don't go together too well. The new timer app was created to alleviate this somewhat. But since it is still a thread outside blenders main thread it has issues with keeping context.

Please notice the way the index of the window is passed in answer to linked question. Simply passing the window as shown here could be catastrophic if the window is closed, and hence the reference lost, between invoking timer and it firing.

$\endgroup$

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.