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.
fn layerByMaterial = (
for obj in geometry where obj.material != undefined do (
layerName = "Material_" + obj.material.name as String
layer = LayerManager.getLayerFromName layerName
if (layer == undefined) then (
layer = layermanager.newLayerFromName layerName