My question is about maintaining per-node instance state without the use of Properties. I though that standard Python instance variables would work, but it looks to me like a different node instance is invoked for each execute(). I see the use of properties in the existing nodes, but is there a way that I can use Python dictionaries instance variables instead? Thanks.


Generally it is not a good idea to store the state of a node in Animation Nodes. The execution of a node should not depend on the executions before. This is also important because nearly all nodes have to work inside of loops - so they can be executed many times with different input in the same node tree execution.

Beside that I agree that it is sometimes useful to store some information (cache data etc.) which cannot be stored in Blenders properties (bpy.props). You cannot use the python dictionary of a node as you noticed. However, it is possible to create a dictionary in the module (file) that contains the node. You can use the identifier of a node (myNode.identifier) as key in the dict. Animation Nodes ensures that each identifier only exists once. The disadvantage of this approach is that the data will not be stored in the .blend file. For eg caching that is no problem though.

This is a 'problem' that is caused by Blender. Animation Nodes (or other addons) cannot change this behavior. Personally I don't care that I can't use the instance dictionary. Maybe it is even better the way it is because so we cannot confuse the data that is stored in the .blend file and other variables.

  • $\begingroup$ I understand about nodes being stateless, but in fact I am caching a result that I think is currently wasting many processor cycles by recreating every evaluation. As far as caching in the module, can you provide an example of one of the current nodes that does something similar? Thanks! $\endgroup$ – MikeCh Jul 16 '16 at 22:20
  • $\begingroup$ Sometimes you don't have to implement caching yourself. The Invoke Subprogram node has a simple caching mechanism in the advanced settings. Here is an example where I used caching (not with the identifier though): github.com/JacquesLucke/animation_nodes/blob/master/nodes/text/… $\endgroup$ – Jacques Lucke Jul 16 '16 at 22:24
  • $\begingroup$ Store the dictionary on a class variable. Duh, that was obvious. Thanks again and thanks for all your efforts. Mike $\endgroup$ – MikeCh Jul 16 '16 at 22:27

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.