Grasshopper

algorithmic modeling for Rhino

I'm struggling with getting defaultdict (or even just dictionaries) to work within ghpython. I ran this code through the Ironpython interpretor and was able to build and append dictionaries. 

Essentially, I am trying to look at the values coming from two sliders, (one as the key, and one as the value associated with that key), and save them to a dictionary as they come in. Using defaultdict allows me to not need to explicitly define all of the keys in the dictionary right off. However, regardless of whether I use defaultdict or a regular dictionary, I am never able to retrieve/save more than one set of key:value combinations. So my hunch is that I am not handling this correctly in grasshopper. Any thoughts? Thanks -

from collections import defaultdict

recorded_list=defaultdict(list)
recorded_list2 = {}

if bake == True:
    recorded_list[key].append(value)

   try:
       recorded_list2[key] = value
   except KeyError:
       recorded_list2.update({key,value})

Views: 590

Attachments:

Replies to This Discussion

An update...

The issue was not the code so much as the implementation in grasshopper. The issue was that the dictionary was defined in the same python component as it was intended to be appended/edited, causing the dictionary to re-initialize at each change in a slider value. The solution was to "initialize" the dictionary (or list, tried both) as an instance of a class in a separate python component. Then pass the dictionary (or list) by way of passing the entire class with the instantiated object to another python component. 

So, lesson learned.

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