Running Rhino commands in Grasshopper Python

Thank u Piac :smile: That helped but i couldn’t get it running on Grasshopper . I m guessing that points should be baked from gh to active document and get points from document … or is there any way ???

import rhinoscriptsyntax as rs
import math
a= rs.command("_MeshfromPoints",True)

is something i ran on rhino py console… donno why py gh didnt work …

Oh you want that in Grasshopper. I just did a quick search and Tudor already wrote that script, maybe not in Python, but it should work as advertised. If you want to translate it to Python, it should also be possible.

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

1 Like

Yes Piac… i have this component in gh but since i m a begginer i m trying to recreate basic to tricky components to teach myself Code.

Hi Giulio,

where can I look up, what Arguments the Command “_Line” needs?
Where Do I know that I need to attach “0,0,0 2,2,2” to that “_Line”?

I ask that, because I need to know which argument “_TextObject” command needs to generate a text solid with rs.Command("_TextObject", True).

Thanks a lot
Regards
Mr Sinter

HI Mr_Sinter

like this:

import rhinoscriptsyntax as rs
str = rs.GetString(“get string”)
rs.Command(" -TextObject " + str + " 0,0,0 “+” _Enter")

Here is my generic sample for generating solids with a text object. There are a LOT of command line options and you do need to have them in the right order. --Mitch

import rhinoscriptsyntax as rs

def CreateTextObjsAsSolids():
    text_string=rs.GetString("Text to insert?")
    if not text_string: return
    pt=rs.GetPoint("Text insert point?")
    if not pt: return
    ht=rs.GetReal("Text height?",minimum=0)
    if not ht: return
    thk=rs.GetReal("Solid thickness?",minimum=0)
    if not thk: return
    
    opts='_GroupOutput=_Yes _FontName="{}" _Italic=_No _Bold=_No '.format("Cambria")
    opts+='_Height={} _Output=_Solids _Thickness={} '.format(ht,thk)
    opts+='_LowerCaseAsSmallCaps=_No _AddSpacing=_No '
    
    rs.EnableRedraw(False)
    rs.Command('_-TextObject '+opts+'"'+text_string+'"'+' '+str(pt),True)
CreateTextObjsAsSolids()
3 Likes

That is absolutely great!

Thanks a lot
Grz Mr_Sinter

Hi Mitch,

I don’t know where I can look up what arguments commands like “_-TextObject” need…

How can I find out, that this command takes options such as those "_GroupOutput=_Yes _FontName="{}" _Italic=_No _Bold=_No "…?

I need this knowledge for several commands, as I want to start Grasshopper with some arguments too…

Thanks a lot in advance
Mr Sinter

Hi @Mr_Sinter,

that is a macro. You can read about macros in the RhinoScript 101 PDF:
http://wiki.mcneel.com/developer/rhinoscript101
These are just the options that end up being written in the command line when you run a command. The hyphen sign (or minus) “-” is used to trigger the no-dialog version of the command. Please read more in the Rhino Help, under macro, or in the PDF above.

I hope this helps,

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hi @piac,

I read about macros in “http://wiki.mcneel.com/rhino/basicmacros”, but it also did not tell me where I can look up which arguments a macro needs/accepts.
Like I asked, how can I see in advance that the macro _TextObject has _Bold=_No as argument?

I tried it with this method:

command_id = rhi.Commands.Command.LookupCommandId('_GrasshopperOpen', False)
print "%s"%rhi.Commands.Command.DisplayHelp(command_id)

But this did not gave me a list of which arguments the macro '_GrasshopperOpen' accepts…
I need to open a certain GH-file with '_GrasshopperOpen' and additionally perform a Grasshopper Bake on a Rhino geometry…

Thanks much in advance
Mr Sinter

You can see that by running the Rhino Command.
Type “_-TextObject” in the Rhino command line. You will see…

