Storing Data in a Project



  • On 03/02/2014 at 12:45, xxxxxxxx wrote:

    This is more of a general C4D scripting question but I'm working in Python so I thought this would be the most appropriate place to ask.

    When I run my script I want to store the editor and render visibility states of every object in the document.  Later I want to be able to return to those stored states.

    In researching how to do this it seems that using the c4d.BaseContainer object's SetData(id, data) and GetData(id) methods would be the way to go but I'm a little hazy on the actual details of implementing it.

    My current thinking is that I should store the data that pertains to the object in that object.  For example, if operating on just one object:

    > bc = op.GetDataInstance()
    > bc.SetData(my_id, op.GetEditorMode())
    >
    The above raises a couple of questions though:

    1. When is it preferable to use GetDataInstance() versus GetData()?
    2. It appears as though it is not possible to store tuples, dicts or lists when using SetData() (which, while disappointing, does make sense!).  Is the proper process to create a sub-container and use it as I would a dictionary for the required values?
    3. It's my understanding that if the user saves, closes the file and re-opens it the data stored in a BaseContainer will still be there, is that correct?

    Thanks for your time.  Just getting started with Python and C4D scripting so any help you can provide would be much appreciated :)

    >



  • On 03/02/2014 at 13:23, xxxxxxxx wrote:

    Hi darby and welcome to the PluginCafe!

    1. As you can read in the documentation, GetData() will return a copy  of the node's container
      while GetDataInstance() returns the container that the node actually uses. You should prefer
      GetData() only if you want to make changes to the container without affecting the node's
      properties.
    2. It is not possible to store arbitrary Python objects in a container. You can use serialization
      (eg. JSON, repr(), marshall, pickle) and store the data as a string in the container or, as you
      already noted, a sub-container if that fits your needs.
    3. Yes, all data stored in the container will persist even after the document was closed.

    Regarding your "my_id", just make sure it's a unique ID you obtained from the PluginCafe. You are,
    however, free to use a Plugin ID that you used for registering a plugin since they will not collide
    within the context of the container.

    Best,
    -Niklas



  • On 03/02/2014 at 21:38, xxxxxxxx wrote:

    Thanks for the help Niklas!

    I've made some good progress today although I can't help but think I'm not following best practices.  I'll definitely post some code once I have it working. Hopefully you kind folks will tear it to shreds so that I can build it better :)

    There's one small implementation detail for the script I'm writing that I can't seem to work around.  I'd like my script to have 2 different behaviors (which I'll call 'A' and 'B').  On first run and every other run after (run 1, 3, 5, etc.) I want behavior A while on the second, fourth, sixth, etc. run I want behavior B.

    My intuition says that I need to set a flag that is stored beyond the life of the script, check that flag to see which behavior should occur and then toggle that flag between states at the end of every run.

    The problem is that I can't figure out how to reliably set a flag that will exist beyond the short life of each script run.  I tried storing it in a new BaseContainer but it would appear that these don't exist in memory beyond the script unless they are associated with a C4D object.  Any suggestions?

    Much obliged,
    Darby



  • On 24/04/2014 at 12:57, xxxxxxxx wrote:

    Hey Darby,

    You can try saving that data in the active document's BaseContainer.

    -Donovan


Log in to reply