Get layer of an object with rs.ObjectLayer(x) inside Python compnent

grasshopper
python

#1

I want to get the layer of an object inside grasshopper with Python component using

import rhinoscriptsyntax as rs
a = rs.ObjectLayer(x)

Type hint Brep
And I get 1. Solution exception:Parameter must be a string or Guid

What’s the correct way to do this?
Thanks


#2

I think you’ll need to set the “Type hint” differently, below seems to work:

_
c.


Pass "current layer" information onto grasshopper? [SOLVED]
#3

@Bogdan_Chipara, btw. the example above uses FindId which only works in Rhino 6.

_
c.


#4

Thanks @clement,
Is it possible to use rhinoscriptsyntax ?


#5

Thanks for mentioning that. You saved me the trouble of re-typing your code, which in itself is a waste of time, all the more so because it won’t work in Rhino 5. Can anyone provide a Rhino 5 solution? Seems like a very reasonable question to me,


#6

@Joseph_Oster, in Rhino 5 it should be enough to use this instead:

import Rhino
obj = Rhino.RhinoDoc.ActiveDoc.Objects.Find(x)

def MyGetObjectLayer(obj):
    index = obj.Attributes.LayerIndex
    layer = Rhino.RhinoDoc.ActiveDoc.Layers[index]
    return layer.Name

a = MyGetObjectLayer(obj)

_
c.


#8

Working code would be so helpful? Tried different ‘Type hint’ values, they fail with different errors.
error


#9

Unfortunately i cannot test in V5, only have GH in V6. But this also works like my first example works if Type hint = Guid:

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc

sc.doc = Rhino.RhinoDoc.ActiveDoc
obj = sc.doc.Objects.Find(x)

def MyGetObjectLayer(obj):
    index = obj.Attributes.LayerIndex
    layer = sc.doc.Layers[index]
    return layer.Name
    
a = MyGetObjectLayer(obj)

Maybe @piac, could post an example using rs.ObjectLayer() i could not get this to work in V6
_
c.


(Tom) #10

this works! You need to feed in an Rhino Object with a “guid” parameter, not an “surface” parameter. Surface, Breps etc won’t have any information about layer, color etc…
Its the RhinoObject who holds an geometric object (GeometryBase)


#11

@TomTom, can you post working code please? I’m just being a dilettante on this but am surprised that a working answer hasn’t been provided for this question yet.

@clement, I get the same error as before using this type hint with a ‘Geo’ param:

All other values for ‘Type hint’ give a different error:
error2


#12

where did you read that you should set your Type hint to Geo ?

_
c.


#13

I meant that I replaced the ‘Srf’ param with a ‘Geo’ param (which is set to a Rhino surface) - separate issue from type hint. As I said, this isn’t my issue, I’m just trying to follow along.


#14

It also works if i do not set any Type hint for x and leave it using “No Type Hint”, but then i have to do the conversion using an ID component:

_
c.


#15

BINGO! How silly.

That also makes the OP’s version work just fine:


python.gh (7.0 KB)

@TomTom, I know you said this too, I just missed it, or didn’t understand it. Thanks.


#16

Good :wink:

_
c.


#17

Doesn’t work for me.

Type hint is no type hint. No type hint works. I can’t choose Guid as type hint.

I don’t understand what am I doing wrong.

(using rhino5)


(Giulio Piacentino) #18

Hi,

this solves it; I had just made a screenshot with similar content (before Windows Update decided to take my next 2 hours)…

layers.gh (8.8 KB)

There is a little more info in the definition. Basically, “Type Hint” does, inside the component, the same as what a ID param + No Hint would do. This is always the case, for any param.

I hope this helps,

Giulio


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


#20

What happens when you open this Rhino file and GH file?
python_get_layer.3dm (29.6 KB)
python_get_layer.gh (7.6 KB)


#21

That didn’t work in Rhino 5. All type hint values with only a Geo param failed until a Guid param was inserted between the Geo and Python components.


(Giulio Piacentino) #22

Please open the file above (layers.gh) in Rhino 5 for a working example. You’ll need, of course, to reset the ID param to an object that exists in the Rhino doc. The Guid type hint did not exist in Rhino 5, it was added in the Rhino 6 WIP. The str type hint can be used, too, but it needs a Guid (not geometry).