0
$\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 after the Object or mesh name in the file. i got the code to rename after the material but not for the object or mesh name. And now i found out there are not only materials in the blend files.

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 = ''



and iam a absolut beginner in python - someone here wrote the script Here is the my question for rename the script after material:

Batch-Rename-File-after-Material-Slot


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]) 

That is the code for rename after the material but i need this to rename after the object file now. Can somebody help me?

$\endgroup$
7
  • $\begingroup$ If there is only one object in the scene then you can just get the name of the object with obj_name = bpy.data.objects[:][0].name and then substitute that into the rename part of the script, instead of using the material name. $\endgroup$
    – Jakemoyo
    Commented Aug 30, 2022 at 12:12
  • $\begingroup$ Look at this part of the API to get a better understanding of what you are able to access via bpy.data $\endgroup$
    – Jakemoyo
    Commented Aug 30, 2022 at 12:14
  • $\begingroup$ you want to rename only after the object name? or also sometimes after the material name? what's the naming priority order? $\endgroup$
    – Harry McKenzie
    Commented Aug 30, 2022 at 14:13
  • $\begingroup$ Did I miss something, or this was not in the original post? iam a absolut beginner in python - someone here wrote the script Here the orginal question: [Batch-Reanme](https://blender.stackexchange.com/questions/273349/batch-rename-a-blend-file-after-the-first-object-or-mesh-name-in-the-file) Plus the link points to the current post. $\endgroup$
    – Lauloque
    Commented Aug 30, 2022 at 14:21
  • 1
    $\begingroup$ @HarryMcKenzie you are the hero of the day i tried it out and it worked. Now after 10000 Materials renamed iam so happy. You are the best! $\endgroup$
    – Gibble
    Commented Aug 30, 2022 at 17:12

1 Answer 1

1
$\begingroup$
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':
        continue
    new_filename = os.path.join(newpath, o.name + ".blend")
    bpy.ops.wm.save_as_mainfile(filepath=new_filename)
    break

if new_filename == '':
    print(filepath, ": Warning no mesh found!")
$\endgroup$
1
  • 1
    $\begingroup$ HarryMCKenzie, you are the hero of the day i tried it out and it worked. Now after 10000 Materials renamed iam so happy. You are the best! Thank you so much! I marked the question as solved! $\endgroup$
    – Gibble
    Commented Aug 30, 2022 at 17:25

You must log in to answer this question.

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