3
$\begingroup$

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
$\endgroup$
4
$\begingroup$

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__.

$\endgroup$
  • 1
    $\begingroup$ Where should I instantiate the class G? $\endgroup$ – Miguellissimo Oct 15 '14 at 18:02
  • 1
    $\begingroup$ 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, ...). $\endgroup$ – CoDEmanX Oct 15 '14 at 20:42
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.