2

In my Add-on's __init__.py file I'm calling my modal operator's register function, then trying to invoke it using the following code:

MyOperator.py

class GRAPH_OT_MyOperator(Operator):
    """My Operator"""
    bl_idname = "graph.my_operator"
    bl_label = "My Operator"
def modal(self, context, event):
    print("Test")
    return {'PASS_THROUGH'}

def invoke(self, context, event):
    context.window_manager.modal_handler_add(self)
    return {'RUNNING_MODAL'}

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

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

__init__.py

def register():
    my_operator.register()
    bpy.ops.graph.my_operator('INVOKE_DEFAULT')

I'm getting the following error. Can anyone please tell me how I can run this modal operator after my Add-on is registered? Thank you!

Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender 3.1\3.1\scripts\modules\addon_utils.py", line 387, in enable
    mod.register()
  File "C:\Users\myname\AppData\Roaming\Blender Foundation\Blender\3.1\scripts\addons\MyAddon\__init__.py", line 116, in register
    my_operator.register()
  File "C:\Users\myname\AppData\Roaming\Blender Foundation\Blender\3.1\scripts\addons\MyAddon\operators\my_operator.py", line 35, in register
    bpy.ops.graph.my_operator('INVOKE_DEFAULT')
  File "C:\Program Files\Blender Foundation\Blender 3.1\3.1\scripts\modules\bpy\ops.py", line 126, in __call__
    _BPyOpsSubModOp._view_layer_update(context)
  File "C:\Program Files\Blender Foundation\Blender 3.1\3.1\scripts\modules\bpy\ops.py", line 88, in _view_layer_update
    view_layer = context.view_layer
AttributeError: '_RestrictContext' object has no attribute 'view_layer'
  • 1
    https://blender.stackexchange.com/questions/7449/execute-operators-from-python-at-start-up – X Y Apr 13 '22 at 05:05
  • 1
    If you're going to have an ongoing modal operator running in the background, please take into consideration that the autosave feature will be disabled in the meantime – Gorgious Apr 13 '22 at 06:15

0 Answers0