The draw callback hack.
There are a number of ways to make a script run constantly, via scene update handlers, modal operators, and draw callbacks to name a few.
The scene update handlers run a squi-billion-times a second which IMO is overkill. A modal timer, akin to one in text editor > templates > python > Modal Timer Operator
would do the trick, but has some downside as it potentially blocks auto-save, and other functionality while running.
The draw callback, primarily designed for bgl
and blf
displays can be used.
Example, left in the blf
which writes on 3d view. The frame is set using the active layer.
import bpy
import blf
#from gi.repository import Gdk
class DrawingClass:
def __init__(self, context, prop):
self.prop = prop
self.handle = bpy.types.SpaceView3D.draw_handler_add(
self.draw_text_callback,(context,),
'WINDOW', 'POST_PIXEL')
def draw_text_callback(self, context):
font_id = 0 # XXX, need to find out how best to get this.
# draw some text
blf.position(font_id, 15, 50, 20)
blf.size(font_id, 20, 72)
blf.draw(font_id, "%s %s" % (context.scene.name, self.prop))
scene = context.scene
layer = scene.active_layer
frame = (layer + 1) * 10 + 1
if scene.frame_current - frame:
scene.frame_set(frame)
def remove_handle(self):
bpy.types.SpaceView3D.draw_handler_remove(self.handle, 'WINDOW')
if __name__ == "__main__":
# test call
dc = DrawingClass(bpy.context, "Auto Layer Running......")
Hint: If you give the script a .py
extension in text editor you can import it into other scripts / console etc.
context.scene.frame_set(frame)
to set the current frame toframe
. $\endgroup$