Hello All,
Firstly I reverted from C# to Python to quickly try stuff out, so I don’t mind any advice or help in any language.
I cracked open the Galapagos.GHA to attempt to get some functionality similar to how the solver will update n sliders before allowing the function to be updated. I found the relevent section in the class ‘GalapagosFitnessDelegate’. One issue I might be having is that I am using an input instead of an output, however, Wallacei seems to work this way, so there must be a work around.
Fundamentally I want to be able to update some sliders recalculate something in an external component and get that result.
#SHOULD PRINT 0.351817 and 0.593142
import Grasshopper
import System.Random
random = System.Random(0)
ghDocument = ghenv.Component.OnPingDocument();
ghNumberSliders = ghenv.Component.Params.Input[0].Sources;
for ghNumberSlider in ghNumberSliders:
ghNumberSlider.Slider.RaiseEvents = False;
ghNumberSlider.TickValue = random.Next(0,1000);
ghNumberSlider.ExpireSolution(False);
ghNumberSlider.Slider.RaiseEvents = True;
ghDocument.NewSolution(False)
sources = ghenv.Component.Params.Input[1].Sources
for source in sources :
data = source.VolatileData.AllData(True)
for d in data :
print d
for ghNumberSlider in ghNumberSliders:
ghNumberSlider.Slider.RaiseEvents = False;
ghNumberSlider.TickValue = 0;
ghNumberSlider.ExpireSolution(False);
ghNumberSlider.Slider.RaiseEvents = True;
The GhPython component runs every time one of its inputs is changed.
Presumablty the source code snippet you posted is inside of your GhPython component connected to the two sliders (A & B).
Via the wizardry of ghenv and the Grasshopper library, you’re doing something on each slider (assigning a random number to each?).
But those sliders are connected to your GhPython component, which even if it works will cause it to run again.
So in normal script mode, you’ve created an infinite feedback loop.
Depending on your goal, you could switch the GhPython component to SDK mode, giving you a class with global and class scopes (and you can add an initialiser). If you put your code outside of the RunScript method therein, (in global scope, on the class or in the initialiser) the adjustments of the sliders will then only happen when the component first runs, breaking the infinite loop.
Hi James,
thanks for the response. I ended up finding the solution…namely, you can’t. If you want to do this you need to add add an event that manages the information manually. If you want to get output you need to end the code with ClearData(), CollectData(), ComputeData()
.
An example of how to do this
protected override void AppendAdditionalComponentMenuItems(System.Windows.Forms.ToolStripDropDown menu)
{
System.Windows.Forms.ToolStripMenuItem run = GH_DocumentObject.Menu_AppendItem(menu, "event", myEvent);
}
private void myEvent(object sender, EventArgs e)
{
//DO THE THING
ClearData();
CollectData();
ComputeData(); //Call Solve Instance
}
public override void ExpireSolution(bool recompute)
{
//EMPTY TO PREVENT SolveInstance() FROM COMPUTING INDEPENDENTLY
}
1 Like