# "bpy.ops.rigidbody.world_add.poll() failed, context is incorrect" occurs when python script is ran more than once

I am seeing a context incorrect error if I run this script more than once in the same .blend file. It occurs in Blender 2.82 and 2.90. My understanding of how contexts work is only mediocre but I cannot think of a reason why/how the context would change after running the script multiple times. I also do not know of any context requirements for trying to add a rigid body world. My end goal is to enable rigid body physics for specific objects and I'm pretty sure enabling rigid world is the first step. I've reduced the code to the following:

import bpy


Reproduction steps:

1. Open a new general blend file.
2. Insert code from above, run once. There will be no error and the rigid body world will be enabled if you look in the "scene tab"
3. Run the script again, error will say context is incorrect.

Thanks.

When you first start Blender there is no rigid body world added to the context. You can check this by running print(C.scene.rigidbody_world) in the console. This should return None
When you run bpy.ops.rigidbody.world_add() you are adding a new rigid body world to bpy.context.scene If you now enter print(C.scene.rigidbody_world) you should get something like <bpy_struct, RigidBodyWorld at 0x00000225720128B8>
If you immediately run bpy.ops.rigidbody.world_add() again the error you get should be something like RuntimeError: Operator bpy.ops.rigidbody.world_add.poll() failed, context is incorrect This tells us that the operator's poll() method is checking "something" in the context and since we know that what has changed is that we now have a rigidbody_world in our context, we can assume that this is what the poll function is checking for. (actually checking this is a bit of a pain as it means tracking down the operator)
1. remove the rigidbody world using bpy.ops.rigidbody.world_remove() before rerunning your script