Converts ESP plugins to ESM and sets missing persistence flags.
A problem:
- All references in ESP plugins are treated as persistent.

A solution:
- Enable the ESM flag in the file header.

Right? Not exactly.

To avoid marking all actors as persistent, Skyrim utilizes the Persistent Location field, making an actor persistent only when player is present in its location. This field allows NPCs to travel between cells, when you are visiting their town. When you leave the town, its citizens lose their persistence, their packages no longer work, and all actors stay trapped in their current cells until you visit the town again.

Many mod makers never used this flag, and never noticed its absence because they released their mods as ESP. Their packages worked as expected because all actors were always loaded and processed in background. Enabling the ESM flag makes all unflagged actors non-persistent, breaking schedules of all actors in the mod.

To fix this properly, mod author should walk through their NPCs and assign correct Persistent Location. Unfortunately, this is not always an option, so we retreat to a backup solution - mark all eligible actors as persistent, like they were in ESP.

What this script does

1. Sets the ESM flag.

2. Mimics behavior of the Creation Kit, marking as persistent references the CK would mark, too. I used DavidJCobb's post on the Skyrim RE Discord server as a reference:

Per reverse-engineering, REFRs need to be persistent (and will be flagged as such by the CK) if they meet any of the following conditions:

The base form is any of: PrisonMarker; DivineMarker; TempleMarker; MapMarker; HorseMarker; MultiBoundMarker; RoomMarker; XMarkerHeading; XMarker.
The base form is a TextureSet (i.e. the ref is a decal emitter).
The base form is an Activator with a water type set (i.e. the ref is a water activator).
The base form is a Door, and the base form has one or more random destinations set.
The base form is either of the DLZM or DCZM Default Objects (i.e. DragonMarker or DragonMarkerCrashStrip).
The ref's location (per extra-data) is the PLOC Default Object (i.e. the PersistAll location).
The base form is a Light, and the ref is set to Never Fade.
The ref is being used (i.e. Use Info) by at least one form.

Mods, created completely in the Creation Kit, should not be affected by this function. This feature is intended for broken mods, mods, created in xEdit, mods after incorrect modifications.

3. Scans packages of non-persistent actors without assigned Persistent Location and tries to determine if an actor must be persistent for its packages to work correctly.


- Copy the .pas script to xEdit Location\Edit Scripts
- Load your plugins in xEdit.
- Select plugins you would like to ESMify.
- In the right-click menu, select "Apply Script..."
- Find in the list Skyrim ESMifier and press OK.

You should know

- "Always save ONAM" must be enabled in xEdit options.
- This script does not process bi-directional location references. Resave plugin in the CK to rebuild them.
- This script was made with assumption that Default Objects DLZM, DCZM, and PLOC were not changed. This is true in 99.999% cases. If you ever find a mod, changing these objects, do let me know, I'm curious what it does.

Git repository