Best way of converting grasshopper workflow into python

I am having lots of trouble converting grasshopper functions into python scripts. I am using the python script module in grasshopper, and I am confused about whether to use rhinoscriptsyntax or RhinoCommon. For instance, I use silhouette = meshIn.GetOutlines(viewPort) to get a “silhouette” polyline curve. From there, I want to divide that curve. However I can’t do points = rs.DivideCurve(silhouette, segments), because I get an error that says "Parameter must be a Guid or string representing a Guid).

The problem with RhinoCommon is different but equally problematic. First off, the .GetOutlines command returns an object called Array[Polyline] which makes it seem as though I cannot do any of the RhinoCommon commands, include Polyline.GetSegments(silhouette). However if I just plug the output of my python script (silhouette) into the divide curve module in grasshopper, it works fine! So what am I doing wrong here? How do I turn the following grasshopper workflow:

into a Rhino Python script if the input to Crv is Array[Polyline] ((<Rhino.Geometry.Polyline object at …

Rhinoscriptsyntax operates in the Rhino space.

so first up in your python script, change script context to Rhino.Rhinodoc.Activedoc
when you are done, make sure to change the script context back to ghdoc.

you would need to iterate through the list of polylines, so in python:

line_list = []
for poly in Silhouette:
    line_list += poly.GetSegments()

However that is different from the divide curve you are doing in the grasshopper script as the getsegments is more of an explode than divide.

for poly in Silhouette:
    parameters = poly.ToNurbsCurve().DividebyCount(30,True)

How do I initialize the Rhino.Rhinodoc.Activedoc script context? I start my python scripts with:

import Rhino
from Rhino.Geometry import *
from Rhino.Input import RhinoGet
from Rhino.Commands import Result
from Rhino.DocObjects import ObjectType
import rhinoscriptsyntax as rs
from scriptcontext import doc

what do I change?

I wrote " import scriptcontext as sc " and then “sc.doc = Rhino.RhinoDoc.ActiveDoc
for poly in Silhouette:
parameters = poly.ToNurbsCurve().DividebyCount(30,True)”

But now some of my previous functions are not working. For instance I used to have a line that said “silhouette = meshIn.GetOutlines(viewPort)” and worked fine. Now I get an error on that line saying object has no attribute GetOutlines. So I can’t simply switch back and forth?

Hi @kieganlenihan,

You might have to upload the gh file because I don’t know what you ghpython component looks like.

I don’t know what type of parameter the input was set as which could change what that variable was imported as.

This is the only amount of code that seems to work, I want to add functions like Divide Curve but nothing is working!

OK here is the general problem. I run into errors because my objects are poorly identified as Rhino.Geometry objects when they should be GUIDs. Look at the following picture.


I print the variable “silhouette” and the output is Array[Polyline]. But then I put that output into the script on the right and when I print silhouette, I get a Guid!!! From there I can modify points using functions like rs.DivideCurve(), but all the points from that function print out as Rhino.Geometry.Point3d when I just want a Guid to be printed. Am I completely off the mark here? How do I keep the format of objects as guid’s so that I can just use rhinoscriptsyntax the whole time?

@Michael_Pryor

Hello,
Does this help? You will find lots more like this on the forum if you search for Rhinocommon, rhinoscriptsyntax and guid.

1 Like