Created
October 30, 2024 11:18
-
-
Save tombasche/015b06f0754d872f056f7545b6fd5904 to your computer and use it in GitHub Desktop.
Batch export selected objects from Blender to FBX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import os | |
from copy import copy | |
# export to blend file location | |
basedir = os.path.dirname(bpy.data.filepath) | |
if not basedir: | |
raise Exception("Blend file is not saved") | |
view_layer = bpy.context.view_layer | |
obj_active = view_layer.objects.active | |
selection = bpy.context.selected_objects | |
bpy.ops.object.select_all(action='DESELECT') | |
for obj in selection: | |
# Capture original position in the scene | |
original_location = copy(obj.location) | |
obj.select_set(True) | |
# Move object back to the origin | |
obj.location = (0,0,0) | |
view_layer.objects.active = obj | |
name = bpy.path.clean_name(obj.name) | |
if name in ["Light", "Camera"]: | |
continue | |
fn = os.path.join(basedir, name) | |
bpy.ops.export_scene.fbx(filepath=fn + ".fbx", use_selection=True) | |
obj.select_set(False) | |
# reset back to original location in the scene | |
obj.location = original_location | |
print("written:", fn) | |
view_layer.objects.active = obj_active | |
for obj in selection: | |
obj.select_set(True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment