5

I have two sets of Operators. The first set, lets call it A, produce data. The second set, lets call it B, needs to read this data. What possibilities do I have to store my data from set A, so it is accessable for the ones from B?

Additional info:

  • The type of the data is a dictionary
  • It is not neccessary to store it in the blender file to make it persistend for later use
  • When Operators from A are called multiple times, the data shall be overwritten, so the Operarators from B always gets the latest data
Miguellissimo
  • 1,355
  • 2
  • 16
  • 29

2 Answers2

6

A simple way is to use a global variable, although I don't recommend to make use of the global keyword on a dict directly. I suggest to use a class instead:

class G:
    pass

class OperatorA(...): ...
    G.your_dict = {"foo": "bar"}

class OperatorB(...): ...
    print(G.your_dict)

You may replace pass with class variable declarations if B might try to access it before A sets it:

class G:
    your_dict = {"default": None}

If the data is really related to let's say OperatorA, you may also use that class instead of G. Inside of a method in OperatorA, you can either refer to the class via the explicit name, or via self.__class__.

CodeManX
  • 29,298
  • 3
  • 89
  • 128
  • 1
    Where should I instantiate the class G? – Miguellissimo Oct 15 '14 at 18:02
  • 1
    Nowhere, you use the class itself, not any instance of it. Declare it in the global scope. It will be like a singleton. Classes are first-class citizens, just like functions - you can reference them like any other object (int, float, dict, function, ...). – CodeManX Oct 15 '14 at 20:42
1

Another place you might store a dictionary is in the scene itself.

bpy.context.scene['mydataparams'] = {'value':1}
bpy.context.scene['mydataparams']['value']

It will get stored with the blender file. Whether that's a good thing or a bad thing is up to you.

volvis
  • 709
  • 3
  • 7
  • It shouldn't be serialized if you use bpy.context.window_manager, http://blender.stackexchange.com/questions/8442/trouble-getting-windowmanager-properties-to-save-its-contents-with-blend-file – CodeManX Oct 16 '14 at 11:35