I’ve been struggling to make definitions that work perfectly in the GH Python component work with rhinoinside. I’m running the newest Hops (1.6.0) and PyCharm with 3.10 as the interpreter and Flask.
import rhinoinside
rhinoinside.load()
from flask import Flask
import ghhops_server as hs
import Rhino.Geometry as rg
import math
[...]
def breakpolyline(polylines, tolerance):
if len([polylines]) > 0:
break_crv = []
break_pts = []
for crv in [polylines]:
end_pts = []
crv_pl = crv.ToPolyline()
exp_crvs = rg.Polyline.GetSegments(crv_pl)
This is where the code breaks, I get an error on the Hops component:
1. Exception occured in handler:
No method matches given arguments for Polyline.GetSegments: ()
File "C:\Users\levzh\SHAGA STUDIO Dropbox\Lev Zhitnik\CTIES_3D MODULE\CODE\TEST\app.py", line 38, in breakpolyline
exp_crvs = rg.Polyline.GetSegments(crv_pl)
Same with exp_crvs = crv_pl.GetSegments(). Do I need to re-write all of these things or did I not import things correctly? If I do need to re-write, is there a source for commands that work with rhinoinside?
Because this Method definitely appears on the RhinoCommon API: https://developer.rhino3d.com/api/rhinocommon/rhino.geometry.polyline#constructors
1. Exception occured in handler:
'rhino3dm._rhino3dm.Polyline' object has no attribute 'GetSegments'
File "C:\Users\...\TEST\app.py", line 39, in breakpolyline
crv_pl.GetSegments
Can’t figure out what I’m doing wrong here… Same thing happens if I add ‘()’ after as I try every single thing I know, nothing seems to work…
The reason for the error is because Polyline.GetSegments exists in RhinoCommon but not in Rhino3dm. If you need Polyline.GetSegments from CPython, you’ll need to use Rhino.Inside.
I must admit, your code is confusing. I see you trying to use Rhino.Inside but also Hops and Rhino3dm. Perhaps a overview of where you are trying to get to might be helpful.
Hi Dale, thanks again for your patience. This somewhat explains my confusion.
*EDIT: To explain, we need to move our processes to CPython (our project is much larger than this definition and there are parts that involve pandas and large dataframes). I’m using Hops because I’m trying to ‘break’ a specific curve that is generated in a GH file, and then I want to use rhinoinside to do the headless calculations. I’m trying to make this work to learn how Rhino.Inside works so that I can use RhinoCommon outside of gh/IronPython and move on to the more complicated definitions (for which I will need rhinoscriptsyntax as well, but I’ll tackle that later) and put them all in a single code. I hope I managed to explain myself.
I have a definition that breaks a curve into segments using an angle (I know there’s a method in RhinoCommon that’s supposed to do that but it seemed to not work well for us).
This is my original script that I’ve been using in my Python component inside GH and it worked great so far:
import Rhino.Geometry as rg
import rhinoscriptsyntax as rs
import math
def breakpolyline(polylines, tolerance):
if len([polylines]) > 0:
"""Break a polyline into individual lines by an angle
difference."""
break_crv = []
break_pts = []
for crv in polylines:
end_pts = []
crv_pl = crv.ToPolyline()
exp_crvs = rg.Polyline.GetSegments(crv_pl)
for i, crv_new in enumerate(exp_crvs):
end_pt1 = rg.Line.PointAt(crv_new, 0)
end_pts.append(end_pt1)
end_pt2 = rg.Line.PointAt(crv_new, 1)
end_pts.append(end_pt2)
new_i = (i + 1) % len(exp_crvs)
vector = crv_new.Direction
crv_2nd = exp_crvs[new_i]
vector_2 = crv_2nd.Direction
int_angle = rg.Vector3d.VectorAngle(vector, vector_2)
int_deg = math.degrees(int_angle)
if int_deg >= tolerance:
break_crv.append(new_i)
end_pts_cull = rg.Point3d.CullDuplicates(end_pts, 0.001)
for i, pt in enumerate(end_pts_cull):
break_pt = break_crv.count(i)
if break_pt > 0:
break_pts.append(pt)
cut_pt = []
for pt in break_pts:
t_crv = rg.PolylineCurve.ClosestPoint(crv, pt)
cut_pt.append(t_crv[1])
final_split = rg.PolylineCurve.Split(crv, cut_pt)
return final_split, break_pts
I thought that in CPython importing Rhino.Geometry is imported directly from rhino.inside because of the discourse in this thread:
I thought that importing Rhino after rhinoinside.load() is the same as simply importing Rhino.Geometry as rg so that it would fit all my existing definitions.
Should I then use something along the lines of
from Rhino import Geometry as rg
If importing Rhino is using rhino3dm, how do I use Rhino.Inside commands specifically to ensure it works with rhino.inside instead of Rhino3dm?
When using CPython outside of Rhino, use either Rhino.Inside or Rhino3dm, but not both. Rhino.Inside has all of the fucntionality of Rhino3dm. So it is preferred.
Also, I don’t know how well, if at all, rhinoscriptsyntax will work outside of Rhino. You see, rhinoscriptsyntax is document-centric. And you are wanting to work headless. So you might just want to stay clear of rhinoscriptsyntax, or just reference the source code when needed.
Understood, so if that’s the case is there a way to connect rhino.inside to a running gh instance and use a geometry from it? Or an easy way to import a specific object (or objects) from a Rhino document into the CPython definition?
Sorry I haven’t replied until today - been out-of-town for a bit.
Are you still having a problem?
Sorry but I don’t understand this. If you are using Rhino.Inside, you are using Rhino and Grasshopper. So I’m confused what you are trying to do and why - sorry.