This script assumes you have at least 1 object with a mesh which has at least 1 material slot and names the final file by the material name and saves it in a new directory generated
inside the directory of all your blend files. The first object with material it finds in the loop will be used. If no mesh is found, it will use the last material name in the bpy.data.materials
list.
Save this script in a separate file as rename-script.py
import bpy
import os
filepath = bpy.data.filepath
basepath, filename = os.path.split(filepath)
newpath = os.path.join(basepath, "generated")
os.makedirs(newpath, exist_ok=True)
new_filename = ''
for o in bpy.data.objects:
if o.type != 'MESH' or len(o.material_slots) <= 0:
continue
new_filename = os.path.join(newpath, o.material_slots[0].name + ".blend")
bpy.ops.wm.save_as_mainfile(filepath=new_filename)
break
if new_filename == '':
print("no mesh found using first material in the materials list")
idx = len(bpy.data.materials) - 1
new_filename = os.path.join(newpath, bpy.data.materials[idx].name + ".blend")
bpy.ops.wm.save_as_mainfile(filepath=new_filename)
Then open Blender and under the Scripting tab paste the following script into the Text Editor
. This script will iterate through all your blend files in the directory you have assigned to variable BLEND_FILES_DIRECTORY
and save them into a sub directory called generated
. Make sure the variable RENAME_SCRIPT
is pointing to wherever you saved your rename-script.py
file. So you can safely delete generated
directory if you want to generate your files again.
import bpy
import os
import glob
from subprocess import call
BLEND_FILES_DIRECTORY = r"/path/to/your/blend/files" #example path: C:\Users\xxxx\Desktop\blendfiles
RENAME_SCRIPT = r"/path/to/rename-script.py" #example path: C:\Users\xxxx\Desktop\blendfiles\rename-script.py
for file in glob.glob(os.path.join(BLEND_FILES_DIRECTORY, '*.blend')):
basepath, filename = os.path.split(file)
print("process file:", filename)
call(['blender','-b', file, '-P', RENAME_SCRIPT])
Open your System Console
to see the progress. You can open it by going to Window > Toggle System Console.