Getting the local position of a mesh node?

I have a model made up of many children, I’m trying to get the node position of one of the children like so:

Focus.prototype.initialize = function() {
    if (this.entity.model) {
        var meshInstances = this.entity.model.meshInstances;
        for (var i = 0; i < meshInstances.length; ++i) {
            var mi = meshInstances[i];
            
            var cur = mi.node;
            while(cur) {
                if(cur.parent === cur) {
                    break;
                }
                cur = cur.parent;
            }
            
          
            console.log(mi.node.name);
            
            if (mi.node.name == this.nodeName) {
               //   alert(mi.node.name);
                this.targetPosition = mi.node.getWorldTransform().getTranslation();
            }
        }
    }
    
};

Unfortunately this seems to be returning the position of the parent models world transform.

Is this at all possible to achieve in play canvas?

Hi @meds,

I think you can easily get the local position like this:

this.targetPosition = mi.node.getLocalPosition();

The pc.GraphNode class is the base class for the pc.Entity class so they share the same methods.

2 Likes

@Leonidas ah I must be doing something wrong, I tried the following:

if (this.entity.model) {
        var meshInstances = this.entity.model.meshInstances;
        for (var i = 0; i < meshInstances.length; ++i) {
            var mi = meshInstances[i];
            
            var cur = mi.node;
            while(cur) {
                if(cur.parent === cur) {
                    break;
                }
                cur = cur.parent;
            }
          
            console.log(mi.node.name + " " + JSON.stringify(mi.node.getLocalPosition()));
            
            if (mi.node.name == this.nodeName) {
                this.targetPosition = mi.node.getLocalPosition();
            }
        }
    }
    

Here: https://playcanvas.com/editor/scene/982564

Every print is coming back at (0,0,0) :frowning_face:

Hi @meds,

I’ve imported your model in Blender and I think the local position you are getting back is correct. All Blender nodes are positioned at 0,0,0 local space. Their vertices are displaced but the pivot point of each node is at 0,0,0.

You could change the pivot point for each node to be at the mass center to get it to be closer at where the corresponding vertices are.

2 Likes

Ah that’s pretty funny, thanks for the help!

1 Like