Fix (unreported) Broken rigidbody requirements when adding objects to a RB collection.
authorBastien Montagne <montagne29@wanadoo.fr>
Wed, 12 Dec 2018 16:21:37 +0000 (17:21 +0100)
committerBastien Montagne <montagne29@wanadoo.fr>
Wed, 12 Dec 2018 16:21:37 +0000 (17:21 +0100)
We have to ensure objects get expected RB data, when they are added to a
RB collection...

source/blender/blenkernel/BKE_rigidbody.h
source/blender/blenkernel/intern/collection.c
source/blender/blenkernel/intern/rigidbody.c
source/blender/blenloader/intern/versioning_280.c

index 4d652326a14c4e2623e28dd82442131df61476e7..9a5f3c312641c29a1ee9699fb196fa304f2154d7 100644 (file)
 struct RigidBodyWorld;
 struct RigidBodyOb;
 
+struct Collection;
 struct Depsgraph;
-struct Scene;
+struct Main;
 struct Object;
+struct Scene;
 
 /* -------------- */
 /* Memory Management */
@@ -70,6 +72,9 @@ struct RigidBodyCon *BKE_rigidbody_create_constraint(struct Scene *scene, struct
 void BKE_rigidbody_objects_collection_validate(struct Scene *scene, struct RigidBodyWorld *rbw);
 void BKE_rigidbody_constraints_collection_validate(struct Scene *scene, struct RigidBodyWorld *rbw);
 
+/* Ensure object added to collection gets RB data if that collection is a RB one. */
+void BKE_rigidbody_main_collection_object_add(struct Main *bmain, struct Collection *collection, struct Object *object);
+
 /* copy */
 struct RigidBodyWorld *BKE_rigidbody_world_copy(struct RigidBodyWorld *rbw, const int flag);
 void BKE_rigidbody_world_groups_relink(struct RigidBodyWorld *rbw);
index 9b68c61e016507f55ca53b5f3c34b7538d4b2dee..33287d5a2f8a0765617b0fb2aa506dc45cfa7987 100644 (file)
@@ -42,6 +42,7 @@
 #include "BKE_library.h"
 #include "BKE_main.h"
 #include "BKE_object.h"
+#include "BKE_rigidbody.h"
 #include "BKE_scene.h"
 
 #include "DNA_ID.h"
@@ -515,6 +516,10 @@ static bool collection_object_add(Main *bmain, Collection *collection, Object *o
                DEG_id_tag_update_ex(bmain, &collection->id, ID_RECALC_COPY_ON_WRITE);
        }
 
+       if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
+               BKE_rigidbody_main_collection_object_add(bmain, collection, ob);
+       }
+
        return true;
 }
 
index aa5051e25cd09a1eb202cda4d7147ea18e15504b..7d4bd74389e335092febbab6c6b4e417c9d95d74 100644 (file)
@@ -61,6 +61,7 @@
 #include "BKE_layer.h"
 #include "BKE_library.h"
 #include "BKE_library_query.h"
+#include "BKE_main.h"
 #include "BKE_mesh.h"
 #include "BKE_mesh_runtime.h"
 #include "BKE_object.h"
@@ -1173,6 +1174,23 @@ void BKE_rigidbody_constraints_collection_validate(Scene *scene, RigidBodyWorld
        }
 }
 
+void BKE_rigidbody_main_collection_object_add(Main *bmain, Collection *collection, Object *object)
+{
+       for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
+               RigidBodyWorld *rbw = scene->rigidbody_world;
+
+               if (rbw == NULL) {
+                       continue;
+               }
+
+               if (rbw->group == collection && object->type == OB_MESH && object->rigidbody_object == NULL) {
+                       object->rigidbody_object = BKE_rigidbody_create_object(scene, object, RBO_TYPE_ACTIVE);
+               }
+               if (rbw->constraints == collection && object->rigidbody_constraint == NULL) {
+                       object->rigidbody_constraint = BKE_rigidbody_create_constraint(scene, object, RBC_TYPE_FIXED);
+               }
+       }
+}
 
 /* ************************************** */
 /* Utilities API */
index 237e1387a4ee9a9c68f6b932c69a979b0efd60b4..e2ac0f16e34f56b973f3f85f3b4f5acf65ecc26e 100644 (file)
@@ -918,7 +918,7 @@ void do_versions_after_linking_280(Main *bmain)
                BKE_paint_toolslots_init_from_main(bmain);
        }
 
-       if (!MAIN_VERSION_ATLEAST(bmain, 280, 36)) {
+       if (!MAIN_VERSION_ATLEAST(bmain, 280, 38)) {
                /* Ensure we get valid rigidbody object/constraint data in relevant collections' objects. */
                for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
                        RigidBodyWorld *rbw = scene->rigidbody_world;