pythonblender

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


When running my script, I get this error:

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

I'm trying to create a script for optimization:

bl_info = {
    "name": "Empic Stone",
    "author": "Null",
    "version": (0, 0, 1),
    "blender": (2, 79, 0),
    "location": "VIEW3D > ADD > Mesh",
    "description": "Empic Stone",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Add Mesh"
}

# Imports
import bpy

# Mesh - Create Cubic
bpy.ops.mesh.primitive_cube_add(location=(0.00, 0.00, 1.00))

def draw_item(self, context):
     self.layout.operator_context = 'INVOKE_DEFAULT'
     self.layout.operator(EpicStone.bl_idname, text="Epic Stone", icon="PLUGIN")

def register():
    bpy.utils.register_module(__name__)
    bpy.types.INFO_MT_mesh_add.append(draw_item)

def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.INFO_MT_mesh_add.remove(draw_item)

if __name__ == '__main__':
     register()

Solution

  • Specifying this script is intended for blender would probably help people. I've worked with it and this is the only reason why I recognized it. Anyway, normally this kind of error happens since Blender restricts access to bpy.context and bpy.data. This is from the documentation:

    This is done because there is no assurance that the data loaded when the addon is registered will be active or even exist when the user accesses operators the addon defines. If you see an exception like this, then the addon needs to be updated to access the context during execution rather then on registration:

    Now rather than "reinvent the wheel", please follow the above link and follow their code.