0
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$ Commented Aug 28, 2020 at 23:59

1 Answer 1

0
$\begingroup$

Some edits

Give operator a poll method so it requires an active object of type mesh (or a type that can have decimate modifier)

Add the modifier using the api method.

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"
     
    @classmethod 
    def poll(cls, context):
        ob = context.active_object
        return ob and ob.type == 'MESH'
    
    def execute(self, context):
        ob = context.active_object
        dm = ob.modifiers.new(name="Foo", type='DECIMATE')
        
        dm.decimate_type = 'UNSUBDIV'
        dm.iterations = 1
        bpy.ops.object.modifier_apply(modifier=dm.name)
        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()
$\endgroup$
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.