Looping repetitive commands, and using their info

Hi,

I have a basic code by which im trying to get the center of a number of curves without them sitting on a cplane.

However im currently doing this for each curve in the list manually, i rather just loop it to apply the code to each curve in a list, and then print out the centroid.

I also need to be able to use this centroid values to select specific curves from these lists. Not sure how to go about it, help is so appreciated. My code is down below:

from Rhino.Commands import Result
from Rhino.DocObjects import ObjectType
import rhinoscriptsyntax as rs
from scriptcontext import doc

layer_objs = rs.ObjectsByType(4,True)
if layer_objs:
#create an empty list
mylist=[]
for obj in layer_objs:
#find curves among the objects
if rs.IsCurve(obj):
if abs(rs.CurveLength(obj)<100):
#curve meets selection criteria, append to “good” list
mylist.append(obj)

rs.UnselectAllObjects()

#CURVE1
#rs.UnselectAllObjects()
rs.SelectObjects(mylist[0])
bBox = rs.BoundingBox(mylist[0])
cent = [(bBox[0][0] + bBox[6][0])/2,(bBox[0][1] + bBox[6][1])/2,(bBox[0][2] + bBox[6][2])/2]
print cent
rs.AddPoint(cent[0],cent[1],cent[2])

Hi @ZKislost, see if below helps:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DoSomething():
    # get selectable curves
    curve_ids = rs.ObjectsByType(rs.filter.curve, select=False, state=1)
    if not curve_ids: return
    
    # set length to search for
    length = 100.0
    
    # filter out curve ids shorter than length
    mylist = filter(lambda crv: rs.CurveLength(crv) < length, curve_ids)
    
    # add point to bbox center
    for crv_id in mylist:
        bbox = rs.BoundingBox(crv_id)
        cent = (bbox[0] + bbox[6]) * 0.5
        rs.AddPoint(cent)
    
DoSomething()

_
c.

1 Like

Hey thanks for your response, definitely a lot neater then the way i was handling it :slight_smile: definitely gave me ideas on how to improve.