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()
context.preferences
there is nouser_preferences
(any more) ... The error is telling you this. Go to python console and typeC.user_<TAB>
(whereC is bpy.context
and <TAB> is the tab key to autocomplete) to confirm. $\endgroup$