Unlinking RigidBodyWorld Collection Objects via Script #110906

Closed
opened 2023-08-07 22:34:32 +02:00 by Kevin C. Burke · 11 comments
Contributor

System Information
Operating system: Windows-10-10.0.22621-SP0 64 Bits
Graphics card: NVIDIA GeForce RTX 3090/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 536.67

Blender Version
Broken: version: 3.6.1, branch: blender-v3.6-release, commit date: 2023-07-17 12:50, hash: 8bda729ef4dc

Short description of error
When unlinked from RigidBodyWorld before removing the object rigid body settings, the objects are linked to the Main Scene Collection.

This was detected when trying this code:

import bpy

scene = bpy.context.scene
enable = True # set to True to link to Rigid Body collection, set False to unlink

if not scene.rigidbody_world: # a RigidBodyWorld collection doesn't exist
    bpy.ops.rigidbody.world_add() # run Add Rigid Body World operator
    scene.rigidbody_world.collection = bpy.context.active_object.users_collection[0] # use the active object's collection for rigid bodies

for o in bpy.context.selected_objects: # get selected objects
    rb_coll = scene.rigidbody_world.collection.objects # get the rigid body world's objects
    if o.type == "MESH": # for mesh objects
        if enable and o not in list(rb_coll):
            rb_coll.link(o) # link object to rigid body world collection
        elif not enable and o in list(rb_coll): 
            rb_coll.unlink(o) # unlink object to rigid body world collection

Exact steps for others to reproduce the error

  • Open attached .blend file
  • Run script to unlink objects
  • For each Cube object, remove the rigid body settings

Objects are linked to the Main Collection

**System Information** Operating system: Windows-10-10.0.22621-SP0 64 Bits Graphics card: NVIDIA GeForce RTX 3090/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 536.67 **Blender Version** Broken: version: 3.6.1, branch: blender-v3.6-release, commit date: 2023-07-17 12:50, hash: `8bda729ef4dc` **Short description of error** When unlinked from `RigidBodyWorld` before removing the object rigid body settings, the objects are linked to the Main Scene Collection. <video src="/attachments/ce28e0d4-e1c8-4bef-a423-99f5b7b9e225" title="Unlinking Rigid Body objects.mp4" controls></video> This was detected when trying this code: ```python import bpy scene = bpy.context.scene enable = True # set to True to link to Rigid Body collection, set False to unlink if not scene.rigidbody_world: # a RigidBodyWorld collection doesn't exist bpy.ops.rigidbody.world_add() # run Add Rigid Body World operator scene.rigidbody_world.collection = bpy.context.active_object.users_collection[0] # use the active object's collection for rigid bodies for o in bpy.context.selected_objects: # get selected objects rb_coll = scene.rigidbody_world.collection.objects # get the rigid body world's objects if o.type == "MESH": # for mesh objects if enable and o not in list(rb_coll): rb_coll.link(o) # link object to rigid body world collection elif not enable and o in list(rb_coll): rb_coll.unlink(o) # unlink object to rigid body world collection ``` **Exact steps for others to reproduce the error** - Open attached .blend file - Run script to unlink objects - For each Cube object, remove the rigid body settings Objects are linked to the Main Collection
Kevin C. Burke added the
Type
Report
Priority
Normal
Status
Needs Triage
labels 2023-08-07 22:34:33 +02:00

From what I saw in the video, the objects are not duplicated.
The same object can be in more than one collection.
This is normal.

But it's strange that the object remains in the Rigid Body collection even after the Rigid Body settings are removed.

However I tried to replicate the problem using this .blend file and was unsuccessful. There must be something wrong with the file shown.

From what I saw in the video, the objects are not duplicated. The same object can be in more than one collection. This is normal. But it's strange that the object remains in the Rigid Body collection even after the Rigid Body settings are removed. However I tried to replicate the problem using this .blend file and was unsuccessful. There must be something wrong with the file shown.
Germano Cavalcante added
Status
Needs Information from User
and removed
Status
Needs Triage
labels 2023-08-08 00:01:37 +02:00
Author
Contributor

Hi @mano-wii , thank you for the reply.

