1
$\begingroup$

I need to export around 200 meshes into FBX, individually, organized with object names. The old method I used before was creating an export preset and export them one-by-one, with the right names. That was bearable with 70 meshes, but rather frustrating with anything beyond.

So my question is the given: Can I export 200 meshes into 200 files with their object names as names?

I have a few complex, animated meshes that I would rather export the old-fashioned way, but if I could skip the other 190 meshes, that would be sweet.

$\endgroup$
2
  • 1
    $\begingroup$ Too little information for writing a script. No list of FBX settings you need, no principles by which objects are meant to be determined by the script for the separate export. Too many probabilities, I'm afraid. $\endgroup$ – Andrey Sokolov Mar 15 '20 at 16:03
  • $\begingroup$ You are right. I didn't specifi that I need to export them 1.00 Scale, apllied Local Scales with -Z forward / Y up and applied Unit and Transform. I'm sorry for the lack of information. $\endgroup$ – 505Legion Mar 16 '20 at 16:54
4
+50
$\begingroup$

Addon based on this answer, adapted to export all meshes in selection to single fbx files.

enter image description here

# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>

bl_info = {
    "name": "Batch export FBX files",
    "author": "brockmann",
    "version": (0, 1, 0),
    "blender": (2, 80, 0),
    "location": "File > Import-Export",
    "description": "Batch Export Objects in Selection to FBX",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Import-Export"}


import bpy
import os

from bpy_extras.io_utils import ExportHelper

from bpy.props import (BoolProperty,
                       FloatProperty,
                       StringProperty,
                       EnumProperty,
                       CollectionProperty
                       )


class Batch_FBX_Export(bpy.types.Operator, ExportHelper):
    """Batch export objects to fbx files"""
    bl_idname = "export_scene.batch_fbx"
    bl_label = "Batch export FBX"
    bl_options = {'PRESET', 'UNDO'}

    # ExportHelper mixin class uses this
    filename_ext = ".fbx"

    filter_glob = StringProperty(
            default="*.fbx",
            options={'HIDDEN'},
            )

    # List of operator properties, the attributes will be assigned
    # to the class instance from the operator setting before calling.

    # context group
    use_selection_setting: BoolProperty(
            name="Selection Only",
            description="Export selected objects only",
            default=True,
            )

    use_mesh_modifiers_setting: BoolProperty(
            name="Apply Modifiers",
            description="Apply modifiers (preview resolution)",
            default=True,
            )
    axis_forward_setting: EnumProperty(
            name="Forward",
            items=(('X', "X Forward", ""),
                   ('Y', "Y Forward", ""),
                   ('Z', "Z Forward", ""),
                   ('-X', "-X Forward", ""),
                   ('-Y', "-Y Forward", ""),
                   ('-Z', "-Z Forward", ""),
                   ),
            default='-Z',
            )
    axis_up_setting: EnumProperty(
            name="Up",
            items=(('X', "X Up", ""),
                   ('Y', "Y Up", ""),
                   ('Z', "Z Up", ""),
                   ('-X', "-X Up", ""),
                   ('-Y', "-Y Up", ""),
                   ('-Z', "-Z Up", ""),
                   ),
            default='Y',
            )
    global_scale_setting: FloatProperty(
            name="Scale",
            min=0.01, max=1000.0,
            default=1.0,
            )

    def execute(self, context):                

        # get the folder
        folder_path = os.path.dirname(self.filepath)

        # get objects selected in the viewport
        viewport_selection = context.selected_objects

        # get export objects
        obj_export_list = viewport_selection
        if self.use_selection_setting == False:
            obj_export_list = [i for i in context.scene.objects]

        # deselect all objects
        bpy.ops.object.select_all(action='DESELECT')

        for item in obj_export_list:
            item.select_set(True)
            if item.type == 'MESH':
                file_path = os.path.join(folder_path, "{}.fbx".format(item.name))

                # FBX settings
                bpy.ops.export_scene.fbx(
                        filepath=file_path, 
                        use_selection=self.use_selection_setting, 
                        use_active_collection=False, 
                        global_scale=self.global_scale_setting, 
                        apply_unit_scale=True, 
                        apply_scale_options='FBX_SCALE_NONE', 
                        bake_space_transform=False, 
                        object_types={'EMPTY', 'CAMERA', 'LIGHT', 'ARMATURE', 'MESH', 'OTHER'}, 
                        use_mesh_modifiers=self.use_mesh_modifiers_setting, 
                        use_mesh_modifiers_render=True, 
                        mesh_smooth_type='OFF', 
                        use_subsurf=False, 
                        use_mesh_edges=False, 
                        use_tspace=False, 
                        use_custom_props=False, 
                        add_leaf_bones=True, primary_bone_axis='Y', 
                        secondary_bone_axis='X', 
                        use_armature_deform_only=False, 
                        armature_nodetype='NULL', 
                        bake_anim=True, 
                        bake_anim_use_all_bones=True, 
                        bake_anim_use_nla_strips=True, 
                        bake_anim_use_all_actions=True, 
                        bake_anim_force_startend_keying=True, 
                        bake_anim_step=1, 
                        bake_anim_simplify_factor=1, 
                        path_mode='AUTO', 
                        embed_textures=False, 
                        batch_mode='OFF', 
                        use_batch_own_dir=True, 
                        use_metadata=True, 
                        axis_forward=self.axis_forward_setting, 
                        axis_up=self.axis_up_setting
                        )

            item.select_set(False)

        # restore viewport selection
        for ob in viewport_selection:
            ob.select_set(True)

        return {'FINISHED'}


# Only needed if you want to add into a dynamic menu
def menu_func_import(self, context):
    self.layout.operator(Batch_FBX_Export.bl_idname, text="FBX Batch Export (.fbx)")


def register():
    bpy.utils.register_class(Batch_FBX_Export)
    bpy.types.TOPBAR_MT_file_export.append(menu_func_import)


def unregister():
    bpy.utils.unregister_class(Batch_FBX_Export)
    bpy.types.TOPBAR_MT_file_export.remove(menu_func_import)


if __name__ == "__main__":
    register()

    # test call
    #bpy.ops.export_scene.batch_fbx('INVOKE_DEFAULT')

I added all possible arguments of the fbx operator (line 125), you can change them or add new properties to the operator, wasn't sure.

$\endgroup$
6
  • $\begingroup$ Works like a charm. Out of the two answers this works the intended way, as it is easily customizable. Thank you for your assistance! You got the bounty! $\endgroup$ – 505Legion Mar 16 '20 at 17:04
  • $\begingroup$ Only question I have is how can I delete export options? I didn't get the desired effect on the first try and after altering a thing or two I ran the script again and now I have 2 of them. $\endgroup$ – 505Legion Mar 16 '20 at 17:14
  • 1
    $\begingroup$ Cool @505Legion Easy, no problem to edit the script, just let me know. Install it like any other add-on instead of running the script in the text editor (because it is an add-on actually). Save the script as eg. io_export_batch_fbx.py using a text editor of your choice, in Blender User Prefs > Addons > Install. What else? $\endgroup$ – brockmann Mar 16 '20 at 17:38
  • $\begingroup$ Here is a screenshot of my problem, as it might have been a bit misleading. imgur.com/fiRjQ3u As you can see I have 2 individual FBX Batch Export, an FBX Batch Export 2 and an FBX Batch Export for Unity. At first I expected that running the script will override the previous one, instead created a clone, so to make sure I'm using the altered one, I ran it again (added the number "2" to its name). After that I ran it with the actual name I needed it with ("for Unity"). I want to keep the latest one, but would like to get rid of the rest. How can I do so? $\endgroup$ – 505Legion Mar 16 '20 at 17:42
  • 1
    $\begingroup$ Seems like your solution (saving and using it as add-on) worked just fine, I just had to restart Blender facepalm. Now all the duplicate options are gone. I have no other complaints about the script. Thank you once more. $\endgroup$ – 505Legion Mar 16 '20 at 17:57
4
$\begingroup$
  1. Save your blend file in the directory you want the objects to be in.
  2. Head over to the Scripting workspace
  3. In the editor window: Templates -> Python -> Batch Export
  4. Select all objects that you want to export
  5. Run the script with the Play button
$\endgroup$
2
  • 1
    $\begingroup$ Smart! (no more to say but needed 15 chars to add the comment) $\endgroup$ – lemon Mar 15 '20 at 17:18
  • $\begingroup$ It worked, but sadly it appears I gave you too few information on the way how these are supposed to be exported (as later commented) and the other answer gave a better result as that is greatly customizable. Your answer is still a viable option and I'll keep it in mind. Thank you very much! $\endgroup$ – 505Legion Mar 16 '20 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.