How I can set the Value as Layer name of the object?

How I can write the Layer name of the objects as a Value of the Key “Material” in the Attributes User Text of the objects?

I do found this piece of code that works but i don’t know how I can get the Layer Name and pass-it into the “Layer_name” value of the script.

import rhinoscriptsyntax as rs
obj = rs.GetObject("Select object")
if obj:
    rs.SetUserText( obj, "Material", "Layer_name" )

P.S. Also I do want to modify this script to accept preselected objects. Best regards.

maybe this?

import rhinoscriptsyntax as rs
obj = rs.GetObject("Select object")
if obj:
    rs.SetUserText( obj, "Material", rs.ObjectLayer(obj))

Unfortunately it is not working with multiple objects. I would like to add the capability to work also with preselected objects.

simply allow preselection, and make a loop:

import rhinoscriptsyntax as rs

def set_usertext(obj):
    rs.SetUserText( obj, "Material", rs.ObjectLayer(obj))


objs = rs.GetObjects("Select objects", preselect=True)

if objs:
    for obj in objs:
        set_usertext(obj)

1 Like

Thank you. It is working now.