Inconsistent behavior of bpy.utils.user_resource( ) function #62736

Closed
opened 2019-03-19 09:05:47 +01:00 by Rainer Trummer · 11 comments

Operating system: Windows-10-10.0.17134 64 Bits
Graphics card: Quadro P4000/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 412.16

Blender Version
Broken: version: 2.80 (sub 50), branch: master, commit date: 2019-03-19 06:43, hash: 4db7842a72
Worked: unknown, 2.79 latest shows same behavior

Short description of error
As a few times before, this is in conjunction with having a custom user_scripts path via environment variables BLENDER_USER_CONFIG and BLENDER_USER_SCRIPTS. When using the bpy.utils.user_resource( ) function for an own add-on, I came across something strange: Passing only the resource type (for instance 'SCRIPTS') to it yields the correct location of my scripts. Passing the optional sub-directory suddenly defaults back to the ones in C:\Users\ME\AppData\etcetc. See output from Blender Python console:

>>> bpy.utils.user_resource('SCRIPTS', 'test')

'C:\\Users\\ME\\AppData\\Roaming\\Blender Foundation\\Blender\\2.80\\scripts\\test'

>>> bpy.utils.user_resource('SCRIPTS')

'D:\\_PREFS_REFACTOR_\\_28_\\UserConfigs\\..\\scripts\\'

>>> bpy.utils.user_resource('CONFIG', 'test')

'C:\\Users\\ME\\AppData\\Roaming\\Blender Foundation\\Blender\\2.80\\config\\test'

>>> bpy.utils.user_resource('CONFIG')

'D:\\_PREFS_REFACTOR_\\_28_\\UserConfigs\\ME\\'

This makes the creation of shared custom presets for my Add-on operator impossible for me, as the preset never lands on the network drive.

Exact steps for others to reproduce the error

  • Set Blender User Scripts path and User Config path to a custom location first (on Windows, create environment variables BLENDER_USER_CONFIG and BLENDER_USER_SCRIPTS and assign path locations to it).
  • Open Blender, go to Python console, test the output of the commands bpy.utils.user_resource('SCRIPTS', 'test') and bpy.utils.user_resource('SCRIPTS').
Operating system: Windows-10-10.0.17134 64 Bits Graphics card: Quadro P4000/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 412.16 **Blender Version** Broken: version: 2.80 (sub 50), branch: master, commit date: 2019-03-19 06:43, hash: `4db7842a72` Worked: unknown, 2.79 latest shows same behavior **Short description of error** As a few times before, this is in conjunction with having a custom user_scripts path via environment variables `BLENDER_USER_CONFIG` and `BLENDER_USER_SCRIPTS`. When using the `bpy.utils.user_resource( )` function for an own add-on, I came across something strange: Passing only the resource type (for instance `'SCRIPTS'`) to it yields the correct location of my scripts. Passing the optional sub-directory suddenly defaults back to the ones in `C:\Users\ME\AppData\etcetc`. See output from Blender Python console: ``` >>> bpy.utils.user_resource('SCRIPTS', 'test') 'C:\\Users\\ME\\AppData\\Roaming\\Blender Foundation\\Blender\\2.80\\scripts\\test' >>> bpy.utils.user_resource('SCRIPTS') 'D:\\_PREFS_REFACTOR_\\_28_\\UserConfigs\\..\\scripts\\' >>> bpy.utils.user_resource('CONFIG', 'test') 'C:\\Users\\ME\\AppData\\Roaming\\Blender Foundation\\Blender\\2.80\\config\\test' >>> bpy.utils.user_resource('CONFIG') 'D:\\_PREFS_REFACTOR_\\_28_\\UserConfigs\\ME\\' ``` This makes the creation of shared custom presets for my Add-on operator impossible for me, as the preset never lands on the network drive. **Exact steps for others to reproduce the error** - Set Blender User Scripts path and User Config path to a custom location first (on Windows, create environment variables `BLENDER_USER_CONFIG` and `BLENDER_USER_SCRIPTS` and assign path locations to it). - Open Blender, go to Python console, test the output of the commands `bpy.utils.user_resource('SCRIPTS', 'test')` and `bpy.utils.user_resource('SCRIPTS')`.
Author

