Is there a way to browse all the objects inside a .blend file in the 2.55 API? I would like to know what all the objects are in an arbitrary .blend file so I can add all of them to a scene using bpy.ops.wm.link_append(). This function requires knowing the name of whatever object is being added, it won’t just add everything in the .blend file automatically.
Idea:
write a script to export the names of all objects of the blend, from which you would like to append …,
(bpy.data.objects[:], type name etc.) to a textfile.
Then in the working blend import the info from the text and use what you need?
T
Good idea, I can run Blender in background mode on…
blender.exe -b file1.blend -P exportScript.py
blender.exe -b file2.blend -P exportScript.py
blender.exe -b file3.blend -P exportScript.py
…
blender.exe -b fileN.blend -P exportScript.py
Then run the main scene file with
blender.exe -b mainscene.blend -P mainScript.py
Thanks a lot.
Ahh, now I see, how you use it, can even be set into a ‘batch-file’
Maybe you can ‘give’ us exportScript.py (being lazy).
Yes, here is the script in three parts:
main.py:
import os
BlenderPath = 'C:\Blender2.55\blender.exe'
scene_file = 'C:\scene.blend'
model_files = ['C:\model1.blend', 'C:\model2.blend', 'C:\model3.blend']
for i in model_files:
os.spawnl(os.P_WAIT, BlenderPath, BlenderPath, " -b " + i + " -P exportscript.py"
os.spawnl(os.P_WAIT, BlenderPath, BlenderPath, " -b " + scene_file + " -t 8 -P scenescript.py"
exportscript.py:
import bpy
f = open('C://scene_info.tmp', 'a')
f.write(bpy.data.filepath)
f.write(" ")
objs = bpy.data.objects
for i in objs:
if (i.type == 'MESH'):
f.write(i.name)
f.write(" ")
f.write("
")
f.close()
scenescript.py:
import bpy
def add_objects(){
F = open("C://scene_info.tmp", "r")
text = F.read()
F.close()
words = text.split()
j = 0
path = ''
for i in words:
if (j == 0):
path = i
else:
bpy.ops.wm.link_append(directory = path + '\\Object\\', filename = i)
lastobjadded = bpy.data.objects[len(bpy.data.objects) - 1]
j += 1
}
add_objects()
Vielen Dank!
Oops, exportscript.py and scenescript.py had a serious bug in how they read the objects. Here is the updated code:
main.py:
import os
BlenderPath = 'C:\Blender2.55\blender.exe'
scene_file = 'C:\scene.blend'
model_files = ['C:\model1.blend', 'C:\model2.blend', 'C:\model3.blend']
for i in model_files:
os.spawnl(os.P_WAIT, BlenderPath, BlenderPath, " -b " + i + " -P exportscript.py"
os.spawnl(os.P_WAIT, BlenderPath, BlenderPath, " -b " + scene_file + " -t 8 -P scenescript.py"
exportscript.py:
import bpy
f = open('C://scene_info.tmp', 'a')
objs = bpy.data.objects
obj_list = []
for i in objs:
if (i.type == 'MESH'):
obj_list.append(i.name)
f.write('
"' + bpy.data.filepath + '" ')
f.write('"' + str(len(obj_list)) + '" ')
for name in obj_list:
f.write('"' + name + '"')
f.write(" ")
f.close()
scenescript.py:
import bpy
import re
def add_objects():
F = open("C://scene_info.tmp", "r")
text = F.read()
F.close()
words = re.findall('".*?"', text)
k = 0
path = ''
for i in words:
i = i[1:len(i) - 1:1]
if (k < 0):
k = int(i)
elif (k == 0):
path = i
k = -1
elif (k > 0):
bpy.ops.wm.link_append(directory = path + '\\Object\\', filename = i)
lastobjadded = bpy.data.objects[len(bpy.data.objects) - 1]
k -= 1
add_objects()