I’m trying to make an interactive next/previous item selector in Grasshopper, but the use of data recorders bothers me. Will them amassing larger volumes of data not start to slow the whole script down? Would the Null checks and stream filters impact performance if this snippet were to be used in a larger, already heavy script?
I think the buttons are unreliable as they might count when you click and let go. I’ve triedd all sorts of things years ago and ended up using a script.
A coding solution is better than a script cobbled together out of components from various random plug-ins, but out of a desparation of not knowing how to code, here is a solution I came up with:
I’ve employed a number of tricks. The buttons are set to +1/-1 increment/decrement values in the pressed state and are devoid of a value in the unpressed state. The If Value Changed component from Parakeet will read the merged outputs of the buttons and send out a pulse when it registers a change. This occurs on both the press and release of the buttons, so a double pulse for a click. In order to ignore the release pulse, the pulses are ANDed with the list length of the merged button outputs, which reads 1 on downpress and 0 on uplift. For every press of a button, you now have a single pulse coming the the Gate And component.
The Data Recorder component is limited to single output. This creates a hold state of the last change of increment value from the buttons. A Panel acts as memory for the counter. When a pulse passes through the Stream Filter, the sum of the increment/decrement hold state with the counter is pushed through the gate and used by the Set Object Value component from Metahopper to overwrite the memory. The entire point of the pulse is to be able to overwrite memory, and then interrupt the read/write feedback loop set up by the wiring.
I really like opening solution that uses purely native Grasshopper components. I wonder if there isn’t a way of using an annotation dot that you write to via Content Cache as memory. Obviously, a very simple piece of code is the best solution.
It operates exclusively on whole numbers, but it can be changed by changing the conversion from int to float, should there be a need. And it also doesn’t repeat while the button is pressed, like your solution does.
Code
import Grasshopper
from Grasshopper.Kernel.Special import GH_Panel
if x:
for obj in Grasshopper.Instances.ActiveDocument.Objects:
if isinstance(obj, GH_Panel):
if obj.NickName == "Bomb":
obj.SetUserText(str(int(obj.UserText)+x))
if y:
for obj in Grasshopper.Instances.ActiveDocument.Objects:
if isinstance(obj, GH_Panel):
if obj.NickName == "Bomb":
obj.SetUserText(str(int(obj.UserText)+y))
Additional detail
Or like this for probably marginally better performance
import Grasshopper
from Grasshopper.Kernel.Special import GH_Panel
pan = 0
if "saved_value" not in globals():
saved_value = 0
for obj in Grasshopper.Instances.ActiveDocument.Objects:
if isinstance(obj, GH_Panel):
if obj.NickName == "Bob":
pan = obj
if x:
pan.SetUserText(str(int(pan.UserText)+x))
if y:
pan.SetUserText(str(int(pan.UserText)+y))
if save:
saved_value = pan.UserText
if reset:
pan.SetUserText(default)
saved = saved_value