Access Component values from external Python script

rhinopython
python

#1

Hello Scripters,

i am trying to access a components values with a python script which is not running in the GHPython component but from the Python Script editor. The component i try to access is a Guid component which is holding some Rhino Object Ids.

I can get the opened Grasshopper document and find the component using:

found_items = document.FindObjects(search_list, 1)
if found_items:
     component_obj = found_items[0]

and i can read the object ids properly using this:

print "Assigned ObjectIds to '{}'".format(component_obj.NickName)
for obj_id in component_obj.VolatileData:
    print obj_id

But how can i change the ids, or set my own to the component ? With some help from @djordje i’ve been able to perform this:

gh_path = Grasshopper.Kernel.Data.GH_Path(0)
gh_guids = [ Grasshopper.Kernel.Types.GH_Guid(obj_id) for obj_id in obj_ids]
success = component_obj.AddVolatileDataList(gh_path, gh_guids)
print "Success:", success
mydoc.NewSolution(True) 

which prints True, and i can also list that my object ids got assigned. But i do not see that in the Grasshopper GUI. What is the save way to do it right ?

thank you
c.


#2

Actually, this seems to work:

for obj_id in obj_ids:
    gh_guid = Grasshopper.Kernel.Types.GH_Guid(obj_id) 
    component_obj.PersistentData.Append(gh_guid)
    
component_obj.ExpireSolution(True)

_
c.


(Giulio Piacentino) #3

I think the safest way would be to populate the sticky dictionary (scriptcontext.sticky), then read the data from the other Python instance that you are using. This way, you will not depend from the Grasshopper SDK.

Thanks,

Giulio


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


#5

Thank you Giulio. So far it seems to work reliable. I only needed to keep track that i do not add object Ids twice to the Guid component, so i first read out what is assigned, then prompt the user for new object Ids while limiting the selection to object Ids which are missing in the component.

_
c.


(Giulio Piacentino) #6

OK, just do what you think it’s best!