Guest User

Export Blender Animation Library with Linked Armature to FBX

a guest
May 3rd, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # animation-export.py
  2. #
  3. # Purpose:
  4. #   When dealing with larger animation projects such as characters for
  5. #   video games or machinima, a lot of Actions (Blender animation clips)
  6. #   will result. It is advantageous to store these in separate files.
  7. #  
  8. #   Blender supports this scenario by allowing .blend files to "link"
  9. #   in objects from each other. However, as of now, the FBX exporter
  10. #   will not correctly export animation tracks in separate .blend files.
  11. #
  12. #   up to 2.72:   works if not using a proxy for the armature
  13. #   2.73:         nothing works at all
  14. #   2.74 to 2.77: only works with proxy but animations are detached
  15. #   2.78:         only works with proxy, animations attached to wrong object
  16. #
  17. #   In short, until the rewrite of the proxy system (may happen in 2.8),
  18. #   exporting animations on linked Armatures only works in Blender 2.72
  19. #   and without proxies, but the old FBX exporter has its own problems.
  20. #
  21. #   The only viable solution, then, is to:
  22. #     1) open the master file
  23. #     2) append all animation tracks from a separate .blend file to it
  24. #     3) export the master file
  25. #     4) exit without saving
  26. #
  27. #   This script will do just that. Since exporting the master meshes
  28. #   multiple times would be a giant waste of space, it only exports
  29. #   a single mesh (the mesh must be a part of the Armature, though).
  30. #  
  31. # Usage:
  32. #   Invoke Blender like this:
  33. #
  34. #   blender master.blend --python animation-export.py -- \
  35. #           animations.blend wildcard* target.fbx
  36. #
  37. #   - The first argument (master.blend) is the .blend file containing
  38. #     your character without animations
  39. #
  40. #   - The second argument (--python animation-export.py) runs this script
  41. #
  42. #   - The third argument (--) is a separator behind which the script
  43. #     arguments begin. You can add other Blender arguments before this.
  44. #
  45. #   - The fourth argument (animations.blend) specifies the .blend file
  46. #     containing the animation tracks you wish to export
  47. #
  48. #   - The fifth argument (wildcard*) is a wildcard string specifying
  49. #     which animations to append (this is useful if you have animations
  50. #     for multiple characters in animations.blend and want to append
  51. #     only the animations for the specific character you're exporting).
  52. #
  53. #   - The seventh argument (target.fbx) is the output file into which
  54. #     the animations will be exported
  55. #
  56. import bpy
  57. import sys
  58. import fnmatch
  59. import os
  60.  
  61. def append_actions_from_file(path, wildcard):
  62.     """
  63.    Builds a list of the actions present in a blend file
  64.    """
  65.    
  66.     # https://www.blender.org/api/blender_python_api_2_77_0/bpy.types.BlendDataLibraries.html
  67.     with bpy.data.libraries.load(path) as (data_from, data_to):
  68.         data_to.actions = [name for name in data_from.actions if fnmatch.fnmatch(name, wildcard)]
  69.  
  70. # ----------------------------------------------------------------------
  71.  
  72. print("animation-export.py running...")
  73.  
  74. cwd = os.getcwd()
  75. print("base path: " + cwd)
  76.  
  77. argv = sys.argv
  78. argv = argv[argv.index("--") + 1:]  # get all args after "--"
  79.  
  80. # Animation library (.blend file) we want to append from
  81. animlib = argv[0]
  82. animlib = os.path.join(cwd, animlib)
  83. print("animation library: " + animlib)
  84.  
  85. # Wildcard of the actions we need to append
  86. wildcard = argv[1]
  87. print("actions to export: " + wildcard)
  88.  
  89. # Target path
  90. fbxpath = argv[2]
  91. print("fbx target path: " + fbxpath)
  92.  
  93. # Append the actions
  94. append_actions_from_file(animlib, wildcard)
  95.  
  96. # ?? Select all Armatures in the scene
  97. # ?? Select mesh specified on command line
  98.  
  99. # Export to FBX
  100. bpy.ops.export_scene.fbx(
  101.     filepath=fbxpath,
  102.     check_existing=False,
  103.     axis_forward='-Z',
  104.     axis_up='Y',
  105.     version='BIN7400',
  106.     use_selection=False,
  107.     global_scale=1.0,
  108.     apply_unit_scale=False,
  109.     bake_space_transform=False,
  110.     object_types={'ARMATURE', 'EMPTY', 'MESH'},
  111.     use_mesh_modifiers=True,
  112.     mesh_smooth_type='OFF',
  113.     use_mesh_edges=False,
  114.     use_tspace=True,
  115.     use_custom_props=False,
  116.     add_leaf_bones=False,
  117.     primary_bone_axis='Y',
  118.     secondary_bone_axis='X',
  119.     use_armature_deform_only=True,
  120.     bake_anim=True,
  121.     bake_anim_use_all_bones=True,
  122.     bake_anim_use_nla_strips=False,
  123.     bake_anim_use_all_actions=True,
  124.     bake_anim_force_startend_keying=True,
  125.     bake_anim_step=1.0,
  126.     bake_anim_simplify_factor=0.0,
  127.     use_anim=True,
  128.     use_anim_action_all=True,
  129.     use_default_take=False,
  130.     use_anim_optimize=False,
  131.     path_mode='AUTO',
  132.     embed_textures=False,
  133.     batch_mode='OFF',
  134.     use_metadata=True
  135. )
  136.  
  137. # Quit. We do not want to risk keeping the window open,
  138. # which might end up making the user save our patchwork file
  139. #
  140. quit()
Add Comment
Please, Sign In to add comment