Solved: Store guid as string using python

rhinopython
python

#1

Hi, I’m fairly new to scripting and even newer to rhino, so my apologies if this is a stupid question.
I’m trying to store a selection of objects inside the User Data of another object. This works nicely with a single object, as in this example:

import rhinoscriptsyntax as rs
# select obj to write to
obj = rs.GetObject("select object")
# get guid to store in user data
objs = rs.GetObject("select objects")
# stores second object's guid in first object's user data
rs.SetUserText(obj, "object", objs)
# just check that the guid was written to user data(the format changes if multiple objects are chosen)
print(rs.GetUserText(obj, "object"))
# select object to get user data from
obj = rs.GetObject("select object")
# select object using guid
print("point selected")
rs.SelectObject(rs.GetUserText(obj, "object"))

but as soon as I try to get it to work for multiple objects, this code stops working. Is it possible to store a list of Guids as User Data string and select the corresponding objects afterwards?
Thanks in advance,
Ivan


#2

are you trying to assign the same list of GUIDs to a few objects? use a for loop

IDs = rs.GetObjects()
Targets = rs.GetObjects()

for i in Targets:
    rs.SetUserText(i,"object",IDs)

#3

Hi Ivan,

Python is a typed language which means each object (or variable) contains something with a certain type. The Python RhinoScript methods rs.SetUserText and rs.GetUserText are intended to store text which is of type string (str).

The methods rs.GetObject and rs.GetObjects both return different types. The first method returns an id which is of type Guid while the second method returns a list of Guid which is of type list.

In order to store a single id or a list of ids as user text (which is of type str), you’ll have to convert that first to str. You can join a list into a large string and split a string into a list. See if below example helps, i’ve added a few comments.

StoreObjectIdsInUserText.py (1.6 KB)

_
c.


#4

Hi Clement,

thanks for the fast answer, I don’t have the chance to test it away from the office, but reading through the file, this seems to do exactly what I was hoping to accomplish and a bit more. I’m going to have to spend a bit more time in the code to understand it completely, but there’s plenty for me to learn there, even aside from storing multiple Guids. Thanks once more!

cheers,
Ivan