Skip to content

Instantly share code, notes, and snippets.

@ztffn
Created February 26, 2024 10:38
Show Gist options
  • Save ztffn/170f238aeb1425966b6c087a1246510a to your computer and use it in GitHub Desktop.
Save ztffn/170f238aeb1425966b6c087a1246510a to your computer and use it in GitHub Desktop.
Python script to batch clean up Blender files
# This script automates the processing of FBX files using Blender's Python API. It is designed to import an FBX file,
# remove a specific camera object, reset and apply all rotations and scales to the mesh objects, rename the mesh objects
# for clarity, and then export the modified scene back to an FBX file. The script is intended for use in batch processing
# or integration into a larger automated workflow, where FBX files need to be cleaned up or standardized before further use.
import bpy
import sys
import os
# Function to process the FBX file
def process_fbx(file_path, export_path):
# Import the FBX file
bpy.ops.import_scene.fbx(filepath=file_path)
# Delete the camera named 'CINEMA_4D_Editor'
if 'CINEMA_4D_Editor' in bpy.data.objects:
bpy.data.objects['CINEMA_4D_Editor'].select_set(True)
bpy.ops.object.delete()
# Define the directory to save the exported FBX files
export_directory = "/Users/x/"
# Set all rotations to 0 and apply all rotations and scale
for obj in bpy.data.objects:
if obj.type == 'MESH':
# Set rotations to 0
obj.rotation_euler = (0, 0, 0)
# Apply rotation and scale
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
# Rename the mesh
original_name = obj.name
obj.name = original_name + "_mesh"
# Deselect the object
obj.select_set(False)
# Extract the base name of the input file
input_base_name = os.path.basename(file_path)
# Replace the existing extension with '.fbx'
export_file_name = os.path.splitext(input_base_name)[0] + ".fbx"
# Create the full export path
export_path = os.path.join(export_directory, export_file_name)
# Export the processed file
bpy.ops.export_scene.fbx(filepath=export_path, use_selection=True, bake_space_transform=False)
# Get the file path from the command line arguments
input_file_path = sys.argv[-2]
output_file_path = sys.argv[-1]
# Process the FBX file
process_fbx(input_file_path, output_file_path)
# END OF FILE
_____________________________
# Batch script OSX, Run through console.
import os
import subprocess
# Directory containing the FBX files
source_directory = "/Users/x/
# Directory to save the processed files
output_directory = "/Users/x/"
# Path to the Blender executable
blender_executable = "/Applications/Blender.app/Contents/MacOS/Blender"
# Path to the processing script
processing_script = "/Users/x/"
# Loop through all FBX files in the source directory
for file_name in os.listdir(source_directory):
if file_name.endswith(".FBX"):
input_path = os.path.join(source_directory, file_name)
output_path = os.path.join(output_directory, file_name)
# Call Blender in headless mode to process the file
print("Processing:", input_path)
subprocess.call([
blender_executable,
"--background",
"--python", processing_script,
"--", input_path, output_path
])
print("Batch processing complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment