Error when activating add-on

by taichi   Last Updated October 13, 2019 11:15 AM

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?



Related Questions


Updated March 26, 2015 07:27 AM

Updated April 04, 2015 21:06 PM

Updated April 17, 2015 20:06 PM

Updated March 28, 2015 11:05 AM

Updated March 29, 2015 06:05 AM