Skip to content

Instantly share code, notes, and snippets.

@marcgpuig
Created April 9, 2022 15:27
Show Gist options
  • Save marcgpuig/49b25470273c6f8cb4f262e55b9867dd to your computer and use it in GitHub Desktop.
Save marcgpuig/49b25470273c6f8cb4f262e55b9867dd to your computer and use it in GitHub Desktop.
Custom Blender 3.1+ FBX Exporter: Individually export all the selected objects and centers them on X and Y.
# Individually export all the selected objects and centers them on X and Y.
#
# You have to provide the export folder path, if not provided, files
# will be exported under the same blend file directory under "/custom_export"
#
# Based on: https://blender.stackexchange.com/a/5383
import bpy
import os
# Absolute path to the folder where to export all the files.
# Windows e.g: "C:\\Users\\user\\exported_meshes"
export_folder_path = "C:\\Users\\my_user\\output_folder" # <-- Change this path!
# If the folder is not valid, export where the blend file is located.
if not os.path.isdir(export_folder_path):
export_folder_path = bpy.path.abspath('//custom_export')
if not os.path.isdir(export_folder_path):
os.mkdir(export_folder_path)
# Get all the selected objects that are meshes.
selected_objects = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
# Unselect everything since we will select one by one to individual export.
bpy.ops.object.select_all(action='DESELECT')
for object in selected_objects:
# Store the original location.
original_location = object.location.copy()
# Move to x=0 and y=0 so the mesh is conviniently exported with the pivot in the middle.
object.location = (0.0, 0.0, original_location.z)
# Generate the file path where it will be exported (export_folder_path + object.name + .fbx)
file_path = os.path.join(export_folder_path, object.name + ".fbx")
# Select the current object to be exported.
object.select_set(True)
# Export as FBX.
bpy.ops.export_scene.fbx(
filepath=file_path,
use_selection=True,
# Add more export options if needed. Those can be found here:
# https://docs.blender.org/api/dev/bpy.ops.export_scene.html
)
# Move the object back to its original location.
object.location = original_location
object.select_set(False)
# Select the original objects again so the scene is the same initial sate.
for object in selected_objects:
object.select_set(True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment