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()
bpy.types.Object`` doesn't have amodifier_add` property. You are mixing apples and oranges, or in this case objects and operators. – batFINGER Nov 03 '20 at 10:59print(bpy.ops.object)vsprint(C.object)And if you know it, do it. – batFINGER Nov 03 '20 at 11:04