0
$\begingroup$

I have a kitbash file and I want to export each one of the assets to a seprate fbx file for easy viewing and to ease on my pc since the whole .blend file contains around 15 mil polys.

enter image description here

I was wondering if there is an option in blender python scripting to export multiple groups with the meshes in their groups like in the picture above. so .py operation will make a "KB3D_LAB_AC_A_grp.fbx" file that will contain the files in that group and will do the same for "KB3D_LAB_AC_B_grp" and so on.

or maybe replace or make a new collection for each one of the empties in order to export using collection with the regular collection batch in the fbx export.

$\endgroup$

1 Answer 1

0
$\begingroup$
import bpy


def export_fbx(context, path: str, name: str):
    ''' Export FBX file.

    path (str) - Filepath to save the file to.
    name (str) - Name of the file.
    '''
    bpy.ops.export_scene.fbx(context,

        filepath = path + name + '.fbx',
        check_existing = True,
        filter_glob = '*.fbx',
        use_selection = True,
    )
    return path + name + '.fbx'


# change 'Collection' to your collection
for object in bpy.data.collections['Collection'].objects:
    if not object.parent:
        override = bpy.context.copy()
        override['selected_objects'] = list(object.children_recursive)

        # set object origin to geometry
        # NOTE: can't undo
        children = [o for o in object.children_recursive if o.type == 'MESH']
        bpy.ops.object.origin_set({
            "object" : children[0],
            "selected_objects" : children,
            "selected_editable_objects" : children,
        })

        # change 'C:\\Users\\karan\\Desktop\\' to your path
        export_fbx(context=override, path=bpy.path.abspath('C:\\Users\\karan\\Desktop\\'), name=object.name)

Added:

# set object origin to geometry
# NOTE: can't undo
children = [o for o in object.children_recursive if o.type == 'MESH']
bpy.ops.object.origin_set({
    "object" : children[0],
    "selected_objects" : children,
    "selected_editable_objects" : children,
})
$\endgroup$
3
  • $\begingroup$ Amazing! thank you! btw is there a way to make this some sort of an addon? so you can select the filepath and the collections you want to export. $\endgroup$
    – Proxeuz
    Feb 27 at 13:28
  • $\begingroup$ Also is there a way to set origin to geometry with export? so the origin will be in the center of the object. $\endgroup$
    – Proxeuz
    Feb 27 at 13:53
  • $\begingroup$ I added set origin to geometry while exporting, but it can't be reverted back after export. You have to undo it with ctrl + Z manually. $\endgroup$
    – Karan
    Feb 28 at 3:17

You must log in to answer this question.

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