Correctly put the entity in the node

I understand correctly, in order to insert an object into the hand and make it move along with the animation, I have to find a node and addChild an entity to it, like this:

var FindNode = pc.createScript('findNode');
FindNode.attributes.add('prize', {type:'entity'})
// initialize code called once per entity
FindNode.prototype.initialize = function() {

    const arm = this.entity.findByPath('RootNode/RigRArm1Gizmo')
    const root = this.app.root.findByName('Root')
    root.removeChild(this.prize)
   
    arm.addChild(this.prize)
     
};

It doesn’t work, error :GraphNode is already parented

Hello @sergey_ch! Try to use reparent.

this.prize.reparent(arm);
2 Likes

The reason it does not work in your code is because this.prize is not a child of root. So, when you call root.removeChild(this.prize), nothing happens and when you later try to add this.prize to arm, it gives an error, because it still has a parent.

A preferred option is to follow @Albertos advice.

Another option is to remove it from the parent first, then add to the arm:

this.prize.parent.removeChild(this.prize);
arm.addChild(this.prize);
3 Likes

Indeed, it partially worked. This. prizes disappeared from the stage, but did not appear on the model. Maybe there are some other nuances?

It should appear at the new location keeping the current local position it had when it was under the previous parent. If it had a local position far away from parent’s center, then it will also be far away from the new parent after you reparent it. You can read the .getLocalPosition() to check if it is correct or reset it to zero. Also make sure the entity is actually enabled after you reparent it.

1 Like

Yes, it really is. You still need to scale up a lot. And is it possible to translate the scale that the previous parent had to the corresponding scale of the current one?

You can read the scale of the old parent and apply on the new one:

// read scale before reparent
var scale = this.prize.parent.getLocalScale();

// then apply to new parent
arm.setLocalScale(scale);
1 Like