List all objects in an external .blend with python?

I’d like to be able to get a list of object names in other .blend files, even better if they could be sorted by scene.

Is there a python API for this? Perhaps using bpy and the file browser?

This seems to work to :


def walk(dir):
		file_dict = {}
		gen = os.walk(dir)
		for each_tuple in gen:
			if each_tuple[2] != []:
				
				for filename in each_tuple[2]:
					if filename[-6:] == '.blend':
						if each_tuple[0][-2:] != ".\\":
							filename = "\\"+filename
						name = each_tuple[0]+filename
						file_dict[ name ] = []
						name2 = name.replace("\\", "/")
						fo = open(name2, 'rb')
						fd = str(fo.read())
						
						while 1:
							if 'OB' in fd:
								derived = ""
								t = 0
								while 1:
									if fd[ fd.index("OB")+2+t ] not in ["\\", " "]:
										
										derived += fd[ fd.index("OB")+2+t ]
									else:
										break
									t += 1
								fd = fd.replace("OB", "$$", 1)
								if derived != "":
									file_dict[name].append(derived)
							else:
								break
						
						
		print(file_dict)
		return(file_dict)
walk('.\\data\\models\\')

Because you have 8 or 9 levels of indentation, classically this suggests you would benefit by splitting it up into 2 or more functions. For various reasons, but especially readability

Pay attention it will NOT work if the a *.blend is compressed! (as I do normally!)

Think about loading a .blend
and do something like this


import bpy
tmp = [el.name for el in bpy.data.objects]
tmp.sort()
for index, el in enumerate(tmp): print(index,el)

Add writing to a file and than try ‘batch’ processing …