KaiClavier

ExportAsFbxUponSave.py

May 15th, 2020 (edited)
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #This script is meant to replace the save function (ctrl + s) in Blender
  2. #Open up the add-ons tab within Blender prefs and import this script.
  3. #Any updates to this add-on will require it to be uninstalled and installed again.
  4. #Make sure to un-map regular saving. Right-click save in the file menu and remove the mapping.
  5. #It will export an FBX that will have optimal settings for Unity, in the same folder as the .blend file.
  6.  
  7. #todo list:
  8. #This crashes when you save a file for the first time, since there's no save path to save to... need to change this!!!
  9. #Make SURE these settings are optimal.
  10. #Option to convert periods to underscores in transform names, for more compatibility with old .blend files and Unity animations/avatar definitions.
  11. #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.
  12. #Get this command to appear in the file menu, under Save and Save As.
  13. #It only works in object mode...
  14.  
  15.  
  16. bl_info = {
  17.     "name": "Auto FBX Export",
  18.     "blender": (2, 80, 0),
  19.     "category": "Object",
  20. }
  21.  
  22. import bpy
  23. import os
  24. import shutil
  25.  
  26. class SaveExportFBX(bpy.types.Operator):
  27.     bl_idname = "object.save_and_export_fbx"
  28.     bl_label = "Save and Export FBX"
  29.     bl_options = {'REGISTER'}
  30.  
  31.     def execute(self, context):
  32.         #This gets excecuted vvv
  33.  
  34.         #Do a normal save
  35.         bpy.ops.wm.save_mainfile()
  36.  
  37.         #Get filepath
  38.         filepath = bpy.data.filepath
  39.         directory = os.path.dirname(filepath)
  40.         fileName = bpy.path.basename(bpy.context.blend_data.filepath)
  41.         fileName = os.path.splitext(fileName)[0]
  42.         fullPath = os.path.join(directory, fileName + ".fbx")
  43.            
  44.         #Export as FBX to this path with settings.
  45.         #Reference to these settings: https://docs.blender.org/api/current/bpy.ops.export_scene.html?highlight=fbx#bpy.ops.export_scene.fbx
  46.         bpy.ops.export_scene.fbx(
  47.                 filepath=fullPath,
  48.                 check_existing=False,
  49.                 filter_glob="*.fbx",
  50.                 use_selection=False,
  51.                 use_active_collection=False,
  52.                 global_scale=1.0,
  53.                 apply_unit_scale=True,
  54.                 apply_scale_options='FBX_SCALE_UNITS',
  55.                 bake_space_transform=False,
  56.                 object_types={'ARMATURE', 'MESH'},
  57.                 use_mesh_modifiers=False,
  58.                 use_mesh_modifiers_render=False,
  59.                 mesh_smooth_type='OFF',
  60.                 use_subsurf=True,
  61.                 use_mesh_edges=False,
  62.                 use_tspace=True,
  63.                 use_custom_props=False,
  64.                 add_leaf_bones=False,
  65.                 primary_bone_axis='Y',
  66.                 secondary_bone_axis='X',
  67.                 use_armature_deform_only=False,
  68.                 armature_nodetype='NULL',
  69.                 bake_anim=True,
  70.                 bake_anim_use_all_bones=True,
  71.                 bake_anim_use_nla_strips=False,
  72.                 bake_anim_use_all_actions=True,
  73.                 bake_anim_force_startend_keying=False,
  74.                 bake_anim_step=1.0,
  75.                 bake_anim_simplify_factor=1.0,
  76.                 path_mode='AUTO',
  77.                 embed_textures=False,
  78.                 batch_mode='OFF',
  79.                 use_batch_own_dir=True,
  80.                 use_metadata=True,
  81.                 axis_forward='Z',
  82.                 axis_up='Y')
  83.                
  84.         print("done")
  85.         #put a message in blender so you know when the FBX is done.
  86.         #Is it possible to get this in green isntead of blue?
  87.         self.report({'INFO'}, "Exported FBX Automatically!")
  88.         return {'FINISHED'}
  89.  
  90. #This is supposed to add it to a menu somewhere
  91. def menu_func(self, context):
  92.     self.layout.operator(SaveExportFBX.bl_idname)
  93.  
  94. # stores keymaps
  95. addon_keymaps = []
  96.  
  97. def register():
  98.     bpy.utils.register_class(SaveExportFBX)
  99.  
  100.     # handle the keymap
  101.     wm = bpy.context.window_manager
  102.     km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
  103.  
  104.     kmi = km.keymap_items.new(SaveExportFBX.bl_idname, 'S', 'PRESS', ctrl=True, shift=False)
  105.  
  106.     addon_keymaps.append((km, kmi))
  107.  
  108.  
  109. def unregister():
  110.     bpy.utils.unregister_class(SaveExportFBX)
  111.  
  112.     # handle the keymap
  113.     for km, kmi in addon_keymaps:
  114.         km.keymap_items.remove(kmi)
  115.     addon_keymaps.clear()
  116.  
  117.  
  118. if __name__ == "__main__":
  119.     register()
RAW Paste Data