Skip to content

Instantly share code, notes, and snippets.

@esnya
Last active March 30, 2021 12:58
Show Gist options
  • Select an option

  • Save esnya/ba0483f44faab3b0106398369e9ca548 to your computer and use it in GitHub Desktop.

Select an option

Save esnya/ba0483f44faab3b0106398369e9ca548 to your computer and use it in GitHub Desktop.
Bulk Import FBX
$override = $FALSE
$threads = 24
Get-ChildItem -Recurse *.fbx | ForEach-Object -ThrottleLimit $threads -Parallel {
$srcDir = $_.DirectoryName
$src = $_.FullName
$baseName = $_.BaseName
$dst = "$srcDir\$baseName.blend"
if (-not $(Test-Path $dst) -or $override) {
Write-Output "$src > $dst"
& "D:\SteamLibrary\steamapps\common\Blender\blender.exe" -b -P bulk_import_fbx2blend.py -- "$src" "$dst"
}
}
import bpy
import glob
import os
import sys
from pathlib import Path
def cleanup():
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=True)
def importFbx(src):
bpy.ops.import_scene.fbx(filepath=src, axis_forward="-Z", axis_up="Y")
def findMissings(src):
bpy.ops.file.find_missing_files(directory=os.path.dirname(src))
def optimize(src):
o = [o for o in bpy.data.objects if o.type == 'MESH'][0]
o.select_set(True)
bpy.context.view_layer.objects.active = o
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.join()
bpy.ops.object.select_all(action='DESELECT')
o = [o for o in bpy.data.objects if o.type == 'MESH'][0]
o.select_set(True)
o.data.use_auto_smooth = True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.remove_doubles(threshold=0.1)
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')
[bpy.data.objects.remove(o) for o in bpy.data.objects if o.type == 'EMPTY']
def save(dst):
bpy.ops.file.pack_all()
bpy.ops.wm.save_as_mainfile(filepath=dst, check_existing=False)
def bulk_import():
argv = sys.argv[sys.argv.index("--") + 1:]
src = argv[0]
dst = argv[1]
#dst = bpy.data.filepath
#src = str(Path(dst).with_suffix('.fbx'))
print("Cleanup")
cleanup()
print("Import FBX")
importFbx(src)
print("Find Missings")
findMissings(src)
print("Optimize")
optimize(src)
print("Save")
save(dst)
bulk_import()
$threads = 24
Get-ChildItem -Recurse *.blend | ForEach-Object -ThrottleLimit $threads -Parallel {
Write-Output $_.FullName
& "D:\SteamLibrary\steamapps\common\Blender\blender.exe" -b $_.FullName -P bulk_rename.py
}
import bpy
from pathlib import Path
def rename():
name = Path(bpy.data.filepath).stem
print(name)
for o in bpy.data.objects:
print("Rename: " + o.name)
o.name = name
o.data.name = name
bpy.ops.wm.save_mainfile(filepath=bpy.data.filepath)
print("!!Renamed!!")
rename()
import bpy
import sys
import os
print("Initializing")
argv = sys.argv[sys.argv.index("--") + 1:]
ratio = float(argv[0])
dst = argv[1]
print("Prepareing")
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
o = [o for o in bpy.data.objects if o.type == 'MESH'][0]
bpy.context.view_layer.objects.active = o
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
print("Decimating")
bpy.ops.mesh.decimate(ratio=ratio)
print("Cleanup")
bpy.ops.object.mode_set(mode='OBJECT')
print("Saving")
filepath=dst
bpy.ops.wm.save_as_mainfile(filepath=filepath)
print("Done")
import bpy
import os
import sys
from pathlib import Path
from glob import glob
def append(file):
with bpy.data.libraries.load(file) as (data_from, data_to):
data_to.objects = data_from.objects
for o in data_to.objects:
bpy.context.collection.objects.link(o)
argv = sys.argv[sys.argv.index("--") + 1:]
dst = argv[0]
patterns = argv[1:]
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
for pattern in patterns:
for file in glob(pattern, recursive=True):
print(file)
append(file)
bpy.ops.wm.save_as_mainfile(filepath=dst)
import bpy
print('Prepare')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
o = [o for o in bpy.data.objects if o.type == 'MESH'][0]
bpy.context.view_layer.objects.active = o
o.select_set(True)
if ('Lightmap' not in o.data.uv_layers):
print('Createing Lightmap Layer')
o.data.uv_layers.new(name='Lightmap')
o.data.uv_layers.active = o.data.uv_layers['Lightmap']
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
if ('dem' in o.name):
print('Unwrapping')
bpy.ops.uv.unwrap()
else:
print('Packing')
bpy.ops.uv.lightmap_pack()
bpy.ops.object.mode_set(mode='OBJECT')
o.data.uv_layers.active = o.data.uv_layers[0]
print('Saving')
bpy.ops.wm.save_mainfile()
print('Done')
# mkdir bird
# mkdir dem
# mkdir LOD1
# mkdir LOD2
Get-ChildItem *.zip -Recurse | ForEach-Object { & 'C:\Program Files\7-Zip\7z.exe' x $_.FullName "-o$($_.BaseName)" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment