I recently put someone soul back in his body (he was floating around as a ghost, and his body was standing around soulless). Here's a script that's basically the steps I took to do it:
body = dfhack.gui.getSelectedUnit()
histfig = df.historical_figure.find(body.hist_figure_id2)
ghost = df.unit.find(histfig.unit_id)
body.name.first_name = ghost.name.first_name
body.name.nickname = ghost.name.nickname
for a,b in pairs(ghost.name.words) do
body.name.words[a] = b
end
for a,b in pairs(ghost.name.parts_of_speech) do
body.name.parts_of_speech[a] = b
end
body.name.language = ghost.name.language
body.name.unknown = ghost.name.unknown
body.name.has_name = ghost.name.has_name
body.status.current_soul = ghost.status.current_soul
body.status.souls:insert("#", body.status.current_soul)
body.hist_figure_id = ghost.hist_figure_id
body.relations.birth_year = ghost.relations.birth_year
body.relations.birth_time = ghost.relations.birth_time
body.relations.old_year = ghost.relations.old_year
body.relations.old_time = ghost.relations.old_time
body.relations.last_attacker_id = ghost.relations.last_attacker_id
body.flags1.important_historical_figure = ghost.flags1.important_historical_figure
body.flags2.important_historical_figure = ghost.flags2.important_historical_figure
ghost.status.current_soul = nil
ghost.status.souls = {}
ghost.hist_figure_id = -1
ghost.flags1.important_historical_figure = false
ghost.flags2.important_historical_figure = false
ghost.relations.ghost_info = nil
ghost.corpse_parts = {}
ghost.flags1.dead = true
histfig.unit_id = body.id
histfig.flags.ghost = false
histfig.flags[15] = false
I also did some extra stuff to restore his outpost liaison position (which he had lost at death), and he promptly left unhappy. All well and good... now I don't have that stupid nameless peasant and foreign ghost wandering around my fortress.
He was never my dwarf anyway, just an unfortunate diplomat.
This situation arose in the first place from a dwarf being killed, animated as an undead, killed again, then properly resurrected. The upshot of this is that there's a lot of stuff that's lost at animation, not just the soul. I didn't even catch everything - just the stuff that happened to be different for this particular dwarf.