This script is a Blender Python script that exports each selected object in the scene into its own file, specifically in FBX format.
![]() |
Blender Python script that exports each selected object |
# exports each selected object into its own file
import bpy
import
os
# 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:
obj.select_set(True)
#
some exporters only use the active object
view_layer.objects.active = obj
name =
bpy.path.clean_name(obj.name)
fn = os.path.join(basedir,
name)
bpy.ops.export_scene.fbx(filepath=fn + ".fbx",
use_selection=True)
# Can be used for multiple formats
# bpy.ops.export_scene.x3d(filepath=fn + ".x3d", use_selection=True)
obj.select_set(False)
print("written:", fn)
view_layer.objects.active
= obj_active
for obj in selection:
obj.select_set(True)
Here's a breakdown of the script:
- 1. Import Statements: The script imports the necessary modules, including `bpy` for Blender Python API and `os` for interacting with the operating system.
- 2. Setting the Export Directory (`basedir`): The script sets the export directory (`basedir`) to the location of the Blender file. If the Blender file has not been saved, it raises an exception.
- 3. Accessing the Active Object and Selected Objects: It retrieves the active object and the list of selected objects in the current view layer.
- 4. Deselecting All Objects: It deselects all objects to ensure that only the selected objects are exported.
- 5. Exporting Each Selected Object: It iterates through each selected object, sets it as the active object, and exports it as an FBX file using `bpy.ops.export_scene.fbx()`. The file is named based on the cleaned name of the object, and the export path is formed by joining the `basedir` and the file name.
- 6. Reverting to the Original Selection: After exporting, it reverts the selection to the original state and sets the original active object.
- 7. Optional Formats: There's a commented line (`# bpy.ops.export_scene.x3d(...)`) that shows how you could potentially export to other formats by uncommenting and modifying the line accordingly.
- 8. Print Statements: It prints a message indicating the file that has been written for each exported object.
To use this script:
- Copy and paste it into a new text file in Blender's Text Editor.
- Run the script by clicking the "Run Script" button.
This Python script for Blender is designed to automate the export of each selected object in the scene into individual FBX files. Let's break down its functionality and key features:
1. Export Directory Determination (`basedir`):
The script establishes the export directory (`basedir`) by retrieving the path of the currently opened Blender file. It ensures that the Blender file has been saved before proceeding. If the file is unsaved, an exception is raised.
basedir = os.path.dirname(bpy.data.filepath) if not basedir: raise Exception("Blend file is not saved")
2. Handling Active and Selected Objects:
- The active object (`obj_active`) and the list of selected objects (`selection`) in the current view layer are obtained.
view_layer = bpy.context.view_layer obj_active = view_layer.objects.active selection = bpy.context.selected_objects
3. Iterating Through Selected Objects:
- The script then iterates through each selected object, deselecting all other objects and setting the current object as the active one.
for obj in selection: obj.select_set(True) view_layer.objects.active = obj
4. Exporting Each Object as FBX:
- For each selected object, it exports the object to an FBX file using the `bpy.ops.export_scene.fbx()` operator. The export path is formed by joining the `basedir` and the cleaned name of the object.
bpy.ops.export_scene.fbx(filepath=fn + ".fbx", use_selection=True)
5. Reverting Selection and Active Object:
- After exporting an object, the script reverts the selection and active object to their original states.
for obj in selection: obj.select_set(False) view_layer.objects.active = obj_active
6. Optional Format Export:
- The script includes a commented line that demonstrates how you could potentially export to other formats, such as X3D, by uncommenting and modifying the line accordingly.
# bpy.ops.export_scene.x3d(filepath=fn + ".x3d", use_selection=True)
7. Print Statements:
- During the export process, the script prints a message indicating the file that has been written for each exported object.
print("written:", fn)
8. Usage:
- The script is intended to be run from the Blender Text Editor. Select the objects you want to export in the 3D View before executing the script.
9. Flexibility:
- You can modify the script to customize export settings, file naming conventions, or target different file formats, depending on your specific requirements.
In summary, this script streamlines the process of exporting multiple selected objects into separate FBX files, offering a practical solution for users who need to manage individual exports for each object in their Blender scenes.
0 Comments
Welcome to our website!