2
$\begingroup$

Is there anything more global than a scene that can contain project-related properties that should be saved to each *.blend file?

Since a single file/project can contain any number of scenes, where would an add-on typically store properties that should only exist in one place?

Looking for something like..

bpy.types.SingletonObject.my_properties = bpy.props.PointerProperty(type=MyProjectData)

Edit: For anyone interested in doing this, here are some of the ideas I've found online so far. Except the last one - that one is from me.

  • Store properties in Window Manager - AFAIK, this only works if you don't want to save the data to the *.blend file.
  • Create an empty object and store properties in it. I'm no expert at this, but I believe the empty would need to exist somewhere (a scene). So what happens when the user deletes that scene?
  • Create a custom/dummy ID-block and attach properties to it. I don't completely understand this one yet, but looking into it.
  • This one is something I thought up and am considering using. It is not as simple as the others, but should allow storing global properties in a single location, as well as saving/loading those properties with the *.blend file:

    1. Store all properties in a custom structure that is attached to BOTH the add-on preferences and the scene data. Will refer to this as global_props. So it would be located at bpy.context.preferences.addons["project"].preferences.global_props and bpy.data.scenes[0].global_props. You would only ever reference the one in the add-on preferences except in the steps below..
    2. Setup a bpy.app.handlers.load_post handler. After a file is loaded, use the handler to copy global_props from the scene to preferences.
    3. Setup a bpy.app.handlers.save_pre handler. Right before a file is saved, use the handler to copy global_props from preferences to scene.
    4. Anywhere you deal with global_props, access and use the preferences version.

      Some notes:

      • It may be possible to only attach global_props to the scene right before the file is saved, then delete it after, if you wanted to avoid wasting the memory it would take up. Not sure if this would cause issues or not.
      • Using this setup would allow you to give the user the option to save certain properties locally (*.blend) or globally. It would be very easy to simply avoid transferring some of the data based on certain options. This was a feature I really needed for my export add-on.
      • Let me know if you think of any issues that would stem from using this setup. I'm currently planning to make use of it.
$\endgroup$
6
  • 1
    $\begingroup$ Some of the add-ons that come with blender like rigify assigning some of the properties to the window manager: bpy.types.WindowManager... also you can print a complete list of all available types in the console by using pythons dir() on bpy.types. $\endgroup$
    – brockmann
    Sep 17 '19 at 13:08
  • $\begingroup$ Thanks! I'm surprised Blender doesn't provide a specific type for add-ons to attach global project properties to. There is also this list docs.blender.org/api/2.80/bpy.types.html - but wow there are a lot of them. $\endgroup$
    – Robert
    Sep 17 '19 at 16:53
  • $\begingroup$ Here is a link to another question that is pretty much identical to mine. No real answers, but some helpful bits: blender.org/forum/viewtopic.php?t=27291 - they seem to suggest that properties attached to the window manager do not get saved to file. However, this was back in 2013, so I'm sure some things have changed. And here is one more: blenderartists.org/t/global-property/626010/6 (2014) $\endgroup$
    – Robert
    Sep 17 '19 at 17:09
  • $\begingroup$ I think you can only assign custom properties to subclasses of ID (or Bone or PoseBone). BlendData inherits directly from bpy_struct, so I don't think it is allowed. $\endgroup$
    – Robert
    Sep 17 '19 at 20:04
  • $\begingroup$ Yeah, tested. nope @Robert I'd use the prefs... that's easy to maintain. $\endgroup$
    – brockmann
    Sep 17 '19 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.