[Bf-python] global dictionary

Willian Padovani Germano wgermano at ig.com.br
Thu Aug 7 21:16:22 CEST 2003


On Thu, 2003-08-07 at 14:15, Stephen Swaney wrote:
(...)
> In ( at least one of ) the original python implementations
> there was a single dictionary for the interpreter.  Now
> it looks like each script runs as __main__ with a new
> interpreter/dictionary.

True, that's it.

> it *seems* that
> once this flag is set, it applies to *all* subsequent script
> executions.  Or at least until an uncaught exception occurs,
> at which time the global dictionary is trashed.
> 
> Could someone confirm this?

(Blender.ReleaseGlobalDict(bool)).  Yes, confirmed.  This was an early
test, I've thought of a better method already and it should be in 2.29,
I hope.

It applies to all scripts until you toggle its state again.

> Also, if there is a better way of maintaining persistant
> data between script executions, I would be interested in
> hearing about it. ( Using pickle to save/restore the good stuff?)

As I said, there will be.  The plan is this:

Instead of using a fresh global dict everytime, we'll clean the current
one of "almost" all stuff added to it after a script runs.

The only thing this dict will keep besides what a fresh one has is a
special dictionary.  We need a good name for it, but for now let's call
it _Blended.

In your script you save all your persistent data (things you want to
"remember") in a dict -- let's say your script is called ProjectX.py and
is in its 1.0a version: you put all your data in a dict called:

ProjectX_1_0a = {'dir': "/path/to/dir", 'mode': 2, 'export_raw': 0}

and put this ProjectX_1_0a dict in the _Blended dict:

_Blended['ProjectX_1_0a'] = ProjectX_1_0a

Everything inside the _Blended dict will be preserved.  And later we may
add a way to save it to disk, so that future sessions can recover that
data, too.

In your script, you can check for an entry in the _Blended dict and, if
there, you get your globals from there, if not (first run), you start
with defaults and in the end save the values to the _Blended dict.

Including the version in the name is a flexible way to avoid
compatibility errors, of course.  If we implement "saving to disk", your
new script versions can erase any older version entries found there, if
you want to.

Using pickle, etc. won't be a good idea in general, unless we end up
including its module in the Blender package itself (if possible), since
not all users have "full" Python installations.

--
Willian, wgermano at ig.com.br




More information about the Bf-python mailing list