0
$\begingroup$

Can anyone tell me why Blender thinks that object type Object does not have attribute modifier_add?

When you check what code Blender use when you manually add any modifier to object, it is this (in this case Bevel modifier):

bpy.ops.object.modifier_add(type='BEVEL')

So I can clearly see object type of Object does have such attribute, right?

But when I do that in my code it puts error message about non-existent attribute of modifier_add...this is my add-on code (it basically adds 3 different modifiers with specific values to any selected object, so that edges look little curved and smooth):

bl_info = {
    "name": "Add Beveling Modifiers To Selected Objects",
    "author": "me",
    "version": (1, 0),
    "blender": (2, 90, 1),
    "location": "View3D > Object > Add Beveling Modifiers To Selected",
    "description": "Add bunch of modifiers that will bevel and smooth edges in selected objects",
    "warning": "",
    "doc_url": "",
    "category": "Object",
}

import bpy
from bpy.types import (
    AddonPreferences,
    Operator,
    Panel,
    PropertyGroup,
)

class OBJECT_OT_addbevelingmodifierstoselected(Operator):
    bl_label = "Add Beveling Modifiers To Selected"
    bl_idname = "object.add_beveling_modifiers_to_selected"
    bl_description = "Add bunch of modifiers that will bevel and smooth edges in selected objects"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_options = {'REGISTER', 'UNDO'}
    
    # ONLY IF SOMETHING IS SELECTED AND IT IS A MESH
    @classmethod
    def poll (cls, context):
        return context.object.select_get() and context.object.type == 'MESH'
    
    def execute (self, context):
        
        # ===== ACTUAL ACTIONS START HERE ===== #
        for ob in bpy.context.selected_objects:
            bpy.context.view_layer.objects.active = ob
            
            # BEVEL
            ob.modifier_add(type='BEVEL')
            bevel = ob.modifiers["Bevel"]
            bevel.affect = 'EDGES'
            bevel.offset_type = 'WIDTH'
            bevel.width = 0.02
            bevel.segments = 1
            bevel.limit_method = 'ANGLE'
            bevel.angle_limit = 1.48353
            bevel.profile_type = 'SUPERELLIPSE'
            bevel.profile = 1
            bevel.miter_outer = 'MITER_SHARP'
            bevel.miter_inner = 'MITER_SHARP'
            bevel.vmesh_method = 'ADJ'
            bevel.use_clamp_overlap = False
            bevel.loop_slide = True
            bevel.harden_normals = True
            bevel.mark_seam = True
            bevel.mark_sharp = True
            bevel.material = -1
            bevel.face_strength_mode = 'FSTR_NONE'
            
            # WEIGHTED NORMAL
            ob.modifier_add(type='WEIGHTED_NORMAL')
            weightednormal = ob.modifiers["WeightedNormal"]
            weightednormal.mode = 'FACE_AREA_WITH_ANGLE'
            weightednormal.weight = 20
            weightednormal.thresh = 0.01
            weightednormal.keep_sharp = True
            weightednormal.face_influence = False
            weightednormal.vertex_group = ""
            
            # SUBDIVISION
            ob.modifier_add(type='SUBSURF')
            subdivision = ob.modifiers["Subdivision"]
            subdivision.subdivision_type = 'CATMULL_CLARK'
            subdivision.levels = 1
            subdivision.render_levels = 1
            subdivision.show_only_control_edges = True
            subdivision.quality = 1
            subdivision.uv_smooth = 'PRESERVE_CORNERS'
            subdivision.use_creases = True
            subdivision.use_custom_normals = False

        # ===== ACTUAL ACTIONS END HERE ===== #
        
        return {'FINISHED'}
    
def menu_func_addbevelingmodifiers (self, context):
    self.layout.operator(OBJECT_OT_addbevelingmodifierstoselected.bl_idname)

def register():
    bpy.utils.register_class(OBJECT_OT_addbevelingmodifierstoselected)
    bpy.types.VIEW3D_MT_object.append(menu_func_addbevelingmodifiers)
    
def unregister():
    bpy.utils.unregister_class(OBJECT_OT_addbevelingmodifierstoselected)
    bpy.types.VIEW3D_MT_object.remove(menu_func_addbevelingmodifiers)

if __name__ == "__main__":
    register()
$\endgroup$
7
  • 1
    $\begingroup$ It doesn't think it, it knows that bpy.types.Object`` doesn't have a modifier_add` property. You are mixing apples and oranges, or in this case objects and operators. $\endgroup$
    – batFINGER
    Nov 3 '20 at 10:59
  • 1
    $\begingroup$ See blender.stackexchange.com/questions/71569/… $\endgroup$
    – batFINGER
    Nov 3 '20 at 10:59
  • $\begingroup$ OK, but can you then make some example what part, or better said how the wrong part should look? You see, I am newbie and basically re-using my other addon script that did work (I post it somewhere around it here too before), thus I thought it would work if I only replace the "working part" itself $\endgroup$
    – qraqatit
    Nov 3 '20 at 11:01
  • $\begingroup$ yes I know that - thanks, but I have no clue how to make it in for loop for every selected object, therefore I thought I am doing the right thing which in turns seems to be not right $\endgroup$
    – qraqatit
    Nov 3 '20 at 11:01
  • $\begingroup$ Go to console print(bpy.ops.object) vs print(C.object) And if you know it, do it. $\endgroup$
    – batFINGER
    Nov 3 '20 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.