I'm trying to apply my modifier to object with Python. I've set the object of interest as the active object and ensured its mode to be object. However, I still receive this error.
Here's the relevant code.
bpy.context.scene.objects.active = obj
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.modifier_add(type='SUBSURF')
bpy.ops.object.modifier_apply(modifier='Subsurf', apply_as='DATA')
RuntimeError: Error: Modifiers cannot be applied in edit mode
I saved the .blend file right before the error and opened this file in GUI. Then, I ran
bpy.ops.object.modifier_apply(modifier='Subsurf', apply_as='DATA')
that used to cause errors in the command pane, and weirdly it ran without errors!