Modifying the material order on a mesh inside of UE through python?

Hi all! Has anyone successfully managed to run python code the re-orders the materials on a mesh from inside of Unreal? (The element 0, element 1, etc) Example image:UE Material Order - Album on Imgur

I need to update several models that I’ve imported that have materials animated in the sequencer. The sequencer uses the material id/order to determine what’s being animated. If that material order changes upon re-importing a model, it will screw up the animation in the sequencer, as those keyframes are no longer applying to the right material.

If found this older python code here, but it doesn’t seem to work with the latest version of UE?


#In Unreal Engine 4, you can use the StaticMesh class and its Materials array to manipulate materials. Here's a basic example to swap the order of the first two materials on a static mesh using Python in Unreal Engine 4.
import unreal
# Get the selected static mesh actor
selected_actor = unreal.EditorUtilityLibrary.get_selected_actor()
if selected_actor and selected_actor.is_a(unreal.StaticMeshActor):
static_mesh_actor = unreal.StaticMeshActor(selected_actor)
# Get the static mesh component
static_mesh_component = static_mesh_actor.get_static_mesh_component()
# Get the materials array
materials = static_mesh_component.get_materials()
# Check if there are at least two materials
if len(materials) >= 2:
# Swap the first two materials
materials[0], materials[1] = materials[1], materials[0]
# Set the updated materials array
static_mesh_component.set_materials(materials)
print("Material order swapped successfully.")
else:
print("Not enough materials on the static mesh.")
else:
print("Please select a Static Mesh Actor.")

Other examples i’ve found simply switch materials (but keep same IDs,) which also wont work because it modifys the look of the geo. Anyone ever managed to pull this off?