Check is material is being used by objects

Tēnā koutou,

I’ve looked around but couldn’t find any relating to this. Is it possible to check what objects a material might be assigned to?

My use case is that when I modify a material, I have found that sometimes the material is shared by multiple objects and I inadvertently modify the material of another object. My solution is to use material.clone() and re-assign it to the current object. I thought it might be good practice to add a check if the material is being used elsewhere, before cloning it, to avoid unnecessary cloning?

You can use

Let materials = []
scene.traverse(c=>{
 if(c.isMesh){
  console.log(c)
  console.log(c.material)
  if( ! materials.includes(c.material){
   materials.push(c.material)
 } else{
  console.log('material is in use') 
  c.material = c.material.clone()
 } 
}
1 Like