I have a button I am attempting to use events for a Mouse onClick event. The user clicks the button and it should swap between the text of Forward or Reverse. If it is Forward when they click, the button text should toggle to Reverse and vice versa. However, I cannot seem to get my python code to do that. It works Reverse > Forward but not from Forward > Reverse.
Note that this is the simple input Button from Perspective Components.
Here is my code, it should just be a simple if else statement but it only seems to work in one direction and I am uncertain why:
if system.tag.read(self.view.custom.Directionpath) == "false":
system.tag.write(self.view.custom.Directionpath,"true")
else:
system.tag.write(self.view.custom.Directionpath,"false")
"true" and "false" are defining String literals, not comparing booleans. Also, you’re reading a tag, which returns a QualifiedValue, not an actual value.
Try:
if system.tag.read(self.view.custom.Directionpath).value == False:
system.tag.write(self.view.custom.Directionpath, True)
else:
system.tag.write(self.view.custom.Directionpath, False)
But that’s also a pretty inefficient way to do things - if it’s a boolean and you want to invert it:
system.tag.write(self.view.custom.Directionpath, not system.tag.read(self.view.custom.Directionpath).value)
Using either set of code you sent back yields the same results as before. As long as the value is false (showing Reverse), I can toggle it to True (showing Forward). But once its set true, it will not toggle to false.
It is definitely a boolean, I am only trying to change this value here
and this one:
It appears as though the scrip does not read the else part of the statement. I can swap the
if system.tag.read(self.view.custom.Directionpath) == "false":
system.tag.write(self.view.custom.Directionpath,"true")
else:
system.tag.write(self.view.custom.Directionpath,"false")
to
if system.tag.read(self.view.custom.Directionpath) == "true":
system.tag.write(self.view.custom.Directionpath,"false")
else:
system.tag.write(self.view.custom.Directionpath,"true")
system.tag.read() returns a Qualified Value. You have to specify that you want the value
if not system.tag.read(self.view.custom.Directionpath).value:
system.tag.write(self.view.custom.Directionpath,True)
else:
system.tag.write(self.view.custom.Directionpath,False)
Also, you should consider moving to readBlocking and writeBlocking.