1
$\begingroup$

I am trying to use "AddonPreferences" in order to allow users to select a folder where output files will be written when pressing a button (operator).

When I install the script, I can correctly see the output folder selection in the "Blender Preferences" window, but when I execute the operator via the button in the panel, I get this error:

AttributeError: 'Context' object has no attribute 'user_preferences'

Does anybody know why I get this error? Here is my code:

import bpy
from bpy.props import StringProperty
from bpy.types import Operator, AddonPreferences
 
bl_info = {
    'name': 'HELLO',
    'category': 'All',
    'author': 'user',
    'version': (0, 0, 1),
    'blender': (2, 80, 0),
    'location': '3D_Viewport window -> N-Panel > Test',
    'description': 'Test'
}
 
class writeFileOperator(bpy.types.Operator):
    bl_idname = 'op.write'
    bl_label = 'Write file'
 
    def execute(self, context):
        path = bpy.context.user_preferences.addons[__name__].preferences.name_folder
        f = open(path + 'test_file.txt', "w")
        return {"FINISHED"}
 
 
class writeFilePanel(bpy.types.Panel):
    bl_idname = 'test.panel'
    bl_label = 'TEST'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'TEST'
 
    def draw(self, context):
        self.layout.operator("op.write", text="Write file")
 
 
class saveLocFile(bpy.types.AddonPreferences):
    bl_idname = __name__
 
    name_folder = StringProperty(
            name="Output folder",
            subtype='DIR_PATH',
            )

    def draw(self, context):
        layout = self.layout
        layout.label(text="Where do you prefer to store your output files?")
        layout.prop(self, "name_folder")


def register():
    bpy.utils.register_class(writeFileOperator)
    bpy.utils.register_class(writeFilePanel)
    bpy.utils.register_class(saveLocFile)
 
 
def unregister():
    bpy.utils.unregister_class(writeFileOperator)
    bpy.utils.unregister_class(writeFilePanel)
    bpy.utils.unregister_class(saveLocFile)
 
 
if __name__ == '__main__':
    register()
$\endgroup$
3
  • 2
    $\begingroup$ It's context.preferences there is no user_preferences (any more) ... The error is telling you this. Go to python console and type C.user_<TAB> (where C is bpy.context and <TAB> is the tab key to autocomplete) to confirm. $\endgroup$
    – batFINGER
    Mar 16, 2021 at 13:53
  • 2
    $\begingroup$ Thanks @batFINGER, it works perfectly! I also checked this through the Python console and, as you said, I can find bpy.context.preferences, but I cannot see bpy.context.user_preferences. $\endgroup$
    – Alex Mekx
    Mar 16, 2021 at 14:03
  • 1
    $\begingroup$ NP. Not sure when it changed, just edited same out of blender.stackexchange.com/a/70587/15543 $\endgroup$
    – batFINGER
    Mar 16, 2021 at 14:06

1 Answer 1

1
$\begingroup$

In 2.8+ you use bpy.context.preferences as @batFINGER wrote. Look in https://wiki.blender.org/wiki/Reference/Release_Notes/2.80/Python_API/Preferences_API

$\endgroup$

You must log in to answer this question.

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