# How to batch rename materials or auto assign them to the name of the texture file

I'm currently working on porting a section of a world/map from one game to use in source filmmaker. I've managed to import the section I wanted and I'm stuck at a very slow and repetitive process.

What I have is dozens of objects (which I imported directly from my game), the material names were assigned based on the name of the object and the texture was set accordingly.

So lets say an object I imported is called chair, and this object has 3 materials or so. Each material will be named chair.001, chair.002, chair.003, etc etc. Now, the texture files/images are named correctly, lets say the textures are named chairwood chairclooth chairmetal or something of that sort. What I need is to be able to automatically merge the names of the materials with the textures they are assigned, so whatever the filename is the material is automatically changed to it. Several of the objects I have use the same texture file whilst having differently named materials, so you can see why its a royal pain.

Pretty much I need to know if I can do any of the following things with or or without existing addons/scripts or default tools in blender. 1) Can you select/batch multiple materials and rename them to all have the same name? 2) Convert/export textures to have them the same as the material?

I've included a imgur link of what I'm trying to explain, maybe it'll be easier to understand what I'm getting at https://i.imgur.com/GXsb332.png

Using blender internal, the textures can be found in bpy.data.textures. By looping through this you can easily match the texture name to the image it has.

import bpy

for tex in bpy.data.textures:
if tex.type == 'IMAGE':
tex.name = tex.image.name


Usually the image name is its filename without extension, if you have image names with extensions, you can use os.path.splitext to remove the extension.

tex.name = os.path.splitext(tex.image.name)[0]

• Not entirely sure what you mean. The code you have provided me, is this something I can just copy paste into text editor and run or is it just a example? I’m still relatively new to Blender so I haven’t picked up on the name of each tool/section/options it has, mainly learned stuff through testing things out. How would I access blender internal, is that some sort of script/text file editable within blender? Forgive me if I sound stupid lol, this is my first time making a help thread on Blender. – DrJoseEvil Nov 13 '18 at 14:42
• @DrJoseEvil Blender internal is the render engine, the way your textures show in the outliner means that is what your materials are setup for. The above script should just do what you want as it is, paste it into blenders text editor and run it, then check things look right before saving. – sambler Nov 13 '18 at 20:52
• The script is giving me a error On line 5 AttributeError: 'NoneType' object has no attribute 'name' Error: Python script fail, look in the console for now... – DrJoseEvil Nov 14 '18 at 23:03