In the attached screen capture, I am using your file and adding 'Rigid Body' to the Active Object. This creates the collection 'RigidBodyWorld'. I then run the script to add the other selected objects to the 'RigidBodyWorld' collection. When I then use the script to unlink the objects, it's just as you said: the objects aren't removed from the Rigid Body collection.

Hi @mano-wii , thank you for the reply. In the attached screen capture, I am using your file and adding 'Rigid Body' to the Active Object. This creates the collection 'RigidBodyWorld'. I then run the script to add the other selected objects to the 'RigidBodyWorld' collection. When I then use the script to unlink the objects, it's just as you said: the objects aren't removed from the Rigid Body collection.
Member

Hi @KevinCBurke Apparently it should be elif (not enable) and (o in list(rb_coll)):, otherwise the logic won't pass. And if changed to this, here it unlinks successfully.

Hi @KevinCBurke Apparently it should be `elif (not enable) and (o in list(rb_coll)):`, otherwise the logic won't pass. And if changed to this, here it unlinks successfully.
YimingWu added the
Interest
Python API
label 2023-08-08 05:01:22 +02:00
Author
Contributor

@ChengduLittleA Thank you for testing. It don't think that it's because of the logic. The issue for me is when the Scene Property's Rigid Body Collection is using RigidBodyWorld (which is what is created when the user adds Rigid Body to a mesh object prior running the bpy.ops.rigidbody.world_add() operator), it doesn't remove the Rigid Body, even with elif (not enable) and (o in list(rb_coll)):

Check out this demo video (Unlinking 3.mp4, attached), using elif (not enable) and (o in list(rb_coll)):. In the beginning of the video, if the Rigid Body collection is RigidBodyWorld, the unlink in the script doesn't work. When I change the Rigid Body Collection to another Collection that is exposed in the Scene Outliner, it does. RigidBodyWorld doesn't seem to work with the Python API.

@ChengduLittleA Thank you for testing. It don't think that it's because of the logic. The issue for me is when the Scene Property's Rigid Body Collection is using `RigidBodyWorld` (which is what is created when the user adds Rigid Body to a mesh object prior running the `bpy.ops.rigidbody.world_add()` operator), it doesn't remove the Rigid Body, even with `elif (not enable) and (o in list(rb_coll)):` Check out this demo video (Unlinking 3.mp4, attached), using `elif (not enable) and (o in list(rb_coll)):`. In the beginning of the video, if the Rigid Body collection is `RigidBodyWorld`, the unlink in the script doesn't work. When I change the Rigid Body Collection to another Collection that is exposed in the Scene Outliner, it does. `RigidBodyWorld` doesn't seem to work with the Python API.
Kevin C. Burke changed title from Unlinking Rigid Body Collection Objects via Script Duplicates Objects to Unlinking RigidBodyWorld Collection Objects via Script 2023-08-08 06:36:25 +02:00
Member

@KevinCBurke It will of course not "remove a rigid body configuration" of that object. It's just removing the link from that specific collection (in this case RigidBodyWorld). Your code works no matter which collection is selected in the rigid body world collection, you can verify in ourliner > Blender File view: After executing the script, those object will get unlinked from RigidBodyWorld. It's just a special collection that's not visible directly under scene.

图片

@KevinCBurke It will of course not "remove a rigid body configuration" of that object. It's just removing the link from that specific collection (in this case `RigidBodyWorld`). Your code works no matter which collection is selected in the rigid body world collection, you can verify in ourliner > Blender File view: After executing the script, those object will get unlinked from `RigidBodyWorld`. It's just a special collection that's not visible directly under scene. ![图片](/attachments/9bb2d3de-ae28-43de-a823-ab1e362ffe91)

The provided Python code is subject to errors in both the Rigid Bodies collection choice and the enable logic.
Also, there is a misunderstanding in the description when it says that the objects are duplicates.


What happens is that the code strips objects from the RigidBodyWorld collection before the configuration is removed.

And when removing the Rigid Body setting, Blender sees that the object has only 1 user (assuming it is the RigidBodyWorld collection) so, to remove it from the collection without losing the object, the object is linked to the main collection in the scene.

It's strange that a Rigid Body object is not in the Rigid Bodies collection but in fact it is possible for this to happen as we can see in this report.

