I'm trying to make an add-on with this code but keep getting this error. I've tried putting this line obj = bpy.context.object in execute but then i'll get another error wich i've sollved by putting this line outside of execute. Any help please?
bl_info = {
"name": "test",
"author": "Aziz",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "...",
"description": "...",
"warning": "",
"doc_url": "",
"category": "Mesh",
}
import bpy
class ReducePolygons(bpy.types.Operator):
bl_idname = "reduce.polygons"
bl_label = "Reduce Polygons"
obj = bpy.context.object
def execute(self, context):
bpy.ops.object.modifier_add(type='DECIMATE')
self.obj.modifiers["Decimate"].decimate_type = 'UNSUBDIV'
self.obj.modifiers["Decimate"].iterations = 1
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Decimate")
return {'FINISHED'}
class LayoutDemoPanel(bpy.types.Panel):
bl_label = "test"
bl_idname = "SCENE_PT_layout"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def draw(self, context):
layout = self.layout
layout.label(text="Mesh Optimizations:")
row = layout.row()
row.scale_y = 2.0
row.operator("reduce.polygons")
def register():
bpy.utils.register_class(LayoutDemoPanel)
bpy.utils.register_class(ReducePolygons)
def unregister():
bpy.utils.unregister_class(LayoutDemoPanel)
bpy.utils.unregister_class(ReducePolygons)
if __name__ == "__main__":
register()
I've tried putting this line obj = bpy.context.object in execute but then i get this error message on launch:
'NoneType' object has no attribute 'modifiers'
Also tried deleting this line obj = bpy.context.object and replacing self.obj with context.object as Robert Gützkow suggested but unfortunately i got the same AttributeError.
Any idea on how to fix this?