QUESTION: Locating Path Fragments Instead Of Fixed Paths?

Hello,

While working on my newest script, I was thinking, is there a way to write a code to trace back to the scripts .py file location to locate a path fragment? Like, if my path was C:\Blender\Script\Texture\Texture.png and the script.py was located in the Script file and the code would say to begin looking in the .py file location and try to find the path with the fragment \Texture\Texture.png

That way even if the script file location changes, it can still locate the texture within the script’s file
Like, if the script file location changed from C:\Blender\Script to C:\Blender\DownloadedFiles\Script or anything like that, it still starts in the Script file and finds the Texture file and the Texture.png within it.

Better Example/ Visual Explanation:


relative paths shouldn’t be a problem,

if your addon resides in C:\Blender\Scripts\your_addon.py,
reference the image file in script like

file = open(“Textures\Texture.png”)

Will this still work if the main file location were to change? I’m mainly having trouble with figuring out how to keep the file path from being lost when someone downloads it, since I’m using an image file for the add-on and not a procedural texture. Although, now that I think about it, I may be able to use a procedural texture instead.

I’ll let you know if this post is still relevant momentarily.

Thank you for the reply!

as long as the paths are relative to the .blend file, that should be ok even if someone else downloaded and unpacked the topmost folder.

You can also include scripts in blends, maybe that’s an option too.

Thank you!