0
$\begingroup$

I made the following add-on by the test and tried it with Blender2.8.

import bpy

bl_info = {
    "name": "modalTest",
    "author": "taichi",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "3DView > Add > Mesh",
    "description": "modalTestOperator",
    "warning": "",
    "support": "COMMUNITY",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Object"
}

class modalTest_OT_Operator(bpy.types.Operator):
    bl_idname = "wm.modal_test_operator"
    bl_label = "Modal Test Operator"
    _timer = None
    def __init__(self):
        self.s = None
    def modal(self, context, event):
        if event.type in {'RIGHTMOUSE', 'ESC'}:
            self.cancel(context)
            return{'CANCELLED'}
        if event.type=='TIMER':
            print('test')
        return{'PASS_THROUGH'}
    def execute(self, context):
        self.socketFunction()
        wm = context.window_manager
        self._timer = wm.event_timer_add(1.00/200.00, window=context.window)
        wm.modal_handler_add(self)
        return {'RUNNING_MODAL'}
    def cancel(self, context):
        wm = context.window_manager
        wm.event_timer_remove(self._timer)

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

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

if __name__ == "__main__":
    register()

bpy.ops.wm.modal_test_operator()

I checked the following buttons and tried to activate the add-on. I got the following error:

Traceback (most recent call last):
  File "C:\Users\taichi\Documents\Blender\2.80\scripts\modules\addon_utils.py", line 351, in enable
    mod = __import__(module_name)
  File "C:\Users\taichi\Documents\Blender\2.80\scripts\addons\modalTest.py", line 49, in <module>
    bpy.ops.wm.modal_test_operator()
  File "C:\Users\taichi\Documents\Blender\2.80\scripts\modules\bpy\ops.py", line 194, in __call__
    BPyOpsSubModOp._view_layer_update(context)
  File "C:\Users\taichi\Documents\Blender\2.80\scripts\modules\bpy\ops.py", line 157, in _view_layer_update
    view_layer = context.view_layer
AttributeError: '_RestrictContext' object has no attribute 'view_layer'

How can I fix the error?

$\endgroup$
1
$\begingroup$

You need to indent your call to bpy.ops.wm.modal_test_operator() so it will only be executed when the code as run as a script, which is the case when __name__ == "__main__". Otherwise you're trying to access blend data when the add-on isn't registered, which results in the exception you're seeing. For more information about this topic take a look at this answer by ideasman42. The corrected code is:

if __name__ == "__main__":
    register()
    bpy.ops.wm.modal_test_operator()

The current script won't work though because there is no socketFunction() in modalTest_OT_Operator.

$\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.