Grasshopper

algorithmic modeling for Rhino

Hi guys,

Today i was trying to write some GhPython code to fit a curve to a defined length, but whatever i did, i failed, and i cannot figure out where i am wrong. However, when i used Python directly to write almost the same code, it worked! Maybe there are some differences between Python and GhPython that i don't know. Anyone can help me figure that out?

thanks.

GhPython Code:

import rhinoscriptsyntax as rs

def fitCurveLength(curve):

    while True:
        length = rs.CurveLength(curve)
        if length <= length_limit: break
        origin = rs.CurveAreaCentroid(curve)
        curve = rs.ScaleObject(curve, origin[0], (0.95, 0.95, 0.95), True)

a = fitCurveLength(curve_id)

Runtime error (TypeErrorException): iteration over non-sequence of type NurbsCurve

Python Code:

import rhinoscriptsyntax as rs

def fitCurvetoLength():
    curve_id = rs.GetObject("Get the curve to FIT")
    if curve_id is None: return
    length = rs.CurveLength(curve_id)
    length_limit = rs.GetReal("Set the limit of the length",length*0.5, length*0.1 )
    if length_limit is None: return

    while True:
        length = rs.CurveLength(curve_id)
        if length <= length_limit: break
        origin = rs.CurveAreaCentroid(curve_id)
        curve_id = rs.ScaleObject(curve_id, origin[0], (0.95, 0.95, 0.95), True)

fitCurvetoLength()

all files attached

FitCurvetoLength.gh

Views: 1470

Attachments:

Replies to This Discussion

Hi,

Change your curve_id type from "Curve" to "ghDoc Object". You are using rhinoscript function rs.ScaleObject() which requires "guids". Guid is a string (of characters) and string is sequence type. So that is the cause of your error message:

Runtime error (TypeErrorException): iteration over non-sequence of type NurbsCurve

It means: you are not providing a string(guid) but an object of type NurbsCurve.

Other than that your function is not returning anything, and you need to return a list of curves:

import rhinoscriptsyntax as rs


def fitCurveLength(curve):
    a = []
    while True:
        length = rs.CurveLength(curve)
        if length <= length_limit: break
        origin = rs.CurveAreaCentroid(curve)
        curve = rs.ScaleObject(curve, origin[0], (0.95, 0.95, 0.95), True)
        a.append(curve)
    return a

a = fitCurveLength(curve_id)

Check the attached file.

Attachments:

Hi djordje,

thank you for your detailed reply. Very helpful to me:)

Maybe something that I could call a 'collateral' doubt I have: couldn't you scale right away the curve at the beginning with the factor obtained by dividing wanted_length with current_length?

Thanks,

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

Hi Giulio,

thanks for your suggestion. actually what i want to get is a series of curves as shown below, not just the final curve with the wanted length. the name of the function is not that precise. i have updated my script based on djordje's. 

thank you anyiway. you guys are sooooo warmhearted!!!

Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service