Getting error writing my first add-on for blender

Here’s my code

bl_info = {
“name”: “My Do Everything Script”,
“author”: “Your Name Here”,
“version”: (1, 0),
“blender”: (2, 80, 0),
“location”: “View3D > Add > Mesh > New Object”,
“description”: “Adds a new Mesh Object”,
“warning”: “”,
“doc_url”: “”,
“category”: “Add Mesh”,
}

import bpy

bpy.ops.object.shade_smooth()
bpy.context.object.data.use_auto_smooth = True
bpy.context.object.active_material.show_transparent_back = False

I’m trying to run this in blender 3.0.1 I’m trying to install this as an add on per tutorial by Ian Hubert about making your first addon …I’m getting this error message

File “C:\Program Files\Blender Foundation\Blender 3.0\3.0\scripts\modules\bpy\ops.py”, line 88, in _view_layer_update
view_layer = context.view_layer
AttributeError: ‘_RestrictContext’ object has no attribute ‘view_layer’

any help would be appreciated

you can’t use context in the global addon scope, you have to wait for the addon to be fully initialized before you can use context. the easiest way to do this is with a timer.

That said, looking at your code that is not likely what you’re going to want to do, since it would only ever run a single time (when you activated the addon) and never again until you restarted Blender. I would suggest taking a look at the script examples that come with Blender. They are in the scripting workspace under the “Templates” menu of the script editor. There are plenty of easy to follow examples of how to do very basic things with the Blender API.

as a side note- anytime you post code be sure to use the correct formatting, otherwise people tend to ignore your question because it’s a pain in the ass to read. you can use three backticks at the start and end of your code block: ```

def example():
    print("Like this")
1 Like

@ mjoe67886

When referencing external material such as “per tutorial by Ian Hubert” it would also be useful to include a link to the specific external reference.

In your code:

import bpy

bpy.ops.object.shade_smooth()
bpy.context.object.data.use_auto_smooth = True
bpy.context.object.active_material.show_transparent_back = False

Would potentially function as a script run from the text editor but as @ testure mentions an addon requires initialization. Within your specific code as a script you should also be aware that there could still be issues since you currently have no verification that an object is selected and active, or that the object has a material assigned to it.

As for scripting tutorials I would recommend watching the series from Blender Blender Collections | Scripting for Artists [6] - YouTube by Dr. Sybren Stuvel

1 Like