Help with python recognizing each selected object in a list


I’m currently working on a project that has to do with generating blocks based on the number of blocks around them. I have a working script that generates the first state randomly giving values of 1 (on) or 0 (off) for each point on a grid so that the rest of the script can react to it using the logic that I insert (Kind of like cellular automata if you know what that is).

Anyways, now instead of randomly generating the first form, I want to be able to start out with a geometry that I manually put into rhino, and then have the logic work from there.

currently I have something like this

obj = rs.GetObjects ("select objects")

def SelectedObjects (xDimension, yDimension, zDimension, obj):
   arrStates = []
   for i in range (0, xDimension):
     for j in range (0, yDimension):
       for k in range (0, zDimension):
         for v in (obj):
           centroid = rs.CurveAreaCentroid (v)
           if centroid:
            print centroid

obj = rs.GetObjects ("select objects")
xDimension = 5
yDimension = 5
zDimension = 5

Sorry if this doesn’t make sense. But what I want the script to do is spit out a list of centroids for each object in the list so that eventually I can say something like:

if centroid == 1
  arrv.append (1)

and do that for the other values i,j,k. This all leads up to eventually having logic like:

if vals[i][j][k] == 1
  (draw a cube here)

after this the logic for the rest of the script should work.

But from what I currently have I’m not getting anything… no bugs, no text, nothing.
I assume I have some text misplaced but I’m not sure where… I’m kind of new to python.

Hi Noah, looks like you have a good start. You need a bit of python fundamentals tidied up but it’s nearly there. I didn’t entirely follow what you’re trying to do, but the example below allows you to select curves and prints the centroid points.

tip 1: post your code as "preformatted text, it’s easier for others to read.

tip 2: It looks like you define the SelectedObjects function but never call it. this may explain why you don’t see any output.

tip 3: you have several for loops which don’t do anything, I commented those out for now.

tip 4: use filters to simplify your inputs. you can filter for curves with rs.GetObjects (planarity checks are more complex, this example will throw an error if you give it a non-planar curve)

Here’s an example, based on what you’d posted, hope this helps.

import rhinoscriptsyntax as rs

def SelectedObjects (xDimension, yDimension, zDimension, obj):
    #arrStates = []
    #for i in range (0, xDimension):
    #for j in range (0, yDimension):
    #for k in range (0, zDimension):
    for v in obj:
        if rs.IsCurve(v):
            if rs.CurveArea(v) is not None:
                centroid = rs.CurveAreaCentroid(v)

obj = rs.GetObjects("Select Curves", filter = rs.filter.curve)

if obj is not None:
    SelectedObjects(5,5,5, obj)

Thanks, that helped a lot!

I’m sorry the wording was confusing… I know it doesn’t make much sense to explain it.
Also thanks for the tips on formatting the forum posts. I’ll keep them in mind next time.