MEL Script
to Assign or Rename Mats and SGs based on the Geometry Name

This script assigns new materials (lamberts) to objects that have the default material in Maya (Lambert1 and InitialShadingGroup),
and name them according to the name of Geometry

In case they already have an assigned material, it will simply rename it

Simply select all the Objects you want and run the Script, it works perfectly in whole scenes

The code is full documented to see how each line works

/*                                      */
/*    MEL Script to Assign/Rename       */
/*  Materials & SG as Geometry Name     */
/*                                      */
/*  Born by Internet Code Pieces        */
/*  Rearranged by Nacho Sanchez         */
/*             */
/*                                      */
/* FEEL FREE to use, modify or whatever */
/*                                      */

//"Collect" in an array the selected objects (geometry)
//"list selected" = ls "list" -sl "selected"
string $objetos[] = `ls -sl`;
//initialization of variables, one for the material or another for the SG, these are used to create new materials and new names
string $nombreMAT, $nombreSG;

//Input loop for the array of selected objects
for($i=0; $i//I access the (all) nodes dependent on the object in question
string $listarRelativos[] = `listRelatives  $objetos[$i]`;
//and I make a filter (-type) to just stay with the "shadingEngine" node
string $myShadingEngine[] = `listConnections -type "shadingEngine" $listarRelativos`;
//And compare the name of the SG with text string "initialShadingGroup"  
//function strcmp ("string comparison")
int $compara = `strcmp $myShadingEngine[0] "initialShadingGroup"`;
          //If the SG is equal to initialShadingGroup then assign a new material with its corresponding name
          if ($compara == 0){ //strcmp devuelve un 0 si las cadenas son iguales

            //I create a new material (lambert), with the object name and adding the suffix _MAT
            $nombreMAT = `shadingNode -as lambert -n ($objetos[$i] + "_MAT")`;
            //I create a new ShadingGroup, with the name of the object and adding the suffix _SG
            $nombreSG = `sets -renderable true -noSurfaceShader true -empty -name ($objetos[$i] + "_SG")`;
            //I connect the new Material and ShadingGroup nodes
            connectAttr -f ($nombreMAT + ".outColor") ($nombreSG + ".surfaceShader");
            //I select the object (geometry) in question
            select $objetos[$i];
            //and connect the new ShadingGroup (which depends on the material, which we have previously connected)
            sets -e -forceElement $nombreSG;
            } //end of if
           // If the SG is different from initialShadingGroup means that already had a material assigned by us 
                       //(something other than lambert1 and initialShadingGroup), could be any type of material to which 
                       //we would have linked textures and others and in no case we are interested in breaking it,
                       //so simply we rename it
                     //Here I make another filter, to obtain, through the filter (flag) "-mat" only 
                               //the node of the material hanging from the ShadingGroup, Which we have 
                               //previously obtained at the beginning of the for loop
                                       string $myMaterial[] = ls("-mat", listConnections($myShadingEngine));
                                       //And rename, again adding the suffixes _SG and _MAT
                                       rename $myShadingEngine ($objetos[$i] + "_SG");
                                       rename $myMaterial ($objetos[$i] + "_MAT"); 
                                       } //end of else
} //end of for loop

In fact this code was born from my need to have well-named materials when I export a large scene to Sketchfab, (I know SketchFab has its own exporter but I prefer to manually upload my models), and if you do not have well-named materials it becomes a mess

A curious detail, if you export the scene in .OBJ SketchFab reads _SG node, but if you export it in .FBX it reads the _MAT node

When I start to do this code, I literally had no idea how to do it, I was reading forums and webs, picking up a little from here and there, researching and learning every possible MEL line

So my thanks to this SimpyMaya thread, which was the one that pointed me in the right direction:

Other great webs where to learn MEL (for those who want start in this world):