Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #This script is meant to replace the save function (ctrl + s) in Blender
- #Open up the add-ons tab within Blender prefs and import this script.
- #Any updates to this add-on will require it to be uninstalled and installed again.
- #Make sure to un-map regular saving. Right-click save in the file menu and remove the mapping.
- #It will export an FBX that will have optimal settings for Unity, in the same folder as the .blend file.
- #todo list:
- #This crashes when you save a file for the first time, since there's no save path to save to... need to change this!!!
- #Make SURE these settings are optimal.
- #Option to convert periods to underscores in transform names, for more compatibility with old .blend files and Unity animations/avatar definitions.
- #Maybe an option to export multiple objects as different fbx files, but I never used a workflow like this in Unity, I would just grab the generated meshes.
- #Get this command to appear in the file menu, under Save and Save As.
- #It only works in object mode...
- bl_info = {
- "name": "Auto FBX Export",
- "blender": (2, 80, 0),
- "category": "Object",
- }
- import bpy
- import os
- import shutil
- class SaveExportFBX(bpy.types.Operator):
- bl_idname = "object.save_and_export_fbx"
- bl_label = "Save and Export FBX"
- bl_options = {'REGISTER'}
- def execute(self, context):
- #This gets excecuted vvv
- #Do a normal save
- bpy.ops.wm.save_mainfile()
- #Get filepath
- filepath = bpy.data.filepath
- directory = os.path.dirname(filepath)
- fileName = bpy.path.basename(bpy.context.blend_data.filepath)
- fileName = os.path.splitext(fileName)[0]
- fullPath = os.path.join(directory, fileName + ".fbx")
- #Export as FBX to this path with settings.
- #Reference to these settings: https://docs.blender.org/api/current/bpy.ops.export_scene.html?highlight=fbx#bpy.ops.export_scene.fbx
- bpy.ops.export_scene.fbx(
- filepath=fullPath,
- check_existing=False,
- filter_glob="*.fbx",
- use_selection=False,
- use_active_collection=False,
- global_scale=1.0,
- apply_unit_scale=True,
- apply_scale_options='FBX_SCALE_UNITS',
- bake_space_transform=False,
- object_types={'ARMATURE', 'MESH'},
- use_mesh_modifiers=False,
- use_mesh_modifiers_render=False,
- mesh_smooth_type='OFF',
- use_subsurf=True,
- use_mesh_edges=False,
- use_tspace=True,
- use_custom_props=False,
- add_leaf_bones=False,
- 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=False,
- bake_anim_use_all_actions=True,
- bake_anim_force_startend_keying=False,
- bake_anim_step=1.0,
- bake_anim_simplify_factor=1.0,
- path_mode='AUTO',
- embed_textures=False,
- batch_mode='OFF',
- use_batch_own_dir=True,
- use_metadata=True,
- axis_forward='Z',
- axis_up='Y')
- print("done")
- #put a message in blender so you know when the FBX is done.
- #Is it possible to get this in green isntead of blue?
- self.report({'INFO'}, "Exported FBX Automatically!")
- return {'FINISHED'}
- #This is supposed to add it to a menu somewhere
- def menu_func(self, context):
- self.layout.operator(SaveExportFBX.bl_idname)
- # stores keymaps
- addon_keymaps = []
- def register():
- bpy.utils.register_class(SaveExportFBX)
- # handle the keymap
- wm = bpy.context.window_manager
- km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
- kmi = km.keymap_items.new(SaveExportFBX.bl_idname, 'S', 'PRESS', ctrl=True, shift=False)
- addon_keymaps.append((km, kmi))
- def unregister():
- bpy.utils.unregister_class(SaveExportFBX)
- # handle the keymap
- for km, kmi in addon_keymaps:
- km.keymap_items.remove(kmi)
- addon_keymaps.clear()
- if __name__ == "__main__":
- register()
Add Comment
Please, Sign In to add comment