Thanks for the reply Cuq! I actually saw your post but since my plugin doesn't need to use qml I don't believe I could really solve it this way without adding more complexity. I did initially attempt to use QUrl but that was pointless. I'm fairly confident now there's no way to use any file path for the setting parameter because when the theme loads it only obtains icons after confirming the existence of a theme.json file. Then it simply looks for an icons folder using that same path.
However... after spending entirely too much time on this and nearly giving up, I found a solution!
I'll try to break it down for the sake of the next person...
Basically, there must be a folder in the plugin directory with a 'theme.json' and an '/icons/default' directory containing any new icons. I'm using other resources so I simply added a 'themes' folder to a 'resources' directory.
The theme.json can contain any necessary data but must at least include metadata with an "inherits" property. Any name is fine because it'll immediately get changed anyway. Stripping it down this much does ensure Cura will throw an error if it's ever used as a normal theme file.
{ "metadata": { "inherits": "cura-light" } }
Here's my proof of concept to get it working:
# Directory containing theme.json: /resources/themes # Directory containing icons: /resources/themes/icons/default # "icon" param must be the filename without the extension (e.g. Awesome.svg = "Awesome"). # Additional module requried to force a theme to load from a path from UM.Qt.Bindings.Theme import Theme # Path to 'resources' dir resource_path = os.path.join(os.path.dirname(__file__), 'resources') self._updateTheme(resource_path) def _updateTheme(self, theme_path: str) -> None: application = CuraApplication.getInstance() preferences = application.getPreferences() # Get name of current theme or set to default if none exist preferences.addPreference('general/theme', application.default_theme) current_theme_name = preferences.getValue('general/theme') # Path to 'theme.json' resource_theme = os.path.join(theme_path, 'themes', 'theme.json') with open(resource_theme) as f: metadata = json.load(f) # Empty theme used only for metadata metadata['metadata']['inherits'] = current_theme_name with open(resource_theme, 'w') as f: json.dump(metadata, f, indent = 4) # Rewrite with current theme to inherit # Force load inherited theme so Cura will include local resources Theme.getInstance().load(path = os.path.join(theme_path, 'themes'))
The most critical part being the last line which forces the local theme to load. I assume it wasn't intended to be used this way, it would be nice if Cura just honored the items in a plugin's resource path .
This should allow a user to even continue using a custom theme without issue. It could certainly be improved and the theme.json file could even get created by the function so there's no chance of getting deleted.
This is usually about the time I realize fieldOfView had a far more efficient solution to my problem but unless that happens, perhaps the setting parameter documentation should be updated :)
Recommended Posts
Cuq 206
Welcome to the club 🙂
I don't know if it's the solution. But I solved my issue with :
could be a beggining of solution for you
Link to post
Share on other sites