I have a custom property with the following change script on it.
if self.custom.Units == 'Each':
# Round current value to nearest integer value.
currentValue.value = round(currentValue.value)
else:
# User is permitted to enter decimal units.
currentValue.value = currentValue.value
I am only ever going to receive integers or decimals and what I would like to do is round the entered decimal value if not a whole integer under certain conditions.
However, my script doesn't appear to work. I cannot tell why it is not working though. Anyone know what I am missing?
The following code also fails to round any entered values so I don't think it is my condition check.
if self.custom.Units == 'Each':
# Round current value to nearest integer value.
currentValue.value = round(currentValue.value)
else:
# User is permitted to enter decimal units.
currentValue.value = round(currentValue.value)
Isn't currentValue read only? I don't think you can assign anything to it in a script. Or at least if you do I don't think it propagates out.
You can set the value by doing a self.props.value = round(currentValue.value). You will need to check the source of the change though so the script doesn't keep triggering itself with a value change, so you should have something along the lines of:
if (origin == 'Browser' or origin == 'Binding') and self.custom.units == 'Each':
self.props.value = round(currentValue.value)
Edit: Whoops, valueInt is vision not perspective and so is event.source.
I adjusted my answer as I realized you probably wanted it to adjust the value coming from the device as well. The check should be if (event.origin =='Browser' or event.origin == 'Binding') and self.custom.units == 'Each'
The script was refusing to work on the custom property binding so I moved it over to the numeric entry field and it works however it locks up for a few seconds sometimes so I am not 100% this is the cleanest way to do this.
Yeah, I thought you were on the value property until after I posted. Mine was intended for the value property not the custom property.
As for the lock up, remove else and everything beyond it. The script is re-triggering itself with that line. The normal value will stay even without you specifying the else.
The bottom line is that you are trying to "fix" the value that is coming from a binding in place. This is an infinite-recursion trap, even if you can patch it with the origin information.
Move your binding to a custom property. Use and expression binding to that custom prop, with desired rounding operation, from the original binding location.
What I ended up doing is adding two custom expression bindings on the numeric entry field. The first binding is the normal value entered by the user. The second value is the rounded value entered by the user. I realize I don't necessarily need the normal custom value but I put it there for readability purposes.
On the format property of the numerical entry field I put a binding:
if({/root.custom.Units}='Each', '0,0', '0,0.##')
If the Units is Each then an Integer format is applied. Alternatively a numerical format is applied.
I will then use logic elsewhere to determine if the normal value or rounded value should be used but the end user will always see what the system is planning on using in any calculations.
Also the "lockup" of the numerical entry field seems to be related to whether or not I have the spinner enabled or not. If the spinner is disabled I have to click on the numeric entry field 2 or 3 times to get back in edit mode (or click on another component and refocus on the numeric entry field). If I have the spinner enabled I can edit it with a single click every time. Since it isn't a big deal overall I just enabled it again (it is enabled by default).