Get mesh name on mesh click?

Hi!
I want to include mesh name in header.textContent at the click of that mesh.
onclick MESH {
header.textContent = myPickedMesh.name;
} else {
header.textContent = “No Picked Mesh”;
}
}
I tried Picking Collisions and Use Actions, both examples I wrote in the babylon playground on lines 70 and 81.
https://playground.babylonjs.com/#G2F8LN
I think after the export from blender objects are in pickable = false, so i tried to test it with true but without success …
One solution, the simplest … ???

Works like this:

myMeshesOne[0].isPickable = true;
myMeshesTwo[0].isPickable = true;
myMeshesThree[0].isPickable = true;

 scene.onPointerDown = function (evt, pickResult) {
        // We try to pick an object
        if (pickResult.hit) {
            header.textContent = pickResult.pickedMesh.name;
        }
    };

https://playground.babylonjs.com/#G2F8LN#2

8 Likes

Thx D!

Also, you can go with “pure actionManagers” and no DOM events.

https://playground.babylonjs.com/#G2F8LN#5

I added a scene.onDispose, too… keeps older HTML from jumping across playground saves. :slight_smile:

Aldin… interpolateValue action is for changing a value “gradually” (slowly animated)… almost always a number.

ExecuteCode action is better for you. This action runs a function, with a ‘bjsevt’ arriving in arg0. The bjsevt.source is the clicked object. Party on.

1 Like

Here’s another… https://playground.babylonjs.com/#G2F8LN#6

I turned ON the DOM eventListener again… just to set “No Picked Mesh” if the pick misses all mesh.

1 Like

Thank you for your answers
I like Action because I want to add visibility later, and this is described in Actions.
Since I’m still learning, I’d like to clarify a little.
If I add;
myMeshesOne [0] .isPickable = true;
I have to add
myMeshesOne [1] .isPickable = true;
for another model … if I have a lot of models it’s too [0,1 … a lot].
Wingnut has solved this with:
scene.onReadyObservable.add (function () {
for (var i = 0; i <scene.meshes.length; i ++) {
var mesh = scene.meshes [i];
mesh.isPickable = true;
mesh.actionManager = new BABYLON.ActionManager (scene);

scenes.onReadyObservable as I understand it serves to trigger an event at a particular moment. (An event triggered when the scene is ready)
Since I’m not the best in javascript, can i just explain it a bit:
for (var i = 0; i <scene.meshes.length; i ++) {
var mesh = scene.meshes [i];
mesh.isPickable = true;
mesh.actionManager = new

Is there a simpler way to get all Meshs in the scene isPickable = true? (of course if they are by defult false)

BABYLON.ExecuteCodeAction (trigger, func, condition): Executes code …, a few more explanations?
In actions bjsevt.source = mesh name?

Hi,

I am trying to get the pickResult.pickedMesh.parent but it is returned as an object.

How do I echo out this please.

pickResult.pickedMesh[‘parent’] give [object Object]

pickResult.pickedMesh.parent[0]) gives undefined

Cheers

Steve Warby

pickResult.pickedMesh.parent.name ?

Thats the one thanks.

I did find it … by accident…

Cheers

Steve Warby

1 Like

Hi,
Is it possible to show the mesh name , just near to the mesh. (i.e like tool-tip) instead of name in top right corner box?

Else on click of button, all the mesh name to show on top of the mesh. on second click of the button to switch off the mesh name.

Thanks

You could rely on the babylon gui system for this and attach the label to the mesh.

First thx D’s answer, but I got an unexpected situation: when I drag the scen, onPointerDown event staill trigger, I want to implement a pick event. So, here is my codes:

const nodePickInfo = {}
scene.onPointerDown = (_evt, pickResult) => {
	const target = pickResult?.pickedMesh
	if (!target) return
	this._nodePickInfo = {
		nodeId: target.id,
		downAt: Date.now()
	}
}
scene.onPointerUp = (_evt, pickResult) => {
	const target = pickResult?.pickedMesh
	if (!target) return
	const { nodeId = '', downAt } = this._nodePickInfo
	if (nodeId === target.id && downAt && Date.now() - downAt < 300) {
		// do somethings with pick node: current target object
	}
	this._nodePickInfo = {}
}

Also, I had try event onPointerPick, but it was not work as I thought…

Welcome to the forums! Can you create a playground with your scenario? It will be much easier to help.

As said, provide a PG. As for this part…

That feels quite logical to me as the event is handled at scene level. I suppose I would work it from the mesh :thinking:… but without a PG and an insight on the entire logic it will be hard to give proper feedback/advise.