Hi!
To be honest, your code doesn't make a lot of sense, you have variables that get defined and never used and others that you don't define but use as conditions, the way you want to check for the preserveUV option is an invalid flag, etc. Also if you want your HUD to update you need to bind it to an event or a condition.
Something I personally wouldn't do, is that you want to turn your HUD off and on based on the condition that your HUD displays. Because once you remove the HUD, the script it exectues wont run again. This simply doesn't work on its own, you would need to have another script (callback or scriptjob) running in the background that checks for the condition and turns the HUD on and off. I get the appeal of having it turn on when the option is turned on, but the extra effort you need to go through is just not worth it in my opinion.
So here is how I would do it, having a simple HUD that you turn on and off using two commands, which displays the status of "Preserve UVs" in its Data section.
This is the command to turn it on:
import maya.cmds as cmds
def preserveUVDisplay(*args):
preserveUVs = cmds.optionVar(q= 'trsManipsPreserveUvs')
if preserveUVs == 1:
return True
else:
return False
if cmds.headsUpDisplay('PreserveUVsHUD', exists=True):
cmds.headsUpDisplay('PreserveUVsHUD', remove=True)
cmds.headsUpDisplay('PreserveUVsHUD', section=3, block=0, blockSize="large", dfs="large", label="Preserve UVs :", atr = True, command = preserveUVDisplay)
This is the command to turn it off:
import maya.cmds as cmds
if cmds.headsUpDisplay('PreserveUVsHUD', exists=True):
cmds.headsUpDisplay('PreserveUVsHUD', remove=True)
I bound the refresh of the HUD to mayas Idle refresh, so you won't see it change immediately, but after you click somewhere else in the Main Window. I'm not sure if there is a condition or an event that would immediately change it once this options is toggled, I couldn't find one during my research.
I hope it helps!