1
$\begingroup$

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

import bpy
bpy.ops.object.apply_all_modifiers()
bpy.ops.object.join()

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.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
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.object.mode_set()
bpy.ops.export_scene.fbx(filepath="../geometry_result/name.fbx", path_mode='RELATIVE', use_selection=True, object_types={'MESH'})
# bpy.ops.ed.undo()
$\endgroup$

1 Answer 1

3
$\begingroup$

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:

$\endgroup$
4
  • $\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 .