Skip to content

Instantly share code, notes, and snippets.

@AdamK2003
Last active July 21, 2023 06:31
Show Gist options
  • Save AdamK2003/13c624871dce6cd469f536d5fbcd03f3 to your computer and use it in GitHub Desktop.
Save AdamK2003/13c624871dce6cd469f536d5fbcd03f3 to your computer and use it in GitHub Desktop.
import os
import shutil
import bpy
basedir = 'E:\! TOTK' # base dir, other dirs are relative to this
dirs = ['modelsAndTextures'] # input dirs
fbxdir = 'FBX' # output dir
faileddir = 'FBX-failed' # FBX files should get moved here if an error happens after the file was created (unlikely)
blenddir = 'Blend' # the script will create a .blend file and drop it here in case it fails to export the fbx file
daedonedir = 'daeDone' # successfully processed .dae files will be moved to this directory
pathList = []
failedList = []
def scanDir(dir):
files = os.listdir(dir)
for file in files:
if file.endswith('.dae') and os.path.isfile(dir + '/' + file):
pathList.append(dir + '/' + file)
if os.path.isdir(dir + '/' + file):
scanDir(dir + '/' + file)
for dir in dirs:
scanDir(basedir + '/' + dir)
for path in pathList:
try:
print(path)
pathElements = path.split('/')
filename = pathElements[-1]
basename = '.'.join(filename.split('.')[:-1])
for w in bpy.context.window_manager.windows:
s = w.screen
for a in s.areas:
if a.type == "VIEW_3D":
with bpy.context.temp_override(window=w, area=a):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
bpy.ops.outliner.orphans_purge()
bpy.ops.outliner.orphans_purge()
bpy.ops.outliner.orphans_purge()
bpy.ops.wm.collada_import(filepath=path, fix_orientation=True, keep_bind_info=True)
bpy.ops.export_scene.fbx(filepath=basedir + '/' + fbxdir + '/' + basename + '.fbx', add_leaf_bones=True, path_mode='COPY', embed_textures=True)
break
try:
shutil.move(path, basedir + '/' + daedonedir)
except:
print('Can\'t move DAE file')
except:
print("Error on " + path)
failedList.append(path)
pathElements = path.split('/')
filename = pathElements[-1]
basename = '.'.join(filename.split('.')[:-1])
try:
shutil.move(basedir + '/' + fbxdir + '/' + basename + '.fbx', basedir + '/' + faileddir)
except:
print('Failed to move FBX file')
for w in bpy.context.window_manager.windows:
s = w.screen
for a in s.areas:
if a.type == "VIEW_3D":
with bpy.context.temp_override(window=w, area=a):
try:
bpy.ops.wm.save_as_mainfile(filepath=basedir + '/' + blenddir + '/' + basename + '.blend')
except:
print('Failed to save blend file')
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
bpy.ops.outliner.orphans_purge()
bpy.ops.outliner.orphans_purge()
bpy.ops.outliner.orphans_purge()
break
print(failedList)
with open(basedir + '/failed.json', 'w', encoding='utf8') as f:
f.writelines(failedList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment