Toggling the setValue on a Button

I am trying to add a functionality to my button so that i can use it to set something to 1, and then to 0 when pressed again. Im trying to use a change script on the Value property for this, but am constantly getting an error. What am i doing wrong

Things like this can be explained by AI, just make sure you tell it what language it is (ie jython or python).

The issue however is your if statement syntax. You're using expression language syntax and not python.

You need to use:

if currentValue.value:
 self.props.setValue = 1
else:
 self.props.setValue = 0
1 Like

If it's a boolean then Nick's code can be reduced to:
return not currentValue.value
(A boolean will use True / False rather than 1 / 0.)

If you are doing this as a transform on a binding then you should avoid the transform and do it all directly in an Expression Binding.
if({tagOrPropertyToBeMonitored}, false, true)
which can be reduced to,
!{tagOrPropertyToBeMonitored}

Note the ! negation at the start.

5 Likes

Don't use a value change script for this.
Read the value from the button script, compute the value you should be writing depending on what it currently is, then write that back.

current_val = path.to.your.prop
if current_val = 0:
    new_val = 1
else:
    new_val = 0
path.to.your.prop = new_val

or, in a more concise manner:

path.to.your.prop = 1 - path.to.your.prop

If it's a boolean, which it probably should be considering it can take only 2 values, then it's even simpler:

path.to.your.prop = not path.to.your.prop
4 Likes