layerByMaterial

The following snippet creates a layer for each material which is assigned to a geometry object in the scene. The objects are added to the layers respectively. If the object doesn’t have a material it remains untouched.