Grasshopper

algorithmic modeling for Rhino

does anyone know if it is possible to us a timer component to update only a portion of code

for example if i have a c# component and inside i have, x is a point3d list

//code that should run once

Point3d[]  pts = new Point3d[x.count]
pts = x.ToArray(); 


//code that will update at the interval set by the timer component

for(int i=0; i<pts.lenght; i++)
{
pts[i].Transform(Transform.Translation(i,i,i);

}



Basicaly i do not want the initial array to get overwrittent everytime the timer component runs


Views: 670

Replies to This Discussion

Hi Robert,

if you want to maintain data between solutions, you should declare that data as a Shared class member like so:


Private Shared myImportantData As SomethingOrOther


instead of


Sub RunScript(....)
Dim myImportantData As SomethingOrOther
...
End Sub


Shared data will persist between solutions.

--
David Rutten
david@mcneel.com
London, UK
In C#, "Shared" is called "static". Static fields are guaranteed to be thread-safe, which might be useful in some cases.

If you always only access that field with one thread only (which is true for most Gh scripts, and for the System.Windows.Forms.Timer -- not for any other timer), then just make that variable a field. This can be accomplished by putting it into the "additional code" part in the end of the script source without the static keyword.

(Under this line is just the C# version of what David wrote):

private static SomeType myImportantData;
instead of:
void RunScript(....){
SomeType myImportantData;
...
}


- Giulio
_________________
giulio@mcneel.com
McNeel Europe, Barcelona
thanks for the suggestion but i don't think i can call a static class and not have it overwrite itself every time the node evaluates. i do not think i am being clear so i included an example. but also here is the code below. this is inside a C# node in grasshopper (although i would be ok with building it into a gha if i have more options inside VS). the idea in this example is that the points just move randomly until the timer object stops but what happens is the points position get reset every time the timer expires the solution.

i was thinking i could do it by putting the loop in a while() statement and use threading.sleep() to pause and then call for a refresh of the screen but cannot figure out how to call for a refresh/update


private void RunScript(List pts, object y, ref object A)
{
double ranX, ranY, ranZ;
Random ran = new Random();
double ranRange = 0.1;

//i need this to stay static (run only at the start of the timer object) until i stop the timer object or
//call for a reset.

Point3d[] newPts = new Point3d[pts.Count];
newPts = pts.ToArray();

//i want this to update with the timer object
for(int i = 0; i < newPts.Length; i++)
{

ranX = (ran.NextDouble() * ranRange) - (ranRange / 2);
ranY = (ran.NextDouble() * ranRange) - (ranRange / 2);
ranZ = (ran.NextDouble() * ranRange) - (ranRange / 2);

newPts[i].Transform(Transform.Translation(ranX, ranY, ranZ));
}

A = newPts;


}
forgot the file
Attachments:
Is it possible that the "Buckets" example on this page is answering your question? (Buckets for GH 0.6.0043 should still load in version 0.6.59)

- Giulio
_______________
giulio@mcneel.com
McNeel Europe, Barcelona
that is exactly it, now i just have to figure out what you did!
on second thought . the definition is close but you had to break the random number creation and moving points into two components and only update the random number component. i need it to all be written into one script. but i think i can work with the form.timer aspect to try to get it all into one.
ok got it! your a genius
1 more timer question.

if i have a timer attached to a script node (c#) or even a custom node is there a way to query properties of the timer. like

bool has a timer attached {get;}
bool timer is running {get;}
double timer interval {get; set;}

in the end i what i am working on will be all done as a GHA in VS so perhaps this function exits where i can do something like

this.timer; returns true if the node has a timer attached
this.timer.IsRunning();
this.timer.Interval();
this.timer.Interval(100);

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service