Grasshopper

algorithmic modeling for Rhino

I am having some trouble getting some methods in the Grasshopper.Plugin.GH_RhinoScriptInterface to work.  I am attempting to automate some GH computation from within a Rhino plug-in but the behavior is not matching the behavior I experience when I call the methods via the com interface outside of Rhino.  Specifically, the OpenDocument method causes a "Grasshopper display error".  

 (Error Message)

(Resulting Canvas)

I know I am not initializing the GUI correctly but not sure where to go.  I can dig deeper into the SDK (ie all the way to GH_DocumentIO.Open()) but honestly much of what I want to do is exposed in GH_RhinoScriptInterface.

My code right now is as follows:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
bool flag1 = false;
bool flag2 = false;

flag1 = Grasshopper.Plugin.Commands.Run_Grasshopper();


Grasshopper.Plugin.GH_RhinoScriptInterface GH_Rhscript = new Grasshopper.Plugin.GH_RhinoScriptInterface();

flag2 = RScript.OpenDocument(@"C:\Temp\Grasshopper\Fins.gh"); //fail

return Result.Success;
}

I have also tried:

var GH_DocEditor = new Grasshopper.GUI.GH_DocumentEditor();
GH_DocEditor = Instances.DocumentEditor;

flag2 = Instances.DocumentEditor.ScriptAccess_OpenDocument(@"C:\Temp\Grasshopper\Fins.gh"); //fail

The SDK docs says "You are unlikely to need any of this." so here I am.

Tags: SDK

Views: 584

Reply to This

Replies to This Discussion

Hi Craig,

I'm confused by the approach here. You can use the Grasshopper SDK when you're running as part of Grasshopper, otherwise, you have to use the GetPluginObject methods. This seems to be a mixture of both. If you're writing a Rhino command plugin (rhp) then linking against Grasshopper.dll and invoking methods there is almost certainly not going to work.

In RhinoScript, the code you're looking for would be written like this:

Call Main()
Sub Main()
  Dim GH : Set GH = Rhino.GetPlugInObject("Grasshopper")
  GH.LoadEditor()
  GH.OpenDocument("C:\Temp\Grasshopper\Fins.gh")
End Sub

In C#, you should probably use a dynamic variable:

dynamic GH = RhinoApp.GetPlugInObject("Grasshopper");
if (GH == null)
{
  RhinoApp.WriteLine("Grasshopper could not be found.");
  return Result.Failure;
}

GH.LoadEditor();
GH.OpenDocument(@"C:\Users\David\Desktop\PiByPolygon.gh");

--

David Rutten

david@mcneel.com

Poprad, Slovakia

I have an external application that uses the dynamic variables you describe to create Rhino application, Rhino Script, and Grasshopper Plugin objects.  I can load the Grasshopper plugin programmatically and all the methods in GH_RhinoScriptInterface work fine.  The reason that I would like to move this logic into a Rhinoscript command plugin is to have better access to the internal data in my Rhino file.  The scripting api has limited access by design.  

Initially, I tried to use the same calls for the grasshopper portion in my rhp but they do not work as far as I could tell.  Here is that code:

Guid gh_plugin_id = new Guid("b45a29b1-4343-4035-989e-044e8580d9cf");
//const string gh_plugin_id = "Grasshopper";

dynamic gh = Rhino.RhinoApp.GetPlugInObject(gh_plugin_id);

gh.OpenDocument(@"C:\Temp\Grasshopper\Fins.gh");

Neither the Guid or the string work as they do outside of the Rhp.  This is why I starting linking directly to the Grasshopper.dll

The ultimate goal is too use the AssignDataToParameter to programmatically change the inputs of my .gh file and BakeDataInObject to automate the output.  

So calling

dynamic gh = Rhino.RhinoApp.GetPlugInObject(gh_plugin_id);

 

results in a failure? Or does it go wrong at the gh.OpenDocument level?

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

The dynamic resolution of gh always returns as null which causes it to fail at gh.OpenDocument

What happens when you call

RhinoApp.RunScript("_Grasshopper", false); 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Ok, now it's working.  Even RunScript didn't work at first while debugging remotely which made no sense.  I ran without debug and it all worked: RunScript, GetPlugin, OpenDocument, etc...  I am assuming that the AssignDataToParameter will work with GUID's that I can now access via my Rhd.

Also Thanks very much

Ok, glad you found the problem.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

RSS

Translate

Search Grasshopper

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2014   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service