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 8b02121ae9 - Show all commits

View File

@ -2536,7 +2536,6 @@ def fbx_data_from_scene(scene, depsgraph, settings):
if ob_obj.type not in BLENDER_OBJECT_TYPES_MESHLIKE: if ob_obj.type not in BLENDER_OBJECT_TYPES_MESHLIKE:
continue continue
ob = ob_obj.bdata ob = ob_obj.bdata
use_org_data = True
org_ob_obj = None org_ob_obj = None
# Do not want to systematically recreate a new mesh for dupliobject instances, kind of break purpose of those. # Do not want to systematically recreate a new mesh for dupliobject instances, kind of break purpose of those.
@ -2546,17 +2545,25 @@ def fbx_data_from_scene(scene, depsgraph, settings):
data_meshes[ob_obj] = data_meshes[org_ob_obj] data_meshes[ob_obj] = data_meshes[org_ob_obj]
continue continue
is_ob_material = any(ms.link == 'OBJECT' for ms in ob.material_slots) # 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.
# 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 a mesh has Object-linked materials. This is to do with how materials are currently mapped to FBX.
# 3) A non-mesh needs to be 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.
# 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_convert = ob.type in BLENDER_OTHER_OBJECT_TYPES
do_evaluate = do_convert and settings.use_mesh_modifiers
if settings.use_mesh_modifiers or settings.use_triangles or ob.type in BLENDER_OTHER_OBJECT_TYPES or is_ob_material: # If the Object is a mesh, and we're applying modifiers, check if there are actually any modifiers to apply.
# We cannot use default mesh in that case, or material would not be the right ones... # If there are then the mesh will need to be evaluated, and we may need to make some temporary changes before
use_org_data = not (is_ob_material or ob.type in BLENDER_OTHER_OBJECT_TYPES) # evaluating the mesh.
apply_modifiers = False
backup_pose_positions = [] backup_pose_positions = []
tmp_mods = [] tmp_mods = []
if use_org_data and ob.type == 'MESH': if ob.type == 'MESH' and settings.use_mesh_modifiers:
if settings.use_triangles:
use_org_data = False
# No need to create a new mesh in this case, if no modifier is active! # No need to create a new mesh in this case, if no modifier is active!
last_subsurf = None last_subsurf = None
for mod in ob.modifiers: for mod in ob.modifiers:
@ -2581,27 +2588,23 @@ def fbx_data_from_scene(scene, depsgraph, settings):
# found applicable subsurf modifier. # found applicable subsurf modifier.
if settings.use_subsurf and mod.type == 'SUBSURF' and mod.subdivision_type == 'CATMULL_CLARK': if settings.use_subsurf and mod.type == 'SUBSURF' and mod.subdivision_type == 'CATMULL_CLARK':
if last_subsurf: if last_subsurf:
use_org_data = False do_evaluate = True
apply_modifiers = True
last_subsurf = mod last_subsurf = mod
else: else:
use_org_data = False do_evaluate = True
apply_modifiers = True
if settings.use_subsurf and last_subsurf: if settings.use_subsurf and last_subsurf:
# XXX: When exporting with subsurf information temporarily disable # XXX: When exporting with subsurf information temporarily disable
# the last subsurf modifier. # the last subsurf modifier.
tmp_mods.append((last_subsurf, last_subsurf.show_render, last_subsurf.show_viewport)) tmp_mods.append((last_subsurf, last_subsurf.show_render, last_subsurf.show_viewport))
last_subsurf.show_render = False
last_subsurf.show_viewport = False if do_evaluate:
if not use_org_data:
# If modifiers has been altered need to update dependency graph. # If modifiers has been altered need to update dependency graph.
if backup_pose_positions or tmp_mods: if backup_pose_positions or tmp_mods:
depsgraph.update() depsgraph.update()
if apply_modifiers or ob.type in BLENDER_OTHER_OBJECT_TYPES:
ob_to_convert = ob.evaluated_get(depsgraph) ob_to_convert = ob.evaluated_get(depsgraph)
# NOTE: The dependency graph might be re-evaluating multiple times, which could # NOTE: The dependency graph might be re-evaluating multiple times, which could
# potentially free the mesh created early on. So we put those meshes to bmain and # potentially free the mesh created early on. So we put those meshes to bmain and
# free them afterwards. Not ideal but ensures correct ownerwhip. # free them afterwards. Not ideal but ensures correct ownership.
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)
@ -2613,11 +2616,19 @@ def fbx_data_from_scene(scene, depsgraph, settings):
if orig_mats != eval_mats: if orig_mats != eval_mats:
# Override the default behaviour of getting materials from ob_obj.bdata.material_slots. # Override the default behaviour of getting materials from ob_obj.bdata.material_slots.
ob_obj.override_materials = eval_mats ob_obj.override_materials = eval_mats
else: elif do_convert:
# bpy.data.meshes.new_from_object always removes shape keys (see #104714), so create a copy of the tmp_me = bpy.data.meshes.new_from_object(ob, preserve_all_data_layers=True, depsgraph=depsgraph)
elif do_copy:
# bpy.data.meshes.new_from_object always removes shape keys (see #104714), so create a direct copy of the
# mesh instead. # mesh instead.
tmp_me = ob.data.copy() tmp_me = ob.data.copy()
else:
tmp_me = None
if tmp_me is None:
# Use the original data of this Object.
data_meshes[ob_obj] = (get_blenderID_key(ob.data), ob.data, False)
else:
# Triangulate the mesh if requested # Triangulate the mesh if requested
if settings.use_triangles: if settings.use_triangles:
import bmesh import bmesh
@ -2626,8 +2637,9 @@ def fbx_data_from_scene(scene, depsgraph, settings):
bmesh.ops.triangulate(bm, faces=bm.faces) bmesh.ops.triangulate(bm, faces=bm.faces)
bm.to_mesh(tmp_me) bm.to_mesh(tmp_me)
bm.free() bm.free()
# A temporary mesh was created for this Object, which should be deleted once the export is complete.
data_meshes[ob_obj] = (get_blenderID_key(tmp_me), tmp_me, True) data_meshes[ob_obj] = (get_blenderID_key(tmp_me), tmp_me, True)
# Change armatures back. # Change armatures back.
for armature, pose_position in backup_pose_positions: for armature, pose_position in backup_pose_positions:
print((armature, pose_position)) print((armature, pose_position))
@ -2639,8 +2651,6 @@ def fbx_data_from_scene(scene, depsgraph, settings):
mod.show_viewport = show_viewport mod.show_viewport = show_viewport
if backup_pose_positions or tmp_mods: if backup_pose_positions or tmp_mods:
depsgraph.update() depsgraph.update()
if use_org_data:
data_meshes[ob_obj] = (get_blenderID_key(ob.data), ob.data, False)
# In case "real" source object of that dupli did not yet still existed in data_meshes, create it now! # In case "real" source object of that dupli did not yet still existed in data_meshes, create it now!
if org_ob_obj is not None: if org_ob_obj is not None: