Custom operator that applies transforms to all objects I need to create an operator to apply transforms to all visible object in the scene. This is how I tried to do it.

class ApplyAllOperator(bpy.types.Operator):
   bl_idname = "my.apply_all"
   bl_label = "Apply All Operator"

   def execute(self, context):
         for object in bpy.context.scene.objects:
             if object.is_visible(bpy.context.scene):
                object.select = True
                bpy.context.scene.objects.active = object
                bpy.ops.object.transform_apply(location = True, scale = True, rotation = True)


However I'm getting an error RuntimeError: Operator bpy.ops.object.transform_apply.poll() failed, context is incorrect.

How can I fix this?
Press A to select all and then &#9096; CtrlA->All Transforms - apply transforms already works on all selected objects.

To get your example working, the context calling the operator needs to be from the 3dviewport, so it will fail if you are running the script in the text editor. Don't call the operator from your script, register the class and then search for it in the 3d view.

If you still have an issue, you can override the context. You can find several questions here to explain ways to do that.