Fix #104714: Missing shape keys in FBX export when original mesh data cannot be used #104890

Merged
Thomas Barlow merged 7 commits from Mysteryem/blender-addons:fbx_fix_triangulate_removing_shapes into main 2023-09-19 02:26:19 +02:00
Showing only changes of commit 49e6617861 - Show all commits

View File

@ -2548,19 +2548,22 @@ def fbx_data_from_scene(scene, depsgraph, settings):
# There are 4 different cases for what we need to do with the original data of each Object: # There are 4 different cases for what we need to do with the original data of each Object:
# 1) The original data can be used without changes. # 1) The original data can be used without changes.
# 2) A copy of the original data needs to be made. # 2) A copy of the original data needs to be made.
# - If a mesh needs to be modified upon export, e.g. it needs triangulating. # - If an export option modifies the data, e.g. Triangulate Faces is enabled.
# - If a mesh has Object-linked materials. This is to do with how materials are currently mapped to FBX. # - If the Object has Object-linked materials. This is because our current mapping of materials to FBX requires
# 3) A non-mesh needs to be converted to a mesh. # that multiple Objects sharing a single mesh must have the same materials.
# 3) The Object needs to be converted to a mesh.
# - All mesh-like Objects that are not meshes need to be converted to a mesh in order to be exported.
# 4) The Object needs to be evaluated and then converted to a mesh. # 4) The Object needs to be evaluated and then converted to a mesh.
# - Whenever use_mesh_modifiers is enabled and either there are modifiers to apply or the Object is not a mesh. # - Whenever use_mesh_modifiers is enabled and either there are modifiers to apply or the Object needs to be
# converted to a mesh.
# If multiple cases apply to an Object, then only the last applicable case is relevant. # If multiple cases apply to an Object, then only the last applicable case is relevant.
do_copy = any(ms.link == 'OBJECT' for ms in ob.material_slots) or (ob.type == 'MESH' and settings.use_triangles) do_copy = any(ms.link == 'OBJECT' for ms in ob.material_slots) or settings.use_triangles
do_convert = ob.type in BLENDER_OTHER_OBJECT_TYPES do_convert = ob.type in BLENDER_OTHER_OBJECT_TYPES
do_evaluate = do_convert and settings.use_mesh_modifiers do_evaluate = do_convert and settings.use_mesh_modifiers
# If the Object is a mesh, and we're applying modifiers, check if there are actually any modifiers to apply. # If the Object is a mesh, and we're applying modifiers, check if there are actually any modifiers to apply.
# If there are then the mesh will need to be evaluated, and we may need to make some temporary changes before # If there are then the mesh will need to be evaluated, and we may need to make some temporary changes to the
# evaluating the mesh. # modifiers or scene before the mesh is evaluated.
backup_pose_positions = [] backup_pose_positions = []
tmp_mods = [] tmp_mods = []
if ob.type == 'MESH' and settings.use_mesh_modifiers: if ob.type == 'MESH' and settings.use_mesh_modifiers:
@ -2608,8 +2611,8 @@ def fbx_data_from_scene(scene, depsgraph, settings):
tmp_me = bpy.data.meshes.new_from_object( tmp_me = bpy.data.meshes.new_from_object(
ob_to_convert, preserve_all_data_layers=True, depsgraph=depsgraph) ob_to_convert, preserve_all_data_layers=True, depsgraph=depsgraph)
# Usually the materials of the evaluated object will be the same, but modifiers, such as Geometry # Usually the materials of the evaluated object will be the same, but modifiers, such as Geometry Nodes,
# Nodes, can change the materials. # can change the materials.
orig_mats = tuple(slot.material for slot in ob.material_slots) orig_mats = tuple(slot.material for slot in ob.material_slots)
eval_mats = tuple(slot.material.original if slot.material else None eval_mats = tuple(slot.material.original if slot.material else None
for slot in ob_to_convert.material_slots) for slot in ob_to_convert.material_slots)
@ -2619,8 +2622,7 @@ def fbx_data_from_scene(scene, depsgraph, settings):
elif do_convert: elif do_convert:
tmp_me = bpy.data.meshes.new_from_object(ob, preserve_all_data_layers=True, depsgraph=depsgraph) tmp_me = bpy.data.meshes.new_from_object(ob, preserve_all_data_layers=True, depsgraph=depsgraph)
elif do_copy: elif do_copy:
# bpy.data.meshes.new_from_object always removes shape keys (see #104714), so create a direct copy of the # bpy.data.meshes.new_from_object removes shape keys (see #104714), so create a copy of the mesh instead.
# mesh instead.
tmp_me = ob.data.copy() tmp_me = ob.data.copy()
else: else:
tmp_me = None tmp_me = None