Get user string doubs

Hello,
I have a question with scripting when assigning a userString to a mesh. I leave two scripts but only one works.

#This Works
import Rhino
import scriptcontext as sc
go = Rhino.Input.Custom.GetObject()
go.SetCommandPrompt("Select Mesh")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Mesh
go.GetMultiple(1, 0)
objRefs = []
if go.CommandResult()==Rhino.Commands.Result.Success:
    objRefs = go.Objects()
go.Dispose()

for objRef in objRefs:
mesh = objRef.Mesh()

textString = "This is a Mesh"
meshCopy = mesh.Duplicate()
meshCopy.SetUserString("key",textString)
sc.doc.Objects.AddMesh(meshCopy)
userText = meshCopy.GetUserString("key")
print userText

With this other form I can not do the same

import Rhino
import scriptcontext as sc
go = Rhino.Input.Custom.GetObject()
go.SetCommandPrompt("Select Mesh")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Mesh
go.GetMultiple(1, 0)
objRefs = []
if go.CommandResult()==Rhino.Commands.Result.Success:
    objRefs = go.Objects()
go.Dispose()

for objRef in objRefs:
mesh = objRef.Mesh()

textString = "This is a Mesh"
meshCopy2 = mesh.Duplicate()
#This part donĀ“t Works
atributtes = Rhino.DocObjects.ObjectAttributes()
atributtes.SetUserString("key2",textString)
sc.doc.Objects.AddMesh(meshCopy2,atributtes)
userText = meshCopy2.GetUserString("key2")
print userText

Any suggestions? Please help me.

SetGetUserString.3dm (2.5 MB)

Hello @cristian.donaire.roj,

In Rhinocommon there are conceptually two main elements that make up every object in the Rhinodoc

You can assign your UserString to either one of those, like you do in your two examples.
If you want to get back the UserString in your second example, you have to get it from the attributes like this:

atributtes = Rhino.DocObjects.ObjectAttributes()
atributtes.SetUserString("key2",textString)
sc.doc.Objects.AddMesh(meshCopy2,atributtes)
userText = atributtes.GetUserString("key2")

Also a little side note:
Your get function lets you select multiple meshes, but then you just take one f those and assign it to your mesh variable. Also, if your get function is escaped by pressing ESC or not selecting anything, your script will crash, try to set up your get function like so:

def myGetOneMeshFunction():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt("Select Mesh")
    go.GeometryFilter = Rhino.DocObjects.ObjectType.Mesh
    go.Get()
    if go.CommandResult() != Rhino.Commands.Result.Success:
        return go.CommandResult()
    
    mesh = go.Objects()[0].Mesh()
    go.Dispose()

    # Do the rest of your logic here...

if __name__ == "__main__":
    myGetOneMeshFunction()