Forum

export helper script

30 August 2017 11:12
Hy I have put these few lines of code together to do individual exports of every object in a scene.

DO NOT USE IN PROJECT FILE !!!!!
USE IN EMPTY FILE NEXT TO THE PROJECT FILE . If you don't understand what's going on don't use this, Thank you!
It imports exports and deletes, one after the other every object in a given blend file. It does the exports but you end up with a empty file.
But i find it useful because i had to link every object in a new file for each object and then go from one file to the other to do all the updates. I ended up with this solution because of the scope of the blender scripts. If you open a file you lose track of your script…
I have one file where i model all objects i need for my b4w app and when i'm done i open a new file with the script and it does all the exports for me and i get all the .json and .bin files i need to load in the project.

If you want to have a look, if you see any optimisation that could be done, let me know. Next thing is going to do a folder for each object group and export every object in a group in a separate folder. A matter of more for in…


import bpy

#create list to get all objects in external source file
sourceList = []
# path to the source file
filepath = "//source.blend"

# build a list of objects in the source file by loading data from it
with bpy.data.libraries.load(filepath) as (data_from, data_to):
data_to.objects = data_from.objects
# now append each object name to the sourceList
for object in data_to.objects:
if object is not None:
sourceList.append(object.name)
print(sourceList)

#UNLOAD DATA ! Else name get suffixes.001 .002 AND we have to re-import every object ONE AT A TIME to export them ONE BY ONE.
for o in bpy.data.objects:
o.select = True
bpy.ops.object.delete()

for mesh in bpy.data.meshes:
bpy.data.meshes.remove(mesh, do_unlink=True)


#now that we have a list lest iterate over it to import, exprort and delete every object
for object_name in sourceList:

#Append object to current scene
blendfile = '/home/user/blenderProject/source.blend'
section = '/Object'
directory = blendfile + section

bpy.ops.wm.append(filename=object_name, directory=directory)

#Add object to the list to later erase mesh from memory
meshes_to_remove = []
for ob in bpy.context.selected_objects:
meshes_to_remove.append(ob.data)

#Do the export
bpy.ops.export_scene.b4w_json(filepath="/home/lukevideo/blenders/scripting/"+object_name+".json")

# delete the object
bpy.ops.object.delete()
# Remove the meshes from memory too
for mesh in bpy.data.meshes:
bpy.data.meshes.remove(mesh, do_unlink=True)
print(meshes_to_remove)


print(bpy.data.meshes)
 
Please register or log in to leave a reply.