Bulk Export - SketchUp Models to .FBX

This script selects everything in a SketchUp file, explodes the model, and then exports selections by tags to a .FBX file location of your choosing.

model = Sketchup.active_model
layers = model.layers
# Ensure all layers/tags are visible and nothing is hidden
layers.each do |layer|
layer.visible = true
end
# Unhide all entities in the model
model.entities.each do |entity|
entity.hidden = false if entity.respond_to?(:hidden=)
end
# Display a friendly greeting and information about the script
UI.messagebox(
"Welcome to the 'SketchUp Tags Batch Exporter to .FBX Files'!\n\n" \
"This tool will help you by exploding all groups and components " \
"in your model and exporting each layer/tag as a separate .fbx file. " \
"You'll be prompted to choose where to save the files.\n\n" \
"Please ensure you save your work before continuing, as this process " \
"is destructive and will modify your model structure.",
MB_OK
)
# Prompt user for export directory
export_dir = UI.select_directory(title: "Select Export Directory for .fbx Files")
if export_dir.nil?
puts "Export directory selection was canceled. Exiting script."
return
end
# Check if layers are enumerable
if layers.respond_to?(:each)
layers.each do |layer|
# Create a new selection set for entities with the current layer
selection = model.selection
selection.clear
# Add entities with the current layer to the selection
model.entities.each do |entity|
if entity.respond_to?(:layer) && entity.layer == layer
selection.add(entity)
end
end
next if selection.empty? # Skip if no entities are tagged with this layer
# Explode all groups and components in the selection
entities_to_explode = selection.to_a
while !entities_to_explode.empty?
new_entities = []
entities_to_explode.each do |entity|
if entity.is_a?(Sketchup::Group) || entity.is_a?(Sketchup::ComponentInstance)
exploded = entity.explode
new_entities.concat(exploded) if exploded
end
end
entities_to_explode = new_entities
end
# Ensure the export path is valid and sanitize the layer name
layer_name = layer.name.gsub(/[:"\/\\|?*]/, "_") # Sanitize layer name
export_path = File.join(export_dir, "#{layer_name}.fbx")
# FBX export options
options = {
:selectionset_only => true,
:triangulated_faces => true,
:texture_maps => true
}
# Export the selection to a .fbx file
status = model.export(export_path, options)
if status
puts "Exported #{layer.name} successfully to #{export_path}!"
else
puts "Failed to export #{layer.name}."
end
end
# Notify user that export has completed
UI.messagebox(
"Export completed successfully!\n\n" \
"Reminder: Do not save the project to preserve your original groups " \
"and components as they have been deleted.",
MB_OK
)
else
puts "Unexpected type for layers: #{layers.class}. Expected an Enumerable collection of layers."
end
# Safely reset the model without saving changes
Sketchup.file_new
Description
It is an amazing time to be alive. I use a variety of tools for my job and most of the AVL industry is not using Blender as a way to distribute product files.
Understandable but sad.
To get these models into Blender, SketchUp supports exports to .fbx files which Blender can then import.
I CAN get my hands on quite a few SketchUp files but I don't want to sit exporting every single model and reimporting them one by one into Blender. I want to do it in batches. With the help of ChatGPT, here is a script that I am using to speed up the process.
Hopefully, it can help you too! If you are using SketchUp 2024, you may get errors related to tags vs. layers but if you just copy the code and put it back in ChatGPT and ask it to switch out for tags instead of layers it should work.
Happy Sketching & Blending!
