Grasshopper

algorithmic modeling for Rhino

How does read file watch for changes in the file it is reading? It is so smooth and never has any errors. I have tried just attaching a timer to my component externally, but then it is being force to check the file rather than watching for changes and only updating when it is needed.

Thoughts?

Thanks!

Views: 2230

Replies to This Discussion

That was my hunch. I guess that  custom components can be much more powerful than python components. It was my understanding that python components are only updated by a timer or when an input changes. It seems like there is way more you can do with a custom component.

Grasshopper typically updates in the wake of events, not timers. I indeed use the filesystemwatcher but I've written a wrapper class for it so it's easier to use (I think). Grasshopper.Kernel.GH_FileWatcher.

Also the next release will have a synchronization option on the File Path parameter which will cause it to recompute whenever the target files change:

--

David Rutten

david@mcneel.com

Poprad, Slovakia

I cant seem to find any documentation on the GH_Filewatcher wrapper class in the SDK. Am i just missing it?

Thanks David!

I just uploaded a new version of the SDK docs. Download again to get the correct docs for 0.9.0052

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Awesome! I cant believe I have gotten this far! Ok, so I am pretty sure that I have done the whole delegate thing correctly. Is ExpireSolution(true) the best way to force it to resolve? As you can see, the  solution is very simple, but randomly as the file is changing, this script will freeze up Grasshopper and Rhino. How can I debug something like that?

Also, is there a better way to post code of these forums? Just as plain text seems sloppy to me.

Thanks for all your help! This is starting to make sense now!

/// <summary>
/// Expire the solution when ever the file has changed.
/// </summary>

public void FileHasChanged(GH_FileWatcher sender, string filename, WatcherChangeTypes change)
{
ExpireSolution(true);
}

/// <summary>
/// This is the method that actually does the work.
/// </summary>
/// <param name="DA">The DA object can be used to retrieve data from input parameters and
/// to store data in output parameters.</param>

protected override void SolveInstance(IGH_DataAccess DA)
{
// First, we need to retrieve all data from the input parameters.
// We'll start by declaring variables and assigning them starting values.
string json_path = "";
// Then we need to access the input parameters individually.
// When data cannot be extracted from a parameter, we should abort this method.
if (!DA.GetData(0, ref json_path)) return;

if (json_path.Length < 0)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "You need to assign a filepath");
}

// Instantiate the delegate as FileHasChanged
GH_FileWatcher.FileChanged fChanged = new GH_FileWatcher.FileChanged(FileHasChanged);

// Invoke the delegate
GH_FileWatcher watcher = GH_FileWatcher.CreateFileWatcher(json_path, GH_FileWatcherEvents.Changed, fChanged);

watcher.Active = true;

string json_string = System.IO.File.ReadAllText(json_path);

var json_list = JsonConvert.DeserializeObject<List<SliderObject>>(json_string);

string output = json_string;

DA.SetData(0, output);
for (int i = 1; i < this.Params.Output.Count; i++)
{
string NickName = this.Params.Output[i].NickName;
// Rhino.RhinoApp.WriteLine("Nickname: {0}", NickName);
int value;
for (int j = 0; j < json_list.Count; j++)
{
if (json_list[j].name == NickName)
{
Rhino.RhinoApp.WriteLine("Match found!");
value = json_list[j].value;
DA.SetData(i, value);
}
else
{
Rhino.RhinoApp.WriteLine("{0} doesn't match", json_list[j].name);
}
}
}


}

Why would you not be abl to do this with a python component?  I have not tried this myself, but it should be possible without a timer.  I found a few links that might help:

https://gist.github.com/vsajip/1675730

http://www.ironpython.info/index.php/Watching_the_FileSystem (not sure why the thread goes to sleep for an hour)

http://devwiki.beloblotskiy.com/index.php5/IronPython_Tutorial:_Adv...

My assumption is that you should be able to define events in the c#, vb.net, and python components without using a timer to update them.  Then again, like I said, I have not tried any of the samples above.

You probably can. I cannot as I still don't speak Python.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

...I still don't speak Python.

You have in the past

:)

I guess you will never belong to the house of Slytherin.....

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service