I am coding a node for animation nodes. I need to initialize a set of variables so I can use them, but I need to do it in such a way that they won't be refreshed (reset to the default value) every time I send an output to the node.

I probably want to define the variables before my create(self) method, so it won't refresh every time I send/receive I/O from the execute(self,a,b,...) method.

I have this:

enter image description here def create(self) new.Input(...) new.Input(...) new.Output(...) etc...

def execute(self,a,b,etc..)
   loops involving variables.

Where do I define my variables, and what is the syntax for calling them to check or change values without resetting them to their default start settings?


You can define such variable outside of the node class and call it as you would call a global variable. Take the Text File Reader node for example, it defines a cache dictionary to cache file reads, the cache is initially defined outside of the class as:

cache = {}

class TextFileReaderNode(bpy.types.Node, AnimationNode):
    bl_idname = "an_TextFileReaderNode"
    bl_label = "Text File Reader"

And it assign and read the cache in the execute function normally:

with open(path, "r", encoding = encoding) as f:
    data = f.read()
    cache[key] = (lastModification, data)

You can also use the node identifier as a key in the dictionary to have a per-node variables. For instance, if you want a variable xBounce for every node, create an empty dictionary called xBounce and in the execution function, set its value using xBounce[self.identifier] = something and retrieve its value using xBounce.get(self.identifier, 0) where zero is the default value

  • $\begingroup$ I tried declaring three integer variables before the node itself, as you have shown here, but I still get a referenced before assignment error. I don't know how to show my code here in the comments, or I would add that to this question." Can you suggest what is going wrong? $\endgroup$
    – NullNaught
    Nov 29 '18 at 13:31
  • $\begingroup$ @NullNaught Please add the code to the question. $\endgroup$
    – Omar Emara
    Nov 29 '18 at 14:30
  • $\begingroup$ When I try to copy and paste, I get "866 characters too long." I don't know how to append my screenshot. I will try adding it as an edit to my question. $\endgroup$
    – NullNaught
    Nov 29 '18 at 14:40
  • $\begingroup$ @NullNaught It seems Python assumes the variable is in the local scope. You should probably declare it as a global variable, see this answer. $\endgroup$
    – Omar Emara
    Nov 29 '18 at 15:13
  • $\begingroup$ Can you tell me, is there any way to achieve the same thing without a global variable? I can't use two such nodes at the same time, when I would like to be able to. $\endgroup$
    – NullNaught
    Nov 30 '18 at 7:47

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.