algorithmic modeling for Rhino
Hello,
I've recently started reading the Grasshopper SDK documentation and have successfully tested the "Hello World"(Simple Component (C#)) component which reverses a string.
At the moment I would like to continuously output a Point from my component. I'm guessing I can plug a timer to it and using a callback from the timer I can send a value to the output ? Would this be a good approach ? Otherwise, what's the recommended way of doing this ?
Also, I'm slightly confused, because the only way I saw how to output a value from the component was through the SolveInstance method which gets a IGH_DataAccess instance which in turn has a SetData method.
Is there any other way to get access to IGH_DataAccess . I was thinking I could create a reference local to my component and set to point to the instance passed from SolveInstance( if it hasn't been set before), but this feels hacky and I need to pass an input at least once. What would the best practice be ?
Thank you,
George
Tags:
Using a timer is ok, but if your component always needs a timer, you should probably consider adding such a feature directly. This is what Kangaroo did a few months ago and it's a much neater solution.
Do note that Grasshopper wasn't really designed to be a continuous solver. If you want to turn it into one you have to be careful about not blocking the UI thread all the time which turns the software unresponsive.
There's no way to get an IGH_DataAccess instance outside of SolveInstance. The DA instance is created specifically for a single solution and it is not guaranteed to remain valid.
The best way of doing this is to schedule solutions. GH_Document exposes methods for scheduling and you can register callbacks for when a schedule starts. In this callback you can expire your component which will ensure that SolveInstance() will be called again. Schedules that are started during solutions will come into effect when the solution completes. Thus, if your solution takes 1 second to complete and you keep scheduling solutions with a delay of 100 milliseconds, then all solutions will be roughly 1.1 seconds apart. I.e. the schedule time refers to the time in between solutions, not the time from the start of one solution to the start of the next.
--
David Rutten
david@mcneel.com
Hi David,
Thank you very much for the explanations, they are very helpful !
Using a timer is ok, but if your component always needs a timer, you should probably consider adding such a feature directly. This is what Kangaroo did a few months ago and it's a much neater solution.
Sounds exactly like what I'd like to implement.
If you want to turn it into one you have to be careful about not blocking the UI thread all the time which turns the software unresponsive.
That make sense. Does that mean I should create a separate thread to do any computation and return the result to my component when it's done (through a callback or something similar) ?
The best way of doing this is to schedule solutions. GH_Document exposes methods for scheduling and you can register callbacks for when a schedule starts.
I've added a callback for the component being added so I could gain access to the GH_Document, so I think I got the first part:
[code]
public override void AddedToDocument(GH_Document document)
{
base.AddedToDocument(document);
document.ScheduleSolution(1000);
}
[/code]
I haven't found the callback I need to register when a schedule starts. Could you please point me to it and I can carry on from the SDK documentation. I'm guessing I expire the solution from my component like this:
[code]
this.ExpireSolution(true);
[/code]
Having another look through the docs makes me think you probably meant using the method signature of the ScheduleSolution which includes a Schedule degate ?
[code]
public override void AddedToDocument(GH_Document document)
{
base.AddedToDocument(document);
document.ScheduleSolution(1000,GH_ScheduleDelegate);
}
public void GH_ScheduleDelegate(GH_Document doc){
this.ExpireSolution(true);
}
[/code]
Is this what you mean ? (If so, I guess I should also add a Removed from Document callback and stop the Schedule somehow)
Thank you,
George
I attached source for a component which outputs the current date and time. It schedules solutions 500 milliseconds apart.
--
David Rutten
david@mcneel.com
Awesome! Thank you very much for taking your time for this sample, I appreciate it!
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by