How to get object data on selecting object in viewer API 2.0?

I’m trying to get all object data when clicking on an object in the stream. I’m using the selection extension in API 2.0, but nothing is logged when I click on an object.

/** Add the selection extension for extra interactivity */
const selector = viewer.createExtension(SelectionExtension);
selector.on(ViewerEvent.ObjectClicked, (selectionInfo: SelectionEvent) => {
	console.log(selectionInfo);
});

Hi @imanwarsame

Listening for interactions with the scene contents is still being done on the viewer instance. So you need to

viewer.on(ViewerEvent.ObjectClicked, (e: SelectionEvent) => {
    /** The selection event contains the entire hit chain. First hit
        is the closest one and generally the one you need. Hits contain
        the node and hit point
    */
    console.log(e.hits[0].node.model.raw);
});

Here’s the current list of viewer events and here is information about nodes

Additionally you can also listen for pure input events by using this (if you ever have a need for it)

The selection extension listens to the same viewer event and just visually selects objects, zooms in on them, hover’s visually (if specified in the options) etc.

We are planning however to improve the way these scene interactions happen, as well as their part in the API in a future iteration.

Cheers

2 Likes

Great, thanks @alex! :smile:

1 Like