Complete globals

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
bannerboy
Posts: 0
Joined: Sun Sep 05, 2004 1:18 am

Complete globals

Post by bannerboy » Mon Dec 27, 2004 9:40 pm

Is there any way to share data between scripts. mabe this is a game engine question, but I want to be able to have a bunch of data that's shared between several different scripts.

joeedh
Posts: 31
Joined: Wed Oct 16, 2002 10:30 pm
Contact:

Re: Complete globals

Post by joeedh » Tue Dec 28, 2004 8:47 am

bannerboy wrote:Is there any way to share data between scripts. mabe this is a game engine question, but I want to be able to have a bunch of data that's shared between several different scripts.
Look at the Registry module. Also, I've seen where people have added a var to a module, like for example adding to the Blender module, as such:

Code: Select all

Blender.some_var = blah
However I do not know how well this would work. The Registry is sortof annoying to work with at times, but with all your data in one place it's easier to save.

Hope this makes sense.

joeedh

SamAdam
Posts: 0
Joined: Thu Mar 04, 2004 1:28 pm
Contact:

Post by SamAdam » Wed Dec 29, 2004 2:27 am

create a dictionary (or matrix or something) and pickle it into a file in the blender directory. All of the scripts can pickle and unpickle to and from that file.

this is the real way to do it, I believe. It is easy, and quick.

simply put everything into, say, a known list [[1,2,3],[4,2,3],[4,5,3,5]], and use pickle:

Code: Select all

list = [[1,2,3],[4,2,3],[4,5,3,5]]
f = file(Blender.Get('datadir') + "save.txt","w")
pickle.dump(list, f)
and then to get it out use:

Code: Select all

f = file(Blender.Get('datadir') + "save.txt","r")
print pickle.load(f)

an-toni
Posts: 0
Joined: Wed Mar 17, 2004 5:20 pm

Post by an-toni » Fri Dec 31, 2004 5:09 pm

i think pickle is only really needed when you want to save it permanently.

like said, the Registry is now there for it. before the way was to just associate the objects to some loaded module (in gameengine GameLogic).

that said, at least for larger apps, it's better to avoid having (a large number of) globals and just pass references to containing objects (which i've done via modules in the game engine).

~Toni

theeth
Posts: 500
Joined: Wed Oct 16, 2002 5:47 am
Location: Montreal
Contact:

Post by theeth » Fri Dec 31, 2004 5:25 pm

Python uses references when passing mutable objects around, so that is hardly a problem, no?

(well, technically, immutable objects also reference the same memory space too, but both references are truely independant and one cannot modify the content of the other)

Martin
Life is what happens to you when you're busy making other plans.
- John Lennon

an-toni
Posts: 0
Joined: Wed Mar 17, 2004 5:20 pm

Post by an-toni » Fri Dec 31, 2004 5:58 pm

oh the possible problem with (lots of) globals is not technical, but was just referring to a style / architecture design .. it's often ugly, prone to errors and can become a mess

~Toni

theeth
Posts: 500
Joined: Wed Oct 16, 2002 5:47 am
Location: Montreal
Contact:

Post by theeth » Fri Dec 31, 2004 6:09 pm

Wholeheartly agreed then.
Although, IMHO, a big singleton not correctly structured is even worse.

Also, while we're talking about pickling, I'd like to point out that I have a module base on pickle (or cPickle if present) that gives dictionnary-like access to a data file.
http://www.clubinfo.bdeb.qc.ca/~theeth/Work/dyshelve.py
In case anyone would find that useful.

Martin
Life is what happens to you when you're busy making other plans.
- John Lennon

Post Reply