Type in “_-GrasshopperOpen” in the command line, you’ll see what I mean…
This does NOT give me the list of arguments this macro accepts, there appears another command line which says:
"
GH or GHX file to open:
"
I want to automatically start a certain GH-file with Grasshopper without being asked by Rhino…
Additionally I want to see, if this "_-GrasshopperOpen"-macro accepts additional arguments, as I need to perform operations with GH on a brep with has been created in Rhino, and I want to “hand over” this brep automatically to GH

Thanks in advance

You inquired about “TextObject”. This request about _GrasshopperOpen would make a good start of a new discussion.

Trying to automate Grasshopper from within a Grasshopper solution, while possible, opens several levels of issues. Be sure that you really know why you need this. The “_GrasshopperOpen” command simply accepts an argument without an option. This means that you are forced to put that one option there, and there are no additional options to call after that.

It’s possible in other cases that a developer intentionally adds hidden commands or command options. It is not possible to know which ones exist, or how to invoke them, unless the developer reveals them. In this case, there are no command options to call (that I know of).

Please start a new discussion if you need help regarding a specific command macro.

Alright I see…The only way to see what arguments these macros accept is to try it out in the command line…

So I found out in try-and-error-style how to open a specific GH-file by script:

rs.Command('_-Grasshopper' +' '+ 'Document' +' '+ 'Open' +' '+ 'somefile.gh')

This still does not resolve for me the question why there is a scripting docu about Rhino and rhinoscriptsyntax, but none about macros…

Nevertheless thanks very much so far
Regards,
Mr Sinter

@Mr_Sinter,

check out the example below, with the attached DrillHole.gh file. Basically the script does the following:

  • Select a solid brep (eg, a sphere or box)
  • Get the Grasshopper plugin object
  • Open the DrillHole.gh file
  • Pass the id of the brep to an existing slot in gh file
  • Change any parameter in the gh file
  • Run the Grasshopper solver
  • Bake the resulting brep to rhino and select it
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DrillHoleWithGrasshopper():
    id = rs.GetObject("Select a closed brep", 16, True, False)
    if not id: return False
    
    Grasshopper = Rhino.RhinoApp.GetPlugInObject("Grasshopper")
    if not Grasshopper: return False
    
    Grasshopper.OpenDocument("C:\DrillHole.gh")
    Grasshopper.DisableSolver()
    Grasshopper.AssignDataToParameter("input_brep", id)
    Grasshopper.AssignDataToParameter("circle_radius", 2.0)
    Grasshopper.RunSolver(True)
    
    objs = Grasshopper.BakeDataInObject("output_brep")
    if not objs: return False 
    rs.SelectObjects(objs)
    
    Grasshopper.CloseDocument()
    Grasshopper.HideEditor()
    
if __name__=="__main__":
    DrillHoleWithGrasshopper()

Do not forget to change the file path in the script above which loads the DrillHole.gh file into Grasshopper.

DrillHole.gh (8.4 KB)

c.

5 Likes

Well, you might imagine there are close to 1000 Rhino commands. That would require writing a ~1000 page documentation… Best is just to run the command with a dash and look at the arguments. --Mitch

Hi Clement,

regardless of these Grasshopper operations working correctly (for me they do not…no holes are punched in the solid), your post is really great. This is exact the “data handover” from rhino to grasshopper I need!
I will apply your example to my specific application case

Thanks a lot
Mr Sinter

Yeah right, was just wondering…
Maybe this issue will be solved by one individual who has lots of time

@Mr_Sinter,

Yeah sorry, i’ve just quickly hacked this together. If the solid object you select in Rhino is a 10x10x10 cube or sphere with diameter 5 it should work. Otherwise you can change the cylinder_height, either from within the script or in gh.

good. :wink: Btw. one thing about passing the name of the *.gh file i´ve forgotten: If the file path to this *.gh file or the filename itself has spaces within, you better load it using this line:

Grasshopper.OpenDocument( chr(34) + "C:\DrillHole.gh" + chr(34) )

This will put a pair of extra quotes around the grasshopper file.

c.

There is a little trick with python - as it accepts both double and single quotes and doesn’t mix them - you can usually do something like this:

Grasshopper.OpenDocument("'C:\DrillHole.gh'")

(I hope it works in this case)

–Mitch