So there really is a kind of bug here.

The provided Python code is subject to errors in both the Rigid Bodies collection choice and the `enable` logic. Also, there is a misunderstanding in the description when it says that the objects are duplicates. --- What happens is that the code strips objects from the `RigidBodyWorld` collection before the configuration is removed. And when removing the Rigid Body setting, Blender sees that the object has only 1 user (assuming it is the `RigidBodyWorld` collection) so, to remove it from the collection without losing the object, the object is linked to the main collection in the scene. It's strange that a Rigid Body object is not in the Rigid Bodies collection but in fact it is possible for this to happen as we can see in this report. So there really is a kind of bug here.

@mont29, I suspect this has something to do with your commit 30116a5274.

It might be a good idea to check if the object is in the Rigid Bodies collection first (with BKE_collection_has_object).

@mont29, I suspect this has something to do with your commit 30116a5274. It might be a good idea to check if the object is in the Rigid Bodies collection first (with `BKE_collection_has_object`).

This has nothing to do directly with a four years old commit (30116a5274), and is the expected behavior.

Local objects should always be linked in at least one collection (i.e. be present in at least one scene). So when trying to remove (unlink) them from the only collection they exist in, nothing happen.

This has nothing to do directly with a four years old commit (30116a5274), and is the expected behavior. Local objects should always be linked in at least one collection (i.e. be present in at least one scene). So when trying to remove (unlink) them from the only collection they exist in, nothing happen.
Blender Bot added
Status
Archived
and removed
Status
Confirmed
labels 2023-08-08 16:57:33 +02:00

@mont29, in this case, the object is in fact only in one collection, but NOT in the collection that will be removed.

Checking the collection would solve the problem.

diff --git a/source/blender/blenkernel/intern/rigidbody.cc b/source/blender/blenkernel/intern/rigidbody.cc
index f855c37f25a..7f357ec5f00 100644
--- a/source/blender/blenkernel/intern/rigidbody.cc
+++ b/source/blender/blenkernel/intern/rigidbody.cc
@@ -1632,15 +1632,17 @@ void BKE_rigidbody_remove_object(Main *bmain, Scene *scene, Object *ob, const bo
       FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
     }
 
-    /* Relying on user-count 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, scene->master_collection, ob);
-    }
-    BKE_collection_object_remove(bmain, rbw->group, ob, free_us);
+    if (BKE_collection_has_object(rbw->group, ob)) {
+      /* Relying on user-count 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, scene->master_collection, ob);
+      }
+      BKE_collection_object_remove(bmain, rbw->group, ob, free_us);
+    }
 
     /* flag cache as outdated */
     BKE_rigidbody_cache_reset(rbw);

@mont29, in this case, the object is in fact only in one collection, but **NOT** in the collection that will be removed. Checking the collection would solve the problem. ```diff diff --git a/source/blender/blenkernel/intern/rigidbody.cc b/source/blender/blenkernel/intern/rigidbody.cc index f855c37f25a..7f357ec5f00 100644 --- a/source/blender/blenkernel/intern/rigidbody.cc +++ b/source/blender/blenkernel/intern/rigidbody.cc @@ -1632,15 +1632,17 @@ void BKE_rigidbody_remove_object(Main *bmain, Scene *scene, Object *ob, const bo FOREACH_COLLECTION_OBJECT_RECURSIVE_END; } - /* Relying on user-count 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, scene->master_collection, ob); - } - BKE_collection_object_remove(bmain, rbw->group, ob, free_us); + if (BKE_collection_has_object(rbw->group, ob)) { + /* Relying on user-count 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, scene->master_collection, ob); + } + BKE_collection_object_remove(bmain, rbw->group, ob, free_us); + } /* flag cache as outdated */ BKE_rigidbody_cache_reset(rbw); ```

@mano-wii but NOT in the collection that will be removed. the objects are removed from the collection? Where is it about removing the collection itself?

@mano-wii `but NOT in the collection that will be removed.` the objects are removed from the collection? Where is it about removing the collection itself?

Unless what you are talking about is not what the report is about...

Unless what you are talking about is not what the report is about...
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#110906
No description provided.