Fix T65109: Object deleted when removed from the RigidBodyWorld collection.

While user should never do that, it appears many end up using a 'view
layer' instancing collection as RBW collection, and even worse, have
objects in that unique collection.

Therefore, when removing RB simulation from an object, which among other
things has to remove it from the RBW collection, it would fully delete
the object from the blend file.

This fix merely checks the usercount of RB-removed object, and if it is
at 1 (which means object was in a single collection), it adds it to the
scene's master collection first.
This commit is contained in:
Bastien Montagne 2019-06-13 17:57:55 +02:00
parent 245129e8e2
commit 30116a5274
Notes: blender-bot 2023-08-08 16:44:00 +02:00
Referenced by issue #65109, 2.8 Rigid body objects disappear from the scene with Rigid Body > Bake to keyframes command
Referenced by issue #110906, Unlinking RigidBodyWorld Collection Objects via Script
1 changed files with 9 additions and 0 deletions

View File

@ -1326,6 +1326,15 @@ void BKE_rigidbody_remove_object(struct Main *bmain, Scene *scene, Object *ob)
}
FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
}
/* Relying on usercount of the object should be OK, and it is much cheaper than looping in all
* collections to check whether the object is already in another one... */
if (ID_REAL_USERS(&ob->id) == 1) {
/* Some users seems to find it funny to use a view-layer instancing collection
* as RBW collection... Despite this being a bad (ab)use of the system, avoid losing objects
* when we remove them from RB simulation. */
BKE_collection_object_add(bmain, BKE_collection_master(scene), ob);
}
BKE_collection_object_remove(bmain, rbw->group, ob, false);
}