I am looking for the most efficient way to save data from one run of a python script to the next. My script generates a list , say presentList . The list is made up of a number of integers. Next time the script runs I want to use that list again as previousList. I would prefer that this data be written and read from memory rather than disk since my script runs quite often and I don't want to write so often to my disk. Can I use pickle with BytesIO to accomplish this? I am also using Animation Nodes. Perhaps there is a way to send the list to and from Animation Nodes to make the data persistent? Thanks for any advice on this. Perhaps this would be better asked on a python forum.
Asked
Active
Viewed 229 times
2
-
This question seems to get pretty close to yours: https://blender.stackexchange.com/questions/103233/storing-a-general-python-object-in-a-blender-scene/103316#103316 – WhatAMesh Feb 26 '19 at 09:24
-
@brockmann the data is just a list of integers. – Jim Ruxton Feb 26 '19 at 09:27
-
3Possible duplicate of How to store particle locations in a list? – Omar Emara Feb 26 '19 at 11:45
-
Cant you just use a custom attribute? should be memory based. Can be made on a collection or something – Jackdaw Feb 26 '19 at 12:56
-
Thanks @OmarAhmad Tthis may work. I was hoping for an all python solution but if I can do it in animation nondes that would be great too. – Jim Ruxton Mar 01 '19 at 08:13
-
Thanks @Jackdaw . I have never used custom attributes but will look into how to use them. – Jim Ruxton Mar 01 '19 at 08:14
-
@JimRuxton The same principle apply, you may store your variables in bpy for instance. – Omar Emara Mar 01 '19 at 08:22
-
Thanks @OmarAhmad . Using setattr() and getattr() worked perfectly. A very simple solution. Really appreciate the suggestion. – Jim Ruxton Mar 01 '19 at 10:03
1 Answers
1
Based on the comment Omar posted I found that I could simply use the python statements setattr() to save my list and getattr() to retrieve it. In this case I stored the list as an attribute in animation_nodes as shown in Omar's example however I assume I could have stored it in any object in my scene. This allowed me to have data persist between running instances of my python script,
Jim Ruxton
- 313
- 1
- 8
-
Is https://stackoverflow.com/questions/4075190/what-is-getattr-exactly-and-how-do-i-use-it the getattr() you're referring to? – Ron Jensen Aug 01 '21 at 18:23