# detachFromBone breaks parent-child relationships

**URL:** <https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728>\
**Category:** Questions\
**Created:** [June 16, 2021, 1:05pm UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728 "2021-06-16T13:05:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [June 16, 2021, 1:05pm UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728/1 "2021-06-16T13:05:30Z")

</div>

repro: [https://playground.babylonjs.com/#11BH6Z#671](https://playground.babylonjs.com/#11BH6Z#671)

I’m surprised at not seeing any report on this. Not really sure if its wai or my understanding is borked. The typical use case is for equipping a character with multiple meshes (weapons, items etc) and allow the character to switch between different objects in hand on the fly. sphere is attached to a particular bone, animation runs, sphere is then detached from bone but should still remain parented to dude. For item switch, its a sequence of multiple attaches/detaches.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [June 16, 2021, 3:11pm UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728/2 "2021-06-16T15:11:53Z")

</div>

Adding @Evgeni_Popov

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 16, 2021, 3:32pm UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728/3 "2021-06-16T15:32:26Z")

</div>

Dude has no child named “sphere1”, so `let sphere2 = dude.getChildMeshes(true).find(child => child.name === "sphere1")` returns `undefined` and `sphere2.attachToBone(skeleton.bones[34], dude)` fails.

If you do `sphere.attachToBone(skeleton.bones[34], dude)` instead if works.

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [June 17, 2021, 1:14am UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728/4 "2021-06-17T01:14:38Z")

</div>

This is entirely my fault, it was late and I didn’t sanitise/explain the PG properly.  
Repro again: [https://playground.babylonjs.com/#11BH6Z#673](https://playground.babylonjs.com/#11BH6Z#673)

The problem here is that detachFromBone should not cause my sphere to lose its parent, ie, dude. console will throw undefined for the 2nd call. wai ?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 17, 2021, 7:43am UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728/5 "2021-06-17T07:43:26Z")

</div>

When you attach a mesh to a bone, the parent of this mesh is updated so that it is now the bone you attach it to. When you detach the mesh from the bone, its parent is reset to null. We could probably save the current parent when you call `attachToBone` and set it back when you call `detachFromBone` (with a flag to stay backward compatible) but I’m not sure it’s a general enough use case and you can simply set the parent to the node you want after you have called `detachFromBone`. What do you think @Deltakosh?

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [June 17, 2021, 9:10am UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728/6 "2021-06-17T09:10:20Z")

</div>

> [@Evgeni\_Popov](#):
>
> I’m not sure it’s a general enough use case and you can simply set the parent to the node you want after you have called `detachFromBone`

Yes, I can do that. But I think this is worthy of discussion as I have no idea what the general use case is as it is. If parenting has no impact on `attachToBone`, I’d expect it not to have an impact either when `detachFromBone` runs. User multi triggering an animation (eg:item switch) means additional calls to finding and reparenting meshes, a minor hassle for a single mesh. But a nightmare when multiple agents are animating. Adding an option to `detachFromBone` to prevent parenting resets is another alternative.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [June 18, 2021, 10:46pm UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728/7 "2021-06-18T22:46:39Z")

</div>

I love the alternative option 🙂

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 19, 2021, 10:16am UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728/8 "2021-06-19T10:16:44Z")

</div>

Here’s the PR:

> <https://github.com/BabylonJS/Babylon.js/pull/10529>
>
> See https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationshi…ps/21728/6

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [June 20, 2021, 1:36am UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728/9 "2021-06-20T01:36:39Z")

</div>

I found this but I guess I was late ☹ Repro: [https://playground.babylonjs.com/#NRNBMM#169](https://playground.babylonjs.com/#NRNBMM#169)  
`detachFromBone` used to null a parent in a way outside of its intended func.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 20, 2021, 9:48am UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728/10 "2021-06-20T09:48:15Z")

</div>

That won’t change with the PR, we don’t check you did call `attachToBone` before calling `detachFromBone`, you are responsible for the proper order of operations (it’s a bug on the user side to call `detachFromBone` without having called `attachToBone` first).

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [June 25, 2021, 6:33am UTC](https://forum.babylonjs.com/t/detachfrombone-breaks-parent-child-relationships/21728/11 "2021-06-25T06:33:04Z")

</div>

Tested, fix works and marking as solved. Thanks ! 🙂
