# Extracting object data based on layer python

**URL:** <https://discourse.mcneel.com/t/extracting-object-data-based-on-layer-python/45634>\
**Category:** Scripting\
**Tags:** rhino, rhinopython, python\
**Created:** [July 7, 2017, 3:03pm UTC](https://discourse.mcneel.com/t/extracting-object-data-based-on-layer-python/45634 "2017-07-07T15:03:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Gornall](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/christopher_gornall/32/63001_2.png) [@Christopher\_Gornall](https://discourse.mcneel.com/u/Christopher_Gornall)\
**Post date:** [July 7, 2017, 3:03pm UTC](https://discourse.mcneel.com/t/extracting-object-data-based-on-layer-python/45634/1 "2017-07-07T15:03:37Z")

</div>

Hey, I’m getting a little hung up. I am trying to extract the point data from various objects in a layer (all simple rectangles). so I am trying to choose the layer by name(there will be multiple with the same naming format…i.e Ballast\_1), identify the objects in the layer, and extract all the point coordinates. it would be cool if it could just cycle through the layers based on the naming format. anything can help.

thanks, Chris

---

<div class="post-metadata">

**Author:** ![Christopher\_Gornall](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/christopher_gornall/32/63001_2.png) [@Christopher\_Gornall](https://discourse.mcneel.com/u/Christopher_Gornall)\
**Post date:** [July 7, 2017, 7:04pm UTC](https://discourse.mcneel.com/t/extracting-object-data-based-on-layer-python/45634/2 "2017-07-07T19:04:54Z")

</div>

```auto
import rhinoscriptsyntax as rs
import scriptcontext as sc
import math
 
##Start Evaluation
def main():
    response=rs.MessageBox( "Evaluate Classroom?", 4)
    if response !=7:
        #Select a layer    
        layer = rs.GetLayer("Please select a layer")

        #If a layer is chosen:
        if layer:
            #Gets objID
            obj = rs.ObjectsByLayer(layer)

            #Selects objects
            rs.SelectObjects(obj)
            ObjId=rs.SelectedObjects(include_lights=False, include_grips=False)
            for obj in ObjId:
                cent=rs.SurfaceAreaCentroid(obj)
                print cent[0]
                rs.AddPoint(cent[0])

    else:
        rs.MessageBox("Evaluation Aborted")

main()

```

so far i have this. the center point is more useful to me. my selection is still manual though which is holding me back a bit

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/759176_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [July 8, 2017, 2:46am UTC](https://discourse.mcneel.com/t/extracting-object-data-based-on-layer-python/45634/3 "2017-07-08T02:46:04Z")

</div>

@Christopher_Gornall,

see if below is helpful, it adds the area centroids (points) to all surfaces on layers which name contains the word “Ballast”:

```python
import Rhino
import scriptcontext 
import rhinoscriptsyntax as rs

def DoSomething():
    # define something as layer name to search for
    search_string = "Ballast"
    # iterate over all layers
    for layer in scriptcontext.doc.Layers:
        # check if search string appears in layer name
        if search_string.lower() in layer.Name.lower():
            # iterate all objects on that layer 
            for rh_obj in scriptcontext.doc.Objects.FindByLayer(layer):
                # only process surfaces
                if rs.IsSurface(rh_obj.Id):
                    # get the area centroid
                    rc = rs.SurfaceAreaCentroid(rh_obj.Id)
                    if rc:
                        # add a point on this layer
                        attr = Rhino.DocObjects.ObjectAttributes()
                        attr.LayerIndex = layer.LayerIndex
                        scriptcontext.doc.Objects.AddPoint(rc[0], attr)
    # refresh views
    scriptcontext.doc.Views.Redraw()
    
DoSomething()

```

- 

c.

---

<div class="post-metadata">

**Author:** ![Christopher\_Gornall](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/christopher_gornall/32/63001_2.png) [@Christopher\_Gornall](https://discourse.mcneel.com/u/Christopher_Gornall)\
**Post date:** [July 11, 2017, 12:32pm UTC](https://discourse.mcneel.com/t/extracting-object-data-based-on-layer-python/45634/4 "2017-07-11T12:32:34Z")

</div>

thank you, this helped, id figured out the centroid aspect but the layer selection is super helpful. thanks
