How difficult is it to make a selection by collection and set the FBX file name as the collection name?

import bpy

scn = bpy.context.scene
sel = bpy.context.selected_objects
meshes = [o for o in sel if o.type == 'MESH']

for obj in meshes:
bpy.context.view_layer.objects.active = obj
bpy.ops.mesh.quads_convert_to_tris(quad_method='BEAUTY', ngon_method='BEAUTY')

# bpy.ops.mesh.flip_normals() # just flip normals
# bpy.ops.mesh.normals_make_consistent(inside=True) # or recalculate inside
bpy.ops.mesh.normals_make_consistent(inside=False) # or recalculate outside

bpy.ops.export_scene.fbx(filepath="../geometry_result/name.fbx", path_mode='RELATIVE', use_selection=True, object_types={'MESH'})
# bpy.ops.ed.undo()

1 Answer 1


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


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

# 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:

# Export the selection

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
    # Select all vertices
    # Convert to tris
    bpy.ops.mesh.quads_convert_to_tris(quad_method='BEAUTY', ngon_method='BEAUTY')
    # Switch back to Object Mode
    # Export the modified objects to FBX
    # 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:

  • $\begingroup$ Many thanks! Now everything works well, my last task is the team Undo, return the geometry to its original state and I will have a dream addon $\endgroup$
    – KasAndrr
    Commented Jul 16, 2021 at 16:56
  • $\begingroup$ I was in a hurry to rejoice, at the output I get a document in which all the geometry from the collection is located, but only the selected one is needed + I have a number of combination and triangulation commands, they began to be partially ignored. Perhaps I would be satisfied with such an export bpy.ops.export_scene.fbx('INVOKE_DEFAULT') Only a very lacking team UNDO $\endgroup$
    – KasAndrr
    Commented Jul 16, 2021 at 17:43
  • $\begingroup$ @KasAndrr The provided code exports all objects of the collection in context. Make sure the collection you'd like to export is active, I guess your Scene Collection is the active one so the operator will export all objects. Recommend take your time, edit your question, explain in detail what else and post your code. $\endgroup$
    – brockmann
    Commented Jul 16, 2021 at 20:58
  • $\begingroup$ @KasAndrr: Please consider to accept the answer. How this site works: blender.stackexchange.com/tour $\endgroup$
    – Chris
    Commented Jul 20, 2021 at 10:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .