Export all objects of a certain collection
Pretty much straight forward. Just pass use_active_collection=True argument when calling export_scene.fbx() operator and assemble your filepath using Collection.name:
import bpy
C = bpy.context
bpy.ops.export_scene.fbx(
filepath=bpy.path.abspath("//{}.fbx".format(C.collection.name)),
use_active_collection=True
)
Note that all modifiers will be applied by default, no need to do anything.
Export selected objects of a certain collection
If you'd like to modify the mesh objects beforehand, you would have to select them anyway so just make sure the objects you want to export are selected and pass use_selection=True to the bpy.ops.export_scene.fbx() operator.
Demo on how export all objects of the collection in context having Cube in their name:
import bpy
C = bpy.context
Deselect all objects
bpy.ops.object.select_all(action="DESELECT")
Get the desired objects of the active collection
mesh_objects = [o for o in C.collection.objects if "Cube" in o.name]
for obj in mesh_objects:
obj.select_set(True)
Export the selection
bpy.ops.export_scene.fbx(
filepath=bpy.path.abspath("//{}.fbx".format(C.collection.name)),
use_selection=True
)
Note that you can also pass a set of object types to the operator in order to export certain types of objects: object_types={'EMPTY', 'CAMERA', 'LIGHT', 'MESH', 'OTHER'}
Modify and export all mesh objects of a certain collection
As of 2.8x we can enter Edit Mode for all selected objects simultaneously, which avoids looping over all the objects as well as switching to Edit Mode and Object Mode each iteration. Also I'd suggest do not modify the original meshes, duplicate the objects you want to modify, edit and delete them.
Demo on how to duplicate all mesh objects of the active collection, switch into Edit Mode, convert the geometry into triangles, export the modified geometry to FBX and lastly remove all temporary objects from the scene:
import bpy
C = bpy.context
Get all mesh objects of the active collection
mesh_objects = [o for o in C.collection.objects if o.type == 'MESH']
If mesh objects list is not empty
if mesh_objects and C.mode == 'OBJECT':
# Duplicate all mesh objects and get their references
bpy.ops.object.duplicate({'selected_objects' : mesh_objects})
mesh_dupes = C.selected_objects
# Set one object of the dupes to the active object
C.view_layer.objects.active = mesh_dupes[0]
# Switch into Edit Mode
bpy.ops.object.mode_set(mode='EDIT')
# Select all vertices
bpy.ops.mesh.select_all(action='SELECT')
# Convert to tris
bpy.ops.mesh.quads_convert_to_tris(quad_method='BEAUTY', ngon_method='BEAUTY')
# Switch back to Object Mode
bpy.ops.object.mode_set(mode='OBJECT')
# Export the modified objects to FBX
bpy.ops.export_scene.fbx(
filepath=bpy.path.abspath("//{}.fbx".format(C.collection.name)),
use_selection=True)
# Delete all duplicates
bpy.ops.object.delete({"selected_objects" : mesh_dupes})
Make sure the desired collection is active in the Outliner and run the script.
If you'd like to turn this script into an operator, have a look into:
Perhaps I would be satisfied with such an export bpy.ops.export_scene.fbx('INVOKE_DEFAULT')
Only a very lacking team UNDO
– KasAndrr Jul 16 '21 at 17:43