0
$\begingroup$

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
bpy.ops.rigidbody.world_add()

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.

$\endgroup$

1 Answer 1

1
$\begingroup$

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)

Wrapping up - you have two options.

  1. remove the rigidbody world using bpy.ops.rigidbody.world_remove() before rerunning your script
  2. Check if there is already a rigidbody world added before adding one and skipping if there is.
$\endgroup$
2
  • $\begingroup$ Thanks, I just added an if for bpy.context.scene.rigidbody_world == None so it doesn't add world if it exists already. I still don't really understand why adding the world when it already exist would give a context error but at least in the future I will be thinking about stuff like this. $\endgroup$
    – BkSlayer
    Nov 2, 2020 at 2:50
  • $\begingroup$ @BkSlayer Yeah context management in Blender is a bit of a pain, largely because the "context is incorrect" error messages usually don't give you enough information and if its a C operator like this one tracking down where the operator is defined and what it is checking for is a pain. In this case its in developer.blender.org/diffusion/B/browse/master/source/blender/… and is the static bool ED_rigidbody_world_add_poll(bContext *C) method $\endgroup$ Nov 2, 2020 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .