Mesh parenting & cloning

Hi everyone,

I have a question w.r.t. mesh parenting in babylon.

I think it would be the best if I explain my question based on an example:

  • Assume I have a car consisting of many different objects as an array of wheels, an array of seats an lots of other stuff.
  • Now, I want to move the car. So, I parent the whole structure and move it with one line of code.
  • I also need to open the doors, which is not a problem as I can store the references to the sub objects and move them separately
  • However, I want to clone the car (as I do have multiple copies of it in the scene)
  • And again, I want to move submeshes (doors) of the copied car.

Of course, I could clone all of the children, rebuild the parenting structure, and remember the references of the cloned children.

However, I wonder if there is a more generic way for cloning a parented mesh-structure such that the children are retrievable? (maybe working with the predicate function of getChildren or similar)

Maybe someone had a similar dilemma, or how would you approach such a problem?

Thx in advance :wink:

you could clone with children then rely on getChildren with a predicate targetting the name/id of the meshes so that this could be factored in only one function on your to clone and retrieve the objects.

But would he using instancing then?

It might if the materials stay the same and for instance the animation do not involve skeletons with different anim groups playing at once :slight_smile:

In this case it would be more performant but does not change the discovery issue for the cloned submesh I guess

Hey all,

thank you very much for the tips.

  • (w.r.t. Instances: unfortunately I do change maetrials after cloning, but thx for the tip @Leon )
  • So I think getChildren + predicate would be the way to go. TY @sebavan .

I do have a couple of different objects “prototypes”, so if anyone comes up with an even more generic idea he/she is very welcome.

(My dilemma with assigning names or ids is twofolod: (1) For every “prototype” a clone function is required. It would analyse the cloned mesh and retrieve submeshes by getChildren(byName) or so. (2) if you have array-like structures as X wheels or similar, some workaround as name = name + index is required, which of course doesn’t look to good)

THX :wink: