Rigidbody.objects_add.poll() failed! After the script that was working as mentioned in my earlier post. I am trying to create a blend file that has the scene setup and a script that has the locations of the objects available in the blend scene. I am able to see the new positions created by the script to the objects in the blend scene. But I am not able to bake them all. I get the below error.

RuntimeError: Operator bpy.ops.rigidbody.objects_add.poll() failed, context is incorrect


This is relevant to the lines

bpy.ops.rigidbody.objects_add(type='ACTIVE')
bpy.ops.ptcache.bake_all(bake=True)


Let me know if you have come across this kind of error. How do I make the context correct. When I try to the print the objects that are in the scene, I am able to see the objects in the blend file. What does the context here mean?

Thanks in advance!
You should deselect non mesh objects before adding rigid body objects

 bpy.ops.object.select_all(action='DESELECT') #clear selection
 candidate_list = [item.name for item in bpy.data.objects if item.type == "MESH"]
for object_name in candidate_list:
    bpy.data.objects[object_name].select = True


this also should be done when clearing the scene if you want to keep the camera and light sources. and regarding the context see this answer poll() failed, context incorrect?