2
$\begingroup$

First of all, I'm not a programmer. This is my first time writing code so understand if this question is dumb. So basically this addon I'm making creates a custom node group in the compositor for some effects but I need to open an image with the 'Image Node'. I'm able to do it with Absolute Paths specific to my computer but I don't know how to then distribute my add-on. Is there a way to open images with relative paths once the addon is installed in Blender?

English is not my native language so tell me if I'm not clear :) and thanks in advance

$\endgroup$
1
  • $\begingroup$ You can distribute images with your addon, you just need to store them in the zip file and you can access them from the root folder $\endgroup$
    – Gorgious
    Feb 14, 2022 at 15:44

1 Answer 1

2
$\begingroup$

Rather than rely on relative paths, Blender has a mechanism for constructing paths relative to add-on installations. Here's one way to use it.

When you create your add-on, give it a subdirectory (folder) with a name like assets. Put the image in that subdirectory.

When you create the zip file that contains the add-on, make sure to include subdirectories. (The way to do this depends on the OS you're using, but it's usually done by default.)

Then, when the Blender installer (in PreferencesAdd-ons) installs your add-on, it will put the subdirectory in the add-on directory with the image file in the subdirectory.

You can read Blender's Directory Layout in the manual for details on the directory layout, but what you need to know is that this code:

import bpy
from bpy.utils import resource_path
from pathlib import Path

USER = Path(resource_path('USER'))
ADDON = "my addon name"
IMAGE = "my image name"
srcPath = USER / "scripts/addons" / ADDON / "assets" / IMAGE
srcFile = str(srcPath)

will set srcFile to a string containing the name of the image in a format acceptable to the local OS. In my example, on Windows, if your user name is me, and you've installed your addon for Blender 2.93, it will produce

'C:\\Users\\me\\AppData\\Roaming\\Blender Foundation\\Blender\\2.93\\scripts\\addons\\my addon name\\assets\\my image name

Replace "my addon name" with the name you gave your addon and "my image name" with the name of the image file, including any extension (such as .jpg) and you can use the string as the image source name.

Pathlib is a standard part of Python. You can read the python manual section for pathlib if you want more details. Basically, it creates paths independent of the OS conventions and allows you to use the slash ("/") operator to put pieces of path names together in an OS independent way.

$\endgroup$
2
  • 2
    $\begingroup$ Thank you so much!! This is exactly what I was looking for. you are a life saver :) $\endgroup$
    – Gabriel
    Feb 14, 2022 at 17:04
  • $\begingroup$ You're very welcome. I look forward to seeing your add-on when it is finished. $\endgroup$ Feb 14, 2022 at 18:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .