Categories
General

Bulk-Edit Materials in UE5 and make them TwoSided

In case you run into a scenario where you have multiple faces flipped and do not issue the lowering of the performance, as in my case just for reworking imported assets, you might find this UE5 python script helpful:

import unreal

def make_selected_materials_two_sided(save=True, recompile=True):
    selected = unreal.EditorUtilityLibrary.get_selected_assets()
    if not selected:
        unreal.log_warning("No assets selected.")
        return

    changed_paths = []
    skipped = 0

    for asset in selected:
        if isinstance(asset, unreal.Material):
            mat = asset
            path = mat.get_path_name()

            if mat.get_editor_property("two_sided"):
                unreal.log(f"[OK] Already TwoSided: {path}")
                continue

            # Set Two Sided flag
            mat.set_editor_property("two_sided", True)

            # Recompile / "Apply"
            if recompile:
                try:
                    unreal.MaterialEditingLibrary.recompile_material(mat)
                except Exception as e:
                    unreal.log_warning(f"[WARN] Recompile failed for {path}: {e}")

            unreal.log(f"[CHANGED] TwoSided enabled: {path}")
            changed_paths.append(path)

        elif isinstance(asset, unreal.MaterialInstanceConstant):
            unreal.log_warning(
                f"[SKIP] MaterialInstanceConstant has no TwoSided flag: {asset.get_path_name()} "
                f"(change the parent material instead)"
            )
            skipped += 1
        else:
            unreal.log_warning(f"[SKIP] Asset is not a Material: {asset.get_path_name()}")
            skipped += 1

    # Save modified assets
    if save and changed_paths:
        saved = 0
        for path in changed_paths:
            try:
                if unreal.EditorAssetLibrary.save_asset(path, only_if_is_dirty=False):
                    saved += 1
            except Exception as e:
                unreal.log_warning(f"[WARN] Failed to save asset {path}: {e}")

        unreal.log(f"[SAVE] Saved {saved}/{len(changed_paths)} material assets.")

    unreal.log(f"Done. Modified: {len(changed_paths)}, Skipped: {skipped}")

# Run:
make_selected_materials_two_sided(save=True, recompile=True)