11
$\begingroup$

How can I use the python API to find the most reliable location that an add-on can use to store data like images, assets, etc. automatically?

It's important that this location:

  • doesn't change for different versions of blender
  • doesn't get deleted on system reboot like some temp folders
  • is available on every OS

One option I've found is to use bpy.utils.resource_path('USER') which ends in the version number (on windows C:\Users\blah\AppData\Roaming\Blender Foundation\Blender\2.78), but then do a relative jump to the folder above it. Is there a better way?

$\endgroup$
2
  • $\begingroup$ Wrote an answer, but then realized it's not automatic as the user has to set a folder in the addon prefs. This is a one time thing if the user enables addon and saves user prefs. $\endgroup$
    – batFINGER
    Jan 5, 2017 at 17:24
  • 1
    $\begingroup$ Maybe we should add a new key 'DATA' to that function? I've already made a suggestion for CACHE. I might get around to implementing it some day ;-) $\endgroup$
    – dr. Sybren
    Jul 18, 2017 at 11:06

2 Answers 2

4
$\begingroup$

Set it once in the addons User Preferences.

Suggest using a property in addon preferences The user chooses the folder. I had deleted this answer, as it is not "automatic", however setting a storage location once, is IMO, not a huge overhead.

Modify the script in link with a folder property

class ExampleAddonPreferences(AddonPreferences):
    # this must match the addon name, use '__package__'
    # when defining this in a submodule of a python package.
    bl_idname = __name__

    assets_folder = StringProperty(
            name="Assets Folder",
            subtype='DIR_PATH',
            )

    def draw(self, context):
        layout = self.layout
        layout.label(text="This is a preferences view for our addon")
        layout.prop(self, "assets_folder")

enter image description here

And then when you need to access it

    preferences = context.preferences
    addon_prefs = preferences.addons[__name__].preferences

    assets_folder = addon_prefs.assets_folder
$\endgroup$
2
$\begingroup$

What about os.path.expanduser('~')?

From what i read on https://docs.python.org/2/library/os.path.html it should work for all UNIX system and Windows. Moreover it is assured that the user have write permission in it.

$\endgroup$
1
  • $\begingroup$ That does seem like quite a good solution, the only slight disadvantage being it's not in a blender-specific location. For reference, on windows this resolves to C:\Users\you, on linux I assume it's just the home directory. $\endgroup$
    – Greg Zaal
    Aug 15, 2017 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .