2004-01-23 02:59:54 +00:00
|
|
|
# Blender.Registry module
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
The Blender.Registry submodule.
|
|
|
|
|
|
2005-04-21 19:44:52 +00:00
|
|
|
B{New}: L{GetKey} and L{SetKey} have been updated to save and load scripts
|
|
|
|
|
*configuration data* to files.
|
Note: this commit includes new functionality to save and restore scripts configure options. This is ongoing work, scripts still have to be updated to use this feature and more tests are needed, though many have been performed. The new Scripts Config Editor script is the main part of this. If anyone wants to check it, only the AC3D importer and exporter have already been updated to use it: simply open them (you can then cancel with ESC) to have the data created, then try the config editor.
Scripts:
- Thanks Jean-Michel Soler (jms) for updated versions of dispaint, fixfromarmature and unweld (also renamed to remove version part).
- Thanks Bart for the upgraded VRML exporter (great doc webpage!). It is available as VRML 97 and the original VRML 2 is for now still there, to help users testing the new version. For the next release the old one should be removed, of course.
- New script: Scripts Config Editor (Scripts win -> Scripts -> System). Scripts with config options (simple data that is to be set according to user needs or preferences) can use this facility instead of providing a gui and writing config files to disk themselves.
- Added new menu: System, available in the Scripts win.
- Updated sys_info.py, help_browse.py and the AC3D importer and exporter.
- Removed use of the Scrollbar and added arrow keys and mouse wheel support instead in Daniel Dunbar's old doc_browser.py. The scrollbar events handling doesn't exist, Ton suggested removing the scrollbar from the API months ago. For now its ref doc is gone and no bundled script uses it, until we get time to implement it properly.
- Added module BPyRegistry.py with functions to handle reading / writing config files automatically to the scripts/bpydata/config dir.
- Removing dir release/bpydata and its contents (moved earlier to release/scripts/bpydata/)
- Bug #2379: made small changes to bevel_center's ui to fix a problem reported by Alexander Ewering (intrr):
http://projects.blender.org/tracker/?func=detail&atid=125&aid=2379&group_id=9
BPython:
- Thanks Campbell Barton for new functionality: Blender.Get() now can also return all the paths from the user prefs -> file paths win and there is a new function: Blender.sys.expandpath() to transform Blender paths (those starting with '//' and ending with '#') to absolute paths.
- Added function Blender.ShowHelp(), to open the Scripts Help Browser with a given help page -- just a time saver for scripts.
- Improved function Blender.Run() to also work with gui and file select scripts.
- Found a (new?) crash related to NMesh.PutRaw when creating a new object while in edit mode. Leaving / entering edit mode fixes the problem, so a check for obj created, edit mode and leaving / re-entering it were added to the code for now (gdb didn't help much, no backtrace)
- doc updates, including splitting intro page in two, with bpython related stuff (registering / documenting / configuring scripts and command line mode (thanks Chris Want for "use system variables to pass parameters to scripts" idea).
- Registry: functions have been updated to support writing to / reading from disk, for the config editor -- only simple config data supported, for large amounts coders should write to a file themselves. This is done with a new parameter: Registry.GetKey(keyname, True) will also search for the key on the config dir, if not already loaded; equiv. for Registry.SetKey(keyname, dict, True). Data is only written to / read from disk when needed and only scripts already used (assuming they support this functionality) will have config data saved.
2005-04-16 05:25:42 +00:00
|
|
|
|
2004-01-23 02:59:54 +00:00
|
|
|
Registry
|
|
|
|
|
========
|
|
|
|
|
|
|
|
|
|
This module provides a way to create, retrieve and edit B{persistent data} in
|
2005-04-21 19:44:52 +00:00
|
|
|
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:
|
2007-04-22 13:48:40 +00:00
|
|
|
- saving arbitrary data from a script that itself or another one will need
|
|
|
|
|
to access later.
|
|
|
|
|
- saving configuration data for a script: users can view and edit this data
|
|
|
|
|
using the "Scripts Configuration Editor" script.
|
|
|
|
|
- saving the current state of a script's GUI (its button values) to restore it
|
|
|
|
|
when the script is executed again.
|
2004-01-23 02:59:54 +00:00
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
2007-04-22 13:48:40 +00:00
|
|
|
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()
|
2005-04-21 19:44:52 +00:00
|
|
|
|
|
|
|
|
@note: In Python terms, the Registry holds a dictionary of dictionaries.
|
2007-04-22 13:48:40 +00:00
|
|
|
Technically any Python or BPython object can be stored: there are no
|
|
|
|
|
restrictions, but ...
|
2005-04-21 19:44:52 +00:00
|
|
|
|
|
|
|
|
@note: We have a few recommendations:
|
2007-04-22 13:48:40 +00:00
|
|
|
|
|
|
|
|
Data saved to the Registry is kept in memory, so if you decide to store large
|
|
|
|
|
amounts your script users should be clearly informed about it --
|
|
|
|
|
always keep in mind that you have no idea about their resources and the
|
|
|
|
|
applications they are running at a given time (unless you are the only
|
|
|
|
|
user), so let them decide.
|
|
|
|
|
|
|
|
|
|
There are restrictions to the data that gets automatically saved to disk by
|
|
|
|
|
L{SetKey}(keyname, dict, True): this feature is only meant for simple data
|
|
|
|
|
(bools, ints, floats, strings and dictionaries or sequences of these types).
|
|
|
|
|
|
|
|
|
|
For more demanding needs, it's of course trivial to save data to another
|
|
|
|
|
file or to a L{Blender Text<Text>}.
|
2004-01-23 02:59:54 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def Keys ():
|
2007-04-22 13:48:40 +00:00
|
|
|
"""
|
|
|
|
|
Get all keys currently in the Registry's dictionary.
|
|
|
|
|
"""
|
2004-01-23 02:59:54 +00:00
|
|
|
|
Note: this commit includes new functionality to save and restore scripts configure options. This is ongoing work, scripts still have to be updated to use this feature and more tests are needed, though many have been performed. The new Scripts Config Editor script is the main part of this. If anyone wants to check it, only the AC3D importer and exporter have already been updated to use it: simply open them (you can then cancel with ESC) to have the data created, then try the config editor.
Scripts:
- Thanks Jean-Michel Soler (jms) for updated versions of dispaint, fixfromarmature and unweld (also renamed to remove version part).
- Thanks Bart for the upgraded VRML exporter (great doc webpage!). It is available as VRML 97 and the original VRML 2 is for now still there, to help users testing the new version. For the next release the old one should be removed, of course.
- New script: Scripts Config Editor (Scripts win -> Scripts -> System). Scripts with config options (simple data that is to be set according to user needs or preferences) can use this facility instead of providing a gui and writing config files to disk themselves.
- Added new menu: System, available in the Scripts win.
- Updated sys_info.py, help_browse.py and the AC3D importer and exporter.
- Removed use of the Scrollbar and added arrow keys and mouse wheel support instead in Daniel Dunbar's old doc_browser.py. The scrollbar events handling doesn't exist, Ton suggested removing the scrollbar from the API months ago. For now its ref doc is gone and no bundled script uses it, until we get time to implement it properly.
- Added module BPyRegistry.py with functions to handle reading / writing config files automatically to the scripts/bpydata/config dir.
- Removing dir release/bpydata and its contents (moved earlier to release/scripts/bpydata/)
- Bug #2379: made small changes to bevel_center's ui to fix a problem reported by Alexander Ewering (intrr):
http://projects.blender.org/tracker/?func=detail&atid=125&aid=2379&group_id=9
BPython:
- Thanks Campbell Barton for new functionality: Blender.Get() now can also return all the paths from the user prefs -> file paths win and there is a new function: Blender.sys.expandpath() to transform Blender paths (those starting with '//' and ending with '#') to absolute paths.
- Added function Blender.ShowHelp(), to open the Scripts Help Browser with a given help page -- just a time saver for scripts.
- Improved function Blender.Run() to also work with gui and file select scripts.
- Found a (new?) crash related to NMesh.PutRaw when creating a new object while in edit mode. Leaving / entering edit mode fixes the problem, so a check for obj created, edit mode and leaving / re-entering it were added to the code for now (gdb didn't help much, no backtrace)
- doc updates, including splitting intro page in two, with bpython related stuff (registering / documenting / configuring scripts and command line mode (thanks Chris Want for "use system variables to pass parameters to scripts" idea).
- Registry: functions have been updated to support writing to / reading from disk, for the config editor -- only simple config data supported, for large amounts coders should write to a file themselves. This is done with a new parameter: Registry.GetKey(keyname, True) will also search for the key on the config dir, if not already loaded; equiv. for Registry.SetKey(keyname, dict, True). Data is only written to / read from disk when needed and only scripts already used (assuming they support this functionality) will have config data saved.
2005-04-16 05:25:42 +00:00
|
|
|
def GetKey (key, cached = False):
|
2007-04-22 13:48:40 +00:00
|
|
|
"""
|
|
|
|
|
Get key 'key' from the Registry.
|
|
|
|
|
@type key: string
|
|
|
|
|
@param key: a key from the Registry dictionary.
|
|
|
|
|
@type cached: bool
|
|
|
|
|
@param 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 L{Blender.Get}('datadir')).
|
|
|
|
|
@return: the dictionary called 'key'.
|
|
|
|
|
"""
|
2004-01-23 02:59:54 +00:00
|
|
|
|
Note: this commit includes new functionality to save and restore scripts configure options. This is ongoing work, scripts still have to be updated to use this feature and more tests are needed, though many have been performed. The new Scripts Config Editor script is the main part of this. If anyone wants to check it, only the AC3D importer and exporter have already been updated to use it: simply open them (you can then cancel with ESC) to have the data created, then try the config editor.
Scripts:
- Thanks Jean-Michel Soler (jms) for updated versions of dispaint, fixfromarmature and unweld (also renamed to remove version part).
- Thanks Bart for the upgraded VRML exporter (great doc webpage!). It is available as VRML 97 and the original VRML 2 is for now still there, to help users testing the new version. For the next release the old one should be removed, of course.
- New script: Scripts Config Editor (Scripts win -> Scripts -> System). Scripts with config options (simple data that is to be set according to user needs or preferences) can use this facility instead of providing a gui and writing config files to disk themselves.
- Added new menu: System, available in the Scripts win.
- Updated sys_info.py, help_browse.py and the AC3D importer and exporter.
- Removed use of the Scrollbar and added arrow keys and mouse wheel support instead in Daniel Dunbar's old doc_browser.py. The scrollbar events handling doesn't exist, Ton suggested removing the scrollbar from the API months ago. For now its ref doc is gone and no bundled script uses it, until we get time to implement it properly.
- Added module BPyRegistry.py with functions to handle reading / writing config files automatically to the scripts/bpydata/config dir.
- Removing dir release/bpydata and its contents (moved earlier to release/scripts/bpydata/)
- Bug #2379: made small changes to bevel_center's ui to fix a problem reported by Alexander Ewering (intrr):
http://projects.blender.org/tracker/?func=detail&atid=125&aid=2379&group_id=9
BPython:
- Thanks Campbell Barton for new functionality: Blender.Get() now can also return all the paths from the user prefs -> file paths win and there is a new function: Blender.sys.expandpath() to transform Blender paths (those starting with '//' and ending with '#') to absolute paths.
- Added function Blender.ShowHelp(), to open the Scripts Help Browser with a given help page -- just a time saver for scripts.
- Improved function Blender.Run() to also work with gui and file select scripts.
- Found a (new?) crash related to NMesh.PutRaw when creating a new object while in edit mode. Leaving / entering edit mode fixes the problem, so a check for obj created, edit mode and leaving / re-entering it were added to the code for now (gdb didn't help much, no backtrace)
- doc updates, including splitting intro page in two, with bpython related stuff (registering / documenting / configuring scripts and command line mode (thanks Chris Want for "use system variables to pass parameters to scripts" idea).
- Registry: functions have been updated to support writing to / reading from disk, for the config editor -- only simple config data supported, for large amounts coders should write to a file themselves. This is done with a new parameter: Registry.GetKey(keyname, True) will also search for the key on the config dir, if not already loaded; equiv. for Registry.SetKey(keyname, dict, True). Data is only written to / read from disk when needed and only scripts already used (assuming they support this functionality) will have config data saved.
2005-04-16 05:25:42 +00:00
|
|
|
def SetKey (key, dict, cache = False):
|
2007-04-22 13:48:40 +00:00
|
|
|
"""
|
|
|
|
|
Store a new entry in the Registry.
|
|
|
|
|
@type key: string
|
|
|
|
|
@param key: the name of the new entry, tipically your script's name.
|
|
|
|
|
@type dict: dictionary
|
|
|
|
|
@param dict: a dict with all data you want to save in the Registry.
|
|
|
|
|
@type cache: bool
|
|
|
|
|
@param 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
|
|
|
|
|
L{Blender.Get}).
|
|
|
|
|
@warn: as stated in the notes above, there are restrictions to what can
|
|
|
|
|
be automatically stored in config files.
|
|
|
|
|
"""
|
2004-01-23 02:59:54 +00:00
|
|
|
|
|
|
|
|
def RemoveKey (key):
|
2007-04-22 13:48:40 +00:00
|
|
|
"""
|
|
|
|
|
Remove the dictionary with key 'key' from the Registry.
|
|
|
|
|
@type key: string
|
|
|
|
|
@param key: the name of an existing Registry key.
|
|
|
|
|
"""
|