I tried scripting with Blender but I am stuck on this problem.
I want so change the Scripts Folder from the Blender Preferences (Preferences → File Paths → Scripts )
Is it possible to do this with Python in Blender directly?
Thank You :)
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It only takes a minute to sign up.
Sign up to join this communityPreferences are found in bpy.context.preferences
. Mostly the name of the next field match the name in the tab, roughly. You can check this by going to the python console and typing dir(bpy.context.preferences)
. It will print something like this:
['__doc__', '__module__', '__slots__', 'active_section', 'addons', 'app_template', 'autoexec_paths', 'bl_rna', 'edit', 'experimental', 'filepaths', 'inputs', 'is_dirty', 'keymap', 'rna_type', 'studio_lights', 'system', 'themes', 'ui_styles', 'use_preferences_save', 'version', 'view']
From this we see that filepaths
is the right field. To find the actual name for the scripts directory, make sure you have enabled Developer Extras and Python Tooltips in Preferences → Interface and hover over the Scripts input field. You'll see something like this:
Since we know from above that "PreferencesFilePaths" is represented by bpy.context.preferences.filepaths
We simply add script_directory
:
bpy.context.preferences.filepaths.script_directory
It is a string variable, but remember that in Python backslash is a string escape character, so if you're providing windows filepaths, you have to either use the 'r' form of a string (r"C:\tmp") or escape the backslashes ("C:\\tmp")
Just posting a comment for anyone who stumbles on this post and is working with Blender 3.6+. Updates to the API mean that the user can now set multiple custom script directories and .script_directory has been deprecated in favour of .script_directories. The first directory path in that list (if set) can now be referenced with:
bpy.context.preferences.filepaths.script_directories[0].directory
Alternatively you still reference the default scripts path with:
bpy.utils.user_resource('SCRIPTS')