RhinoPython vs GrasshopperPython

I am trying to use the code below from RhinoScript to GrasshopperPython, but I get different results. I am trying to divide a curve using its domain and segments.

This is how the RhinoScript Syntax code looks like

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext
import random

crv = rs.GetObject(“Select the curve to divide”,rs.filter.curve)
segments = rs.GetInteger(“Select the number of divisions”,25,5)

crvdomain =rs.CurveDomain(crv,segments)

print crvdomain

step_size = (crvdomain[1]-crvdomain[0])/segments

t = crvdomain[0]

print step_size

for t in rs.frange(crvdomain[0],crvdomain[1],step_size):
A = rs.EvaluateCurve(crv,t)
rs.AddPoint(A)

This is how i have rewritten in GHPython but i am not getting the same results.

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext
import random

crvdomain =rs.CurveDomain(crv,segments)

step_size = (crvdomain[1]-crvdomain[0])/segments

t = crvdomain[0]

for t in rs.frange(crvdomain[0],crvdomain[1],step_size):
A = rs.DivideCurve(crv,t)

Hi @_AS,

The difference is that your Rhino Python script actively adds a point for each iteration of your for loop to the document, which the GHPython component doesn’t do. It executes all your code and outputs the resulting data, if desired. This means that you need to save, what you want to output in a variable, in your case a list of division points. This list gets then output, for instance, through script output a.

In the script below, the variables crv and segments are undeclared script component inputs.

import rhinoscriptsyntax as rs

crvdomain = rs.CurveDomain(crv, segments)

print "Domain Start: %.1f" %(crvdomain[0])
print "Domain End: %.1f" %(crvdomain[1])

step_size = (crvdomain[1] - crvdomain[0]) / segments

t = crvdomain[0]

print "Step Size: %.3f" %(step_size)

points = []

for t in rs.frange(crvdomain[0], crvdomain[1], step_size):
    pt = rs.EvaluateCurve(crv, t)
    points.append(pt)

a = points

Here’s an example:
rh_vs_ghpthon_01.gh (4.0 KB)

Next time, please make an effort to upload your GH or script file first. This makes coming up with an answer much faster, since people don’t have to setup their own files, to be able to look for answer to your problem.

3 Likes

Hello , I apologize for my mistake .I will make an effort to upload my GH file from next time. Thanks a lot for this.

2 Likes