I can do a LOT of things with Python in Blender, but for some reason I'm struggling with changing the material name.

When I import an FBX Blender renames the material names if they are dupes. So I get a "Glass.001", "Glass.002" but I really need them to all just be "Glass". I can rename them manually with no problems, but my code to do so fails.

for obj in bpy.data.objects:
    for num, m in list(enumerate(obj.material_slots)): #rename materials with . numbers
        if m.material:
            myhold = m.material.name        
            if myhold.find(".") > -1:
                print ("Material is", m.material.name)
                tempholder = myhold[:myhold.find(".")]
                m.material.name = tempholder
                print ("Material is now", tempholder)
                print ("So should be", m.material.name)

which prints something like:

"Material is Glass.001" "Material is now Glass" "So should be Glass.001"

It must be something obvious but I don't get it. TIA for any help.


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)

enter image description here 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)

enter image description here Assigning each slot linked to "Material.nnn" to "Material"

Prevent Blender to add numbers behind materials when importing a FBX

Disable Material Duplication

  • $\begingroup$ Okay, I guess I'll have to hit it on the backside of what I'm doing (which is creating folders for those material names -- I can rename them. $\endgroup$ Mar 18 '19 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.