Registry GetKey/Keys weirdness

I noticed some unexpected behavior with the GetKey and Keys functions:


Dict = Blender.Registry.GetKey('key1')
print Dict

Dict = Blender.Registry.Keys()
print Dict

The first segment retrieves all keys as a dictionary instead of a single key:

{'key1': 12, 'key2': 57, 'key3': 24}

The second segment retrieves only a list of the key names:

['key1', 'key2', 'key3']

Can anyone tell me if this is expected behavior, or if I’m doing something wrong?

Thanks in advance…

How are you assigning the keys into the registry?

Something Like This?:



dictInit = {'key1': 12, 'key2':57, 'key3':24}

Blender.Registry.SetKey('key1', dictInit)
Blender.Registry.SetKey('key2', dictInit)
Blender.Registry.SetKey('key3', dictInit)


If thats the case, then yes that should be the expected output. What you may be trying to do is:


dictInit = {'key1': 12, 'key2':57, 'key3':24}
Blender.Registry.SetKey('keys', dictInit)

keys = Blender.Registry.GetKey('keys')
print keys

keynames = Blender.Registry.Keys()
print keynames

The way the Registry work is that the registry itself is a Dictionary. So when you say set key your saying something similar to:


#This happens internally (essentially)
Registry = {}

#Your Code
dictInit = {'key1': 12, 'key2':57, 'key3':24}

# Equivalent to Registry['keys'] = dictInit
Blender.Registry.SetKey('keys', dictInit)


See the registry is a dictionary of dictionaries, and the SetKey function creates a new key in the dictionary and assigns a dictionary to it. There is a cache option as well, which just saves the Registry values out to a config file.

Thanks for the response.

From your example I seem to be doing it right, I just expected “GetKey()” to retrieve a single key, and “Keys()” to give me the entire dictionary.

It works fine the way it is, but the “Keys()” function seems sort of pointless since the key names and their values are all returned with any “GetKey()” call.

Since “GetKey()” gives up everything, regardless of which key I ask for, I am confused as to why I need to pass a key name to “GetKey().”

It really doesn’t matter in the end, since it works, it just makes the code harder to read.

Thanks again. :cool:

GetKey() does return a single key. The key it returns, however, is the key in the Registry. This key is associated with the dictionary you assign to it via the SetKey() method. You get your original dictionary back.

Keys() allows you to see what keys the registry has stored in it, so when used like below you can see what keys you’ve assigned in.

Ideally, the Registry could/should be used for something like:


meshNames = {dict of mesh names}
Registry.SetKey('names', meshNames)

colors = {dict of colors}
Registry.SetKey('colors', colors)

etc...

This allows you to store data organized well.

Here is what the registry will look like (essentially):


Registry = {'names':{dict of mesh names}, 'colors':{dict of colors}}

What your trying to do though, is more like:


Registry = {'key1': 12, 'key2':57, 'key3':24}

which is not what it was designed to do. Its a dictionary of dictionaries, so it expects you to give it a dictionary to store, not a single integer value.