How to use the Dictionary Components Node in Python?

How to use the Dictionary Components Node in the Python Node?
image
image

itemsDS = Autodesk.DesignScript.Components(dict) ?

I have tried something like this without success:

dict = {
    "Name":"Herbert", 
    "Nachname":"Maier", 
    "Stadt":"Stuttgart", 
    "PLZ": 71399
    }
# DesignScript Syntax
keysDS = dict.Keys
valuesDS = dict.Values
itemsDS = dict.Components()
# Python Syntax
keysPy = dict.keys()
valuesPy = dict.values()
itemsPy = dict.items()
OUT = keysDS, valuesDS, itemsDS, keysPy, valuesPy, itemsPy

But I get the error:
AttributeError: ‘dict’ object has no attribute ‘Components’

Thanks!

Any reason not to use the built in python dictionary capability? Assuming you are near current build (I think 2.6) the Python dictionary should output as a Dynamo dictionary.

hi @JacobSmall ,
I use atm 2.10
I just was wondering why it doesn’t work :slight_smile: by using the DesignScript Syntax. Maybe I have to import sth special? Or are the action Nodes not supported? What is the reason? I’m just curious
Thanks

Hi @Thomas3,

I think you misunderstand the “DesignScript Syntax” in your Python code. This is actually DotNet Syntax and by calling

dict.Keys
dict.Values

you are not calling the Dynamo Functions (Designscript) but you are calling the DotNet properties of the dictionary

If you want to use the actual Dynamo Functions, you need to reference the right .dll file. But I would not recommend that for Dictionaries, because you can already do so much with the built in dictionary capability, just like @JacobSmall suggested

1 Like

oh ok, thanks @Joelmick for making it more clear!
Let’s just assume I want to use the Components Node in the Python code, for whatever reason. How do you know which dll file I have to reference and import?

Normally I would look at the Dynamo Github Source code to see which Class (Dictionaries, Lists, Lines, Surface etc) is in which Library (.dll).

You can also do this by downloading a dll peeker like DotPeek and you can see what is inside a dll file.

You then just reference the dll into Python like usual and refer to the correct namespace

I don’t know in which library the Dictionary class sits, since I have never used it.

2 Likes