Changing a material's name with Python
Each material has a unique name

Each ID object in blender has a unique name. Internally blender does this for us if needed. Eg duplicate the cube. This is often referred to as the "naming system".

For example, If another material already has the name you are trying to assign, to test bpy.materials.get("Glass") is not None then cannot name another material "Glass". Can try to, `anothermaterial.name = "Glass", but the naming system will rename it with a .nnn extension.

This explains the behaviour described in your question.


  I can rename them manually with no problems


Seriously doubt that claim. Contend that instead you are also unwittingly renaming the original. (or selecting it from material drop down)


Rename green material to "Material" just like blue

If you want to link to only one;  assign the material to the "Glass" original, eg using the tempholder name from script above

m.material = bpy.data.materials.get(tempholder)



Assigning each slot linked to  "Material.nnn" to "Material"

Prevent Blender to add numbers behind materials when importing a FBX

Disable Material Duplication