Skip to content

Instantly share code, notes, and snippets.

@ecmjohnson
Forked from PierrickKoch/dae_obj.py
Last active May 20, 2021 10:17
Show Gist options
  • Save ecmjohnson/5f1c4837b84be4422284cc8be3f06004 to your computer and use it in GitHub Desktop.
Save ecmjohnson/5f1c4837b84be4422284cc8be3f06004 to your computer and use it in GitHub Desktop.
Batch convert Collada to Wavefront (.dae to .obj) using Blender
"""
Batch convert Collada to Wavefront (.dae to .obj) using Blender
usage: blender -b -P dae_obj.py -- ./in_dir/ ./out_dir/
"""
import sys
import os
import bpy # Blender Python API
def clear():
""" Clears the scene """
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
def usage():
sys.stderr.write(__doc__)
def convert(fdae, fobj):
""" Converts individual .dae file to .obj """
bpy.ops.wm.collada_import(filepath=fdae)
bpy.ops.export_scene.obj(filepath=fobj)
def main(argv=[]):
args = []
if '--' in argv:
args = argv[(argv.index('--')+1):]
if len(args) < 2:
usage()
return 1
ddae = args[0]
dobj = args[1]
# Select only .dae files in the input directory
fdaes = [os.path.join(ddae, f) for f in os.listdir(ddae)]
fdaes = list(filter(lambda x: x.endswith('.dae'), fdaes))
# Create the output directory if necessary
os.makedirs(dobj, exist_ok=True)
# Convert every file keeping the base name
for fdae in fdaes:
clear()
modelname = os.path.basename(fdae).rstrip('.dae')
fobj = os.path.join(dobj, modelname + '.obj')
convert(fdae, fobj)
if __name__ == '__main__':
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment