2
$\begingroup$

I'd like to enable the rigid body settings for a set of objects, and I don't want to use bpy.ops.rigid_body.object_add(). I see that objects have the property object.rigid_body, which is read-only. So how do I create the rigid body settings, and assign them to object.rigid_body?

$\endgroup$

2 Answers 2

6
$\begingroup$

This question is related to How do I add a rigidbody to an object with python?.

In 2.8 you need to use collections, so if you want to add rigid body physics to object my_object for scene 'Scene':

bpy.data.scenes['Scene'].rigidbody_world.collection.objects.link(my_object)

and verify outcome with

my_object.rigid_body is not None
$\endgroup$
2
  • $\begingroup$ @batFINGER I updated the variable name to my_object. Thanks. $\endgroup$
    – Dazotaro
    Apr 10, 2019 at 21:33
  • 1
    $\begingroup$ Finally, the right answer! The accepted answer to the linked question is outdated. $\endgroup$ Sep 2, 2022 at 6:35
1
$\begingroup$

This seems to have changed recently in 2.90, and I could only find a workaround by talking to folks in discord. Here's a complete snippet for code that works to create rigid body objects in Blender:

import bpy

bpy.ops.mesh.primitive_monkey_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 5), scale=(1, 1, 1))
suz = bpy.context.object

bpy.ops.mesh.primitive_plane_add(size=10, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
plane = bpy.context.object

bpy.ops.rigidbody.world_add()
sc = bpy.context.scene
sc.rigidbody_world.enabled = True

collection = bpy.data.collections.new("MyTestCollection")

sc.rigidbody_world.collection = collection
sc.rigidbody_world.collection.objects.link(suz)
sc.rigidbody_world.collection.objects.link(plane)

plane.rigid_body.type = 'PASSIVE'

$\endgroup$
0

You must log in to answer this question.

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