Added subscriber: @RainerTrummer

Added subscriber: @RainerTrummer
Author
The thing came up while following a given answer on StackExchange https://blender.stackexchange.com/questions/134613/can-python-operator-presets-be-shared-between-operators/134620?noredirect=1#comment231376_134620
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

Confirmed, checking...

Confirmed, checking...
Member

Added subscriber: @ideasman42

Added subscriber: @ideasman42
Member

Seems to be working if the folder actually exists.
Not sure about the logic in bpy_user_resource / BKE_appdir_folder_id_user_notest [responsible for falling back to get_path_user instead of get_path_environment if the combined path doesnt exist...]

There is also following comment there:

same logic as BKE_appdir_folder_id_create(), but best leave it up to the script author to create

so it seems to be designed to leave it up to the user to create the directory first...
imo this could/should be changed -- making it consistent like the report suggests...

will look into implementing this (unless @ideasman42 has objections?)

Seems to be working if the folder actually exists. Not sure about the logic in `bpy_user_resource` / `BKE_appdir_folder_id_user_notest` [responsible for falling back to `get_path_user` instead of `get_path_environment` if the combined path doesnt exist...] There is also following comment there: > same logic as BKE_appdir_folder_id_create(), but best leave it up to the script author to create so it seems to be designed to leave it up to the user to create the directory first... imo this could/should be changed -- making it consistent like the report suggests... will look into implementing this (unless @ideasman42 has objections?)

Looking into this, from what I can tell the report is that the environment variable should take priority over the users scripts directory when neither exist.

While a reasonable suggestion this isn't a bug.

Note that creation is handled by bpy.utils.user_resource(resource_type, path="", create=False).

Allowing create=False is useful since you can know if the directory doesn't exist and not attempt to read it's content.


Any users of this API that want to use the first scripts path found (environment var) can use os.path.join(bpy.utils.user_resource('SCRIPTS'), 'test').

Setting as TODO.

Looking into this, from what I can tell the report is that the environment variable should take priority over the users scripts directory when neither exist. While a reasonable suggestion this isn't a bug. Note that creation is handled by `bpy.utils.user_resource(resource_type, path="", create=False)`. Allowing create=False is useful since you can know if the directory doesn't exist and not attempt to read it's content. --- Any users of this API that want to use the first scripts path found (environment var) can use `os.path.join(bpy.utils.user_resource('SCRIPTS'), 'test')`. Setting as TODO.
Member

@ideasman42 : thing is that even if the directory specified in BLENDER_USER_CONFIG environment variable exists, it will fallback to get_path_user if the specified subfolder doesnt exist.
(this is because BKE_appdir_folder_id_user_notest does indeed test if the directory exists indirectly, so calling it _notest is pretty much nonsense...)

@ideasman42 : thing is that even if the directory specified in `BLENDER_USER_CONFIG` environment variable exists, it will fallback to `get_path_user` **if the specified subfolder doesnt exist**. (this is because `BKE_appdir_folder_id_user_notest` does indeed test if the directory exists indirectly, so calling it `_notest` is pretty much nonsense...)
Author

@ideasman42 bpy.utils.user_resource(resource_type, path="", create=False) is indeed useful. The bug report however tackles a case which is buried within the AddPresetBase class. There, bpy.utils.user_resource(resource_type, path="somesubpath", create=**True**) is called. The subpath IS then created as it should be, but not within the path coming from get_path_environment. The location it ends up in is unexpected and undesired from a user's side.

@ideasman42 `bpy.utils.user_resource(resource_type, path="", create=False)` is indeed useful. The bug report however tackles a case which is buried within the `AddPresetBase` class. There, `bpy.utils.user_resource(resource_type, path="somesubpath", create=**True**)` is called. The subpath IS then created as it should be, but not within the path coming from `get_path_environment`. The location it ends up in is unexpected and undesired from a user's side.

This issue was referenced by fe7c7d2820

This issue was referenced by fe7c7d2820f84b815eec5d9c322c7192f33b04b8

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#62736
No description provided.