Grasshopper

algorithmic modeling for Rhino

I understand from this post that it is possible transfer a python dictionary from one component to another if the dictionary is wrapped in a custom class.

Following Benjamin Golder's example, I've written a class, ghDefaultDict, in order to use the defaultdict. Similarly, the dictionary itself is at variablename.d

It seems to work, however, about 50% of the time (this happens more often in a complex definition, and especially after unlocking the canvas) the component to which the data is passed returns a completely empty dictionary. For example, these two python components should print the same thing, and they do here, but fairly often, the second will print "KEYS: []" instead.

Any thoughts on why this is or how to fix it?

This file is attached below. The Python_2 component contains only the code:

print("KEYS:")
print(x.d.keys())

Views: 613

Attachments:

Replies to This Discussion

Hi Trevor,

I took a quick look at your definition since I had the same question (passing dictionaries around) a while ago.

I don't get the error you described, but edited the code in your components (mainly your class constructor the sticky component and then few lines of code in 'Python_1'). Hopefully it helps.

       

Attachments:

Thanks for looking. In this case, I can have more than one curve at a given height and want to be able to assign all of those at the same level to the same key: thus defaultdict instead of just dict.

Your version only allows one element per key so the dictionary only contains the pink curves.:

But if I change the class constructor to:

class ghDefaultDict():
   """:ghDefaultDict. ex: myVarName.d=defaultdict(list)"""
   def __init__(self,defD):
   self.d = defD

called like:

crvDict= scriptcontext.sticky['ghDefaultDict'](defaultdict(list))

it works again. I'll let you know if this helps with the main problem of exchanging between script components in a more complex setting or not.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service