+ Reply to Thread
Results 1 to 3 of 3

Thread: Initiating Grasshopper via Python

  1. #1

    Initiating Grasshopper via Python

    Hello,
    I have several grasshopper scripts which I use to parametrically model in rhino. I was curious as to if I may to streamline the process of implementing a grasshopper script via python. I know I can use:
    rs.Command("_Grasshopper")
    to start running the grasshopper plugin but is there a way to specify which .gh file grasshopper opens?

    Additionally is it possible to use:
    rs.GetObjects()
    to select the input geometries for grasshopper rather than having to right click and pick "set curves" for geometry components.

    Thanks in advance,
    5chmidt

  2. #2
    Senior Member Steve Baer's Avatar
    Join Date
    Apr 2010
    Location
    Seattle
    Posts
    1,464
    Blog Entries
    19
    Yes, this can be done. You'll need to use a little RhinoCommon, but it shouldn't be too bad.
    Python Code:
      import Rhino
       
      gh = Rhino.RhinoApp.GetPlugInObject("grasshopper")
      if gh:
          gh.ShowEditor()

    Here is the list of functions you can call on the grasshopper object.

    c# Code:
      //Find a parameter and assign some persistent data.
      //Parameters:
      //  parameterID: Instance ID of parameter or parameter name.
      //  data: Data to assign.
      // Returns:
      //     True on success, false on failure.
      public bool AssignDataToParameter(string parameterID, object data);
       
      //Find an object and bake all geometry inside of it.
      //Parameters:
      //  objectID: Object InstanceID or name.
      // Returns:
      //     An array of Rhino object IDs, or null on failure.
      public object BakeDataInObject(string objectID);
       
      //Close all Grasshopper documents.
      // Returns:
      //     True on success, false on failure.
      public bool CloseAllDocuments();
       
      // Close the currently active Grasshopper document. If there is not active document,
      // nothing will happen.
      // Returns:
      //     True on success, false on failure.
      public bool CloseDocument();
       
      // Disables the display of the Grasshopper banner during Component loading.
      // The banner is typically only shown once during a Grasshopper session, namely
      // when the Editor is first loaded.
      public void DisableBanner();
       
      // Disables the Grasshopper Solver. If the Solver is disabled, expired components
      // and parameter will not be recomputed, though any existing solution will remain
      // intact.
      public void DisableSolver();
       
      // Enables the display of the Grasshopper banner during Component loading. The
      // banner is typically only shown once during a Grasshopper session, namely
      // when the Editor is first loaded.
      public void EnableBanner();
       
      // Enables the Grasshopper Solver. If the Solver is enabled, expired components
      // and parameter will be recomputed.
      public void EnableSolver();
       
      // Hide the main Grasshopper Editor. If the editor hasn't been loaded or if
      // the Editor is already hidden, nothing will happen.
      public void HideEditor();
       
      // Returns the loaded state of the Grasshopper Main window.
      // Returns:
      //     True if the Main Grasshopper Window has been loaded.
      public bool IsEditorLoaded();
       
      // Returns the visible state of the Grasshopper Main window.
      // Returns:
      //     True if the Main Grasshopper Window has been loaded and is visible.
      public bool IsEditorVisible();
       
      // Returns the state of the Grasshopper Solver.
      // Returns:
      //     True if the Grasshopper Solver is enabled.
      public bool IsSolverEnabled();
       
      // Load the main Grasshopper Editor. If the editor has already been loaded nothing
      // will happen.
      public void LoadEditor();
       
      // Open a Grasshopper document. The editor will be loaded if necessary, but
      // it will not be automatically shown.
      // Parameters:
      //   filename: Path of file to open (must be a *.gh or *.ghx extension).
      // Returns:
      //     True on success, false on failure.
      public bool OpenDocument(string filename);
       
      // Runs the solver once, even if the global solver lock is on.
      // Parameters:
      //   expireAllObjects: If true, will expire all objects in the current document.
      public void RunSolver(bool expireAllObjects);
       
      // Save the currently active Grasshopper document. If the active document has
      // never been saved, a Save... dialog will be shown.  If there is no active
      // document, nothing will happen.
      // Returns:
      //     True on success, false on failure.
      public bool SaveDocument();
       
      // Save the currently active Grasshopper document in a specific location. If
      // there is no active document, nothing will happen.
      // Parameters:
      //   filename: Path of file to save to (must be a *.gh or *ghx extension).
      // Returns:
      //     True on success, false on failure.
      public bool SaveDocumentAs(string filename);
       
      // Show the main Grasshopper Editor. The editor will be loaded first if needed.
      // If the Editor is already on screen, nothing will happen.
      public void ShowEditor();

  3. #3
    Steve,
    Thanks for the reply, it was very helpful,
    5chmidt

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts