Module Registry
[frames | no frames]

Module Registry

The Blender.Registry submodule.

New: GetKey and SetKey have been updated to save and load scripts *configuration data* to files.

Registry

This module provides a way to create, retrieve and edit persistent data in Blender.

When a script is executed it has its own private global dictionary, which is deleted when the script exits. This is done to avoid problems with name clashes and garbage collecting. But because of this, the data created by a script isn't kept after it leaves: the data is not persistent. The Registry module was created to give programmers a way around this limitation.

Possible uses: Example:
 import Blender
 from Blender import Registry

 # this function updates the Registry when we need to:
 def update_Registry():
   d = {}
   d['myvar1'] = myvar1
   d['myvar2'] = myvar2
   d['mystr'] = mystr
   # cache = True: data is also saved to a file
   Blender.Registry.SetKey('MyScript', d, True)

 # first declare global variables that should go to the Registry:
 myvar1 = 0
 myvar2 = 3.2
 mystr = "hello"

 # then check if they are already there (saved on a
 # previous execution of this script):
 rdict = Registry.GetKey('MyScript', True) # True to check on disk also
 if rdict: # if found, get the values saved there
   try:
     myvar1 = rdict['myvar1']
     myvar2 = rdict['myvar2']
     mystr = rdict['mystr']
   except: update_Registry() # if data isn't valid rewrite it

 # ...
 # here goes the main part of the script ...
 # ...

 # if at some point the data is changed, we update the Registry:
 update_Registry()

Notes:

Function Summary
  GetKey(key, cached)
Get key 'key' from the Registry.
  Keys()
Get all keys currently in the Registry's dictionary.
  RemoveKey(key)
Remove the dictionary with key 'key' from the Registry.
  SetKey(key, dict, cache)
Store a new entry in the Registry.

Function Details

GetKey(key, cached=False)

Get key 'key' from the Registry.
Parameters:
key - a key from the Registry dictionary.
           (type=string)
cached - if True and the requested key isn't already loaded in the Registry, it will also be searched on the user or default scripts config data dir (config subdir in Blender.Get('datadir')).
           (type=bool)
Returns:
the dictionary called 'key'.

Keys()

Get all keys currently in the Registry's dictionary.

RemoveKey(key)

Remove the dictionary with key 'key' from the Registry.
Parameters:
key - the name of an existing Registry key.
           (type=string)

SetKey(key, dict, cache=False)

Store a new entry in the Registry.
Parameters:
key - the name of the new entry, tipically your script's name.
           (type=string)
dict - a dict with all data you want to save in the Registry.
           (type=dictionary)
cache - if True the given key data will also be saved as a file in the config subdir of the scripts user or default data dir (see Blender.Get).
           (type=bool)

Warning: as stated in the notes above, there are restrictions to what can be automatically stored in config files.


Generated by Epydoc 2.1 on Thu Jul 13 16:50:04 2006 http://epydoc.sf.net