5
$\begingroup$

Is there any way to search for an object (such as you would import using the Append option) when not in Blender?

I was a bad boy and made a model inside a .blend file a long time ago and have no idea what file I built it in, and didn't save it as a stand-alone file. Short of searching through every .blend file I have, is there anyway to locate it?

$\endgroup$

2 Answers 2

5
$\begingroup$

Use blendfile.py

Using the blendfile script outlined in answers to Is there a way to acces external .blend file's data with python script? to quickly read blend file and run thru objects in a blend file without having to open it with blender.

Here is a simple demo script which prints out file name and a list of objects within that file.I've simply dumped the blendfile.py and script below as blendobjects.py in a root search folder. (rather than using sys.argv etc.. might get back to it)

# install as module or put in same folder.
import blendfile
from pathlib import Path

def get_id_name(block):
    name = block[b'id', b'name'].decode()
    return name[2:]

for file_path in Path('.').glob('**/*.blend'):
    bf = blendfile.open_blend(file_path)
    objects = bf.find_blocks_from_code(b'OB')
    print(file_path, [get_id_name(o) for o in objects])
    bf.close()

result (run in system console not blender), prints blend file name and a list of objects in that file, which can be redirected into a file and searched.

batfinger@shitbox2:~/Documents/blender$ python ./blendobjects.py 
MS2Props02.blend ['Camera', 'Mesh_0449', 'Mesh_0449.001', 'Mesh_0484', 'Mesh_0484.001', 'Mesh_0490', 'Mesh_0490.001', 'Mesh_0773', 'Mesh_0773.001']
yawpitchroll.blend ['Camera', 'Cube', 'Empty', 'Lamp', 'Suzanne']
tests/snoff.blend ['Camera', 'Cube', 'Lamp']
tests/aubio_tests.blend ['Camera', 'Cube', 'Cylinder', 'Lamp']
tests/untitled.blend ['Camera', 'Grid', 'Lamp']
tests/belt.blend ['Armature', 'Armature.001', 'Camera', 'Cone', 'Empty', 'Empty.001', 'Empty.002', 'Empty.003', 'Lamp', 'Torus']
$\endgroup$
0
0
$\begingroup$

You could search through all blends with a python script. I suppose your clue to the object is its name. The following method finds all objects with a given name and returns their file locations.

Go to the top level directory, from which you want to search files in all subdirectories. (If you're crazy enough, this might just be the root of your drive.)

Create a new file name search_blend.py.

Open it and enter the following script.

import bpy
import os
import os.path


name = "Cube"
save_file = "D:/blndtmp/results.txt"

objects = bpy.data.objects
for o in objects:
    if o.name == name:
        if os.path.exists(save_file):
            append_write = 'a' # append if already exists
        else:
            append_write = 'w'
        with open(save_file, append_write) as save_file:
            save_file.write("Found " + name + " in " + bpy.data.filepath + ".\n")

This script will be executed by every blend file, which we find. You will have to change the name variable to your objects name. name = "my objects name"
Furthermore you will have to set the path in which we save the results. I save my results to my path D:/blndtmp/results.txt. The following lines loop through all objects, compare their name with the search name and write to the save_file if an object with the same name is found.

Assuming you have no python installed, lets use Blenders internal python installation. Open up Blender and save the file in the same directory as the search_blend.py script. I named my file empty_blend.

Your folder will now include these files.

enter image description here

In the empty_blend open up a text editor and paste this script inside.

import os
import subprocess

blender_path = "C:/Program Files/Blender Foundation/Blender/blender.exe"

searched_files = []
for root,dirs,files in os.walk(os.getcwd()):
    for f in files:
        if f[-6:] == ".blend":
            path = os.path.join(root,f)[len(os.getcwd()) + 1:]
            searched_files.append(path)
            subprocess.call([blender_path, path, "--background","--python", "search_blend.py"])

print(searched_files)

Correct the blender_path variable to your Blender path. You are now ready to execute the script. You may want to open up the Console first to see if it is running correctly. The script searches for all files in all subdirectories with os.walk(), compares their extension, and if it is a .blend file, executes is in background mode deploying the search_blend.py script.

Hit Run Script in the text editor to run the script.

After its completion, it will have created a new file next to search_blend.py and empty_blend.blend, if an object was found. It is the results path, which we entered in the search_blend.py script.

My results.txt looked like this after a test run.

Found Cube in D:\blndtmp\files\a.blend.
Found Cube in D:\blndtmp\files\c.blend.
Found Cube in D:\blndtmp\files\more files\a.blend.
Found Cube in D:\blndtmp\files\more files\c.blend.
$\endgroup$
2
  • 1
    $\begingroup$ Couple of things would use blender_path = bpy.app.binary_path. and rather than using search_blend.py above have a look at using libraries in your second script to list object names. $\endgroup$
    – batFINGER
    Commented May 30, 2018 at 11:05
  • $\begingroup$ Wow! Thanks so much. I'll give this a try and report how I did. $\endgroup$ Commented May 30, 2018 at 21:30

You must log in to answer this question.

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