I have batch imported 300 obj files (a tile set) into blender. When I do this, the materials get cloned many times for each object.

For example, Object 1 might have "Rock", "White", "Red" as materials.

Object 2 would then be "Rock.001", "White.001", and "Red.001"

Object 3 has "Rock.002", "White.002", and so on.

So, to remedy this, I am trying to write a python script that will look at all materials on each object and check if it ends with a number, and if so, then strip off the "." and the numbers afterwards.

Ideally, this would get rid of all of the duplicate materials created in the import process.

This is my script. It seems to isolate the names correctly but for some reason doesn't actually change them.

import bpy

objects = bpy.data.objects

for obj in objects:
    for mat in obj.data.materials:
        char = mat.name.strip()[-1]
        if char.isdigit() == True:
            new_name = mat.name.split(".")[0]
            mat.name = new_name

Any help with this would be very much appreciated. Thanks a whole lot!


marked as duplicate by batFINGER python May 16 at 21:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Browse other questions tagged or ask your own question.