Help with moving PetFrame to top of PlayerFrame

I’m trying to make an addon that moves the PetFrame to the top of the PlayerFrame. I have the following function:

hooksecurefunc(PetFrame, "SetPoint", function(self)
if petframe then
	return
end
petframe = true
self:ClearAllPoints()
self:SetPoint("TOPLEFT", PlayerFrame, "TOPLEFT", 44, 52)
petframe = nil
end
)

It works fine, until I summon a totem or summon a new pet in combat, which then throws the error “An action was blocked in combat because of taint from MoveStuff - PetFrame:ClearAllPoints()”

Is there anyway around this issue?

PetFrame is protected (https://wow.gamepedia.com/API_Region_IsProtected):

/run print(PetFrame:IsProtected())

So you cannot modify its position during combat lockdown. But do you have to? What happens if you position your PetFrame once while not in combat and then leave it be? Is the position reset when you summon a new pet in combat?

Here’s a quick adaptation from CTMod if you only want the ten buttons and not the entire frame.

PetActionButton1:ClearAllPoints()
PetActionButton1:SetPoint("TOPLEFT", PlayerFrame, "TOPLEFT", 44, 0)
for i=1, 10 do _G["PetActionButton"..i]:SetParent(PlayerFrame) end
RegisterStateDriver(PetActionBarFrame,"visibility", "hide")

Yes, it gets reset when summoning a new pet or using a totem in combat

That’s why CTMod moves PetActionButton1 instead of PetActionBarFrame.

(And then either hides PetActionBarFrame, or makes it non-responsive to clicks, so that it doesn’t become a “dead space” in the user interface.)