Hello, I am writing an export plugin that I would like to customise easily. This includes changing the icon used. The icon file name is set at self.icon in the settings method of the principal plugin class. However, I can only set the file name, not a path.
Ideally, I would like to keep my icons in a separate directory. I tried setting something like self.icon = "img/icon" instead of self.icon = "icon", but that didn’t work. Any tips? Thanks!
You can load the image manually as an NSImage and set the name of the image to something specific (best to prefix the name with your plug-in’s name). Then, provide that name.
I’m not sure I understand your answer correctly. In case I do, I think you misunderstood my question: I want to set a path to my icon file, not the icon name.
Currently, it’s not a problem to do the following:
self.icon = "my_icon_name"
However, it’s not possible to reference this icon name if it’s in a different directory, like this:
self.icon = "path/to/my_icon_name"
Is this a limitation of the file format plugin, or am I missing something?
The way that API works is it’s using the name of an image for the icon.
If the system finds no image for that name, it looks for image files with that name. This file name cannot contain a slash. So, the solution is to register an image name manually, loading the file from the correct paths yourself.
I might be wrong about some specifics, but my reading of the code that handles this tells me that registering an image by name manually should work.
Edit: fixed that the image needs to be loaded manually also from the path.
Setting image.name registers it with NSImage and it can be accessed by NSImage.imageNamed_(). You just need to store the image in an instance variable to make sure it stays around. For images in the main bundle you can use the file name and NSImage will find it. But for plugins, that doesn’t work.
The FileFormat Plugins icon code is a bit strange. Didn’t look at it in a while. You can only set the image by self.icon = icon_name and the image needs to be next to the plugin.py file.
I improved the code to be more flexible. Will be better wit the next version.
For easier file management. I have my plugin set up in a way that I set a config name, and it then loads with custom options for everything, including a different icon. Having 6 icons in addition to 6+ python files in the Resources folder gets quite cluttered.