Set material name by Layer name

Hello everyone - I have been trying to make a python script that materials name are set by the name of their layers.

The same question was posted previously by me. I got two responses but it did not work. Here is the link to my previous post: Material name to match Layer name

I tried a few more times and still no result. The script runs perfectly but the materials name are not changing.

This whole idea came to me when I read the following post (here objects name are set by layer name and it works): Set object name by layer name with counter

Thanks!

there is no need for double posting, just reply under your initial thread