Storing variables across scriptlink frames

Hello,

What is the best way to store variables that can save values when using them in a scriptlink that is run each redraw of the screen?

a) is it storing it in the Blender class? ie Blender.myvariable
b) storing it in registry
c) in a file
d) another way?

I am asking because i was storing a dictionary in Blender.dict, and blender crashes when I use the same dictinary variably for both the scriptlink script and my GUI. I was wondering if there was a better way.

Also, do global variables hold their values across scripts? or just fuctions in the same script?

It seems to be a flaw in the script link implementation.
Blender istelf destroys all objects created in the scriptlink script when the frame change event is over.
I have a class that gets destoryed every frame change so on the next frame, my class no longer exists and the init runs everytime.
I would love to get this figured out.
In my opinion, there needs to be an INIT event for scriptlinks. That way you can create your variables in that event then when the Render or Change Frame occurs, you are ready to go.

I hadn’t thought about trying to save my class variable in the registry, I may try that…

as Iv saied afore… its not good paractice but a fast and easy way to do it is

Blender.someVar = foo
and
foo = Blender.someVar

You need to do this in a try exceot since the var wont start off existing. - add it if its not there.

Blender in the CVS has ID properties youll be able to use for this,

These ID properties things sound good :slight_smile: .i’ll try to look them up …

I was using Blender.someVar = …, but in one of my scripts when I used a dictionary eg. Blender.dict = {var1:value1,var2:val2} with a scriptlink and a GUI it was crashing blender.

These crashes led me to think there might be a better way.

Ok ideasman42,
If you attach this script to the Frame Changed script link of the default cube and scrub the timeline, you will see that the except case always runs.


import Blender as B
class myAdder:
 localValue = 0
 def __init__(self):
  self.localValue = 0
 def add_value(self):
  self.localValue = self.localValue + 1
 def get_value(self):
  return (str(self.localValue))
  
try:
 # All other times we should get here.
 myRecoverClass = B.holdMyClass
 myRecoverClass.add_value()
 print(myRecoverClass.get_value())
except:
 # We should get here only the first time.
 # No class exists.
 myClass = myAdder () 
 myClass.add_value()
 B.holdMyClass = myClass
 print ("exception")

It seems that even my Blender variable will fall out of scope in a scriptlink framechange event so I always end up in the except.
What I need is a global place to put my “import Blender as B” line.
Then what you are saying might work. I still can not tell if you can store a class in the Blender object like you suggest?

I think this is the proper behavour for frame changed events. Its to remove mem problems i belive. I have use the some method that alows me to add data to the blender project/file itself. This means the data is there accross saves as well. But it can make the files a bit fat.

I don’t have the code on this machine so i can’t tell you what the method is of the top of my head…