I have the following register function:

def register():

It'll work if I run the code from Blender's Text Editor, but will give the following error if I try to use it as add-on:

AttributeError: '_RestrictContext' object has no attribute 'scene'

Is there a way of running my modal operator on Blender's start (or on opening new file)?

  • $\begingroup$ You don't need to put bpy.ops.object.modal_operator('INVOKE_DEFAULT') in register function. $\endgroup$ – Alex Apr 22 '19 at 3:27
  • $\begingroup$ But how should I start my modal operator then? $\endgroup$ – Den Den Apr 22 '19 at 21:05

You can use a handler to start your operator after a blend file is loaded, ensure the handler function is marked as persistent to keep it active between file loads.

This is of limited use on blender start, it will work if the addon is enabled and the splash screen is not shown on startup. If the splash screen is shown the operator does work but the display is not updated while the splash screen is visible, dismissing the splash screen also stops the operator.

from bpy.app.handlers import persistent

def load_handler(dummy):

def register():

def unregister():
| improve this answer | |
  • $\begingroup$ Thanks, that works. However I don't quite understand about the splash screen. Could you provide a little bit more detail about it? As I understand from your answer, my operator should not work after dismissing the splash screen. But I've tested it and everything works just fine. $\endgroup$ – Den Den Apr 25 '19 at 21:31
  • 1
    $\begingroup$ Actually it would depend on the operator. In my test the 3D view didn't update while the splash screen was present. The same click or key press that dismisses the splash screen is also received by the operator, if the same event is set to cancel or stop the operator then it stops at that point. $\endgroup$ – sambler Apr 26 '19 at 3:23
  • $\begingroup$ Got it. Thanks! $\endgroup$ – Den Den Apr 26 '19 at 16:33

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.