How to "permanently" store data?
-
Hi guys,
as the title already suggests I wonder how we are supposed to permanently store data. With permanently I mean storing data from for example a layer that will still be accesible after a document was closed and opend again.
I know that we can store data in a nodes
c4d.BaseContainer
. But doing so I found that one can not simply store whatever python object one likes in it. For example the dictionary one gets fromlayer.GetLayerData(doc)
can not be stored in the container as it is, right?So, am I really supposed to put every single attribute of given dictionary as a single entry in a
c4d.BaseContainer
or is there a simpler/better way to do this?And how would one store a nodes unique id (
C4DAtom.FindUniqueID(c4d.MAXON_CREATOR_ID)
) along with the data in that container? As I learned that this unique id is the safest and only way to identify a certain node.Cheers,
Sebastian -
Hello @herrmay,
Thank you for reaching out to us. While I understand the general gist of your question, you might want to clarify what you mean by 'permanently'.
Do you want to persistently store data, i.e., serialize it to disk? The native solution would be HyperFile for that (which is just a way to serialize and deserialize
BaseContainer
; there are some differences, but they are not huge). Or do you just want to store data in memory, which is trivial - just keep things in memory? You say:I mean storing data from, for example, a layer that will still be accessible after a document was closed and opened again.
Getting the data container will meet that requirement. But I guess the implicit requirement is that you want to keep the node alive which is representing the layer, so that when one closes and re-opens its document, that it yields that (possibly) new state. That is not possible, if you want to access data of nodes over reallocation boundaries, markers, specifically the
MAXON_CREATOR_ID
UUID, are the only way.It might be easier if you tell us what you want to do concretely, so that we can look for a concrete solution together. If you want just an abstraction layer for layers, i.e., something which allows you to treat a set of layer objects consistently as "a set of layers", not matter how often Cinema 4D reallocates them in the background, you could follow this template here for materials: MaterialCollection example.
Cheers,
Ferdinand -
Hello @ferdinand,
my goal here is to store layer states of a document so that I can check if some attribute of a layer changed, restore or update a state.
At first I thought I simply write the layers dictionary
layer.GetLayerData(doc)
as a json file to disk. But that appears to me as the wrong way to go. Since then I quickly would end up with a lot of files on the disk for every document I want to store layer states from.Thats the reason why I then thought about storing a reference of each layer along with its dictionary in a
c4d.BaseContainer
and store those containers again in the documents container so that it gets saved automatically by c4d.While this will probably work, it feels cumbersome.
Cheers,
Sebastian -
Hello @herrmay,
Yes, it can be a bit cumbersome to write a whole abstraction layer, but most of the time it is avoidable to do that in the first place. In your case it should be pretty simple.
- Iterate over all layers you want to save to disk. Get the
MAXON_CREATOR_ID
UUID of each layer and its data container. Write the UUID and the data container to disk using JSON orHyperFile
, I would recommend the latter as it will be less work. You only must convert the UUID bytes to a string, put the string into the file, then the data container and you are done. If you want to, you could also store all layer data in one file per document, or just have one giant file for all documents. - Later load these hyper file fragments back and do what you want with the data. When you need the original node, traverse the layers in the active document for a node with the stored UUID. You could also make things fancier and search in all open documents or even store the document path in your serialized data and load that document.
Saving things in the document container is a possibility too, you should make sure though to use a plugin ID for that.
Cheers,
Ferdinand - Iterate over all layers you want to save to disk. Get the