1
$\begingroup$

After a data crash I have a couple of thousand files like this "f1288357332.blend". I'm not good at python but there must be a way to rename those files as batch (that are mostly materials) after the name of the material in the first slot of blender.

Until now I have only this:

import bpy
import os

#get current filename

filepath = bpy.data.filepath
filepath_split = os.path.split(filepath) #list with [path, name] 
$\endgroup$
13
  • $\begingroup$ what is the logic you want to use for renaming? like f1288357332.blend what is it supposed to be renamed to? EDIT: oh i didnt read the title. XD $\endgroup$
    – Harry McKenzie
    Commented Aug 19, 2022 at 11:40
  • $\begingroup$ so we can prepare the script assuming you have only 1 mesh object that has only 1 material, or use the material at material index 0. $\endgroup$
    – Harry McKenzie
    Commented Aug 19, 2022 at 11:46
  • $\begingroup$ The problem is most of the files have a material in it but no mesh. So the material is not even applied somewhere... uff i struggle with the logic. $\endgroup$
    – Gibble
    Commented Aug 19, 2022 at 11:48
  • $\begingroup$ ok let me sort that out. i'll update my answer. $\endgroup$
    – Harry McKenzie
    Commented Aug 19, 2022 at 11:57
  • 1
    $\begingroup$ It worked! Thank you so much for the script. Renaming thousends of files is really time consuming. Really Good Work! $\endgroup$
    – Gibble
    Commented Aug 19, 2022 at 13:21

1 Answer 1

2
$\begingroup$

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.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .