DataSource reference from picked/DynamicObject

Hello,

is there any elegant and fast way to find out which particular DataSource owns an object picked with the scene.pick function?

Thanks,
Alessio

It’s not a built-in property, because in the general case, DynamicObjects can be in multiple collections, and collections aren’t necessarily associated with a DataSource.

You can try to locate the dynamic object in the data source’s collection by id and see if it’s the same object. This is fast because objects are hashed by id. If you have multiple data sources, you can loop through them and check each one.

var dynamicObjectCollection = dataSource.getDynamicObjectCollection();

if (dynamicObjectCollection.getById(dynamicObject.id) === dynamicObject) {

// dataSource “owns” dynamicObject

}

We don’t have anything built in, but here’s some code that should do it. (I didn’t actually test this, so you might need to tweak it some)

function getAssociatedDataSource(dynamicObject) {

for(var i = 0; i < dataSources.length; i++){

var dataSource = dataSources.get(i);

var collection = dataSource.getDynamicObjectCollection();

// DynamicObject ids are not universal, so if an object with that

// id exists in another data source, we need to verify it is the instance we want.

if (collection.getById(dynamicObject.id) === dynamicObject) {

return dataSource;

}

}

}

Thank you very much! Also thanks Matthew Amato :slight_smile: