0
$\begingroup$

I'm trying to import a github python directory, which is not on PyPi, and I am attempting to develop an addon for blender. I cannot use the method which automatically downloads dependencies because of this, so I have to work within a folder that acts as my addon's "zip" folder.

However the alternative method to import dependencies requires a direct filepath, so I need to get the working file directory of the addon/script.py file while testing the addon in blender (so I don't have to reinstall the addon for every minor change):

path = pathlib.Path(bpy.data.filepath)
myProjects_dir = path.parent.parent.parent.resolve() 
myProjects_dir = str(myProjects_dir)
print(myProjects_dir)

but this just prints the blender install directory folder. Every variation of this using abspath or os.path.realpath, which end up either being a blank line, /, or /Script.py, with no actual path or parent directory listed.

How am I supposed to actively write a blender addon if I cannot test it in the editor with one click? Am I really expected to have to install and uninstall the addon every time?

$\endgroup$

1 Answer 1

0
$\begingroup$

You need to understand Blender's Directory Layout and how to access it from Python. Your specific case requires the USER path with the scripts/addons path appended followed by the path to your addon. Something along these lines:

import bpy
import pathlib

USER  = pathlib.Path(bpy.utils.resource_path('USER'))
ADDONS = USER / 'addons'
MYADDON = ADDONS / 'my_addon_name'  # Replace with the name of your addon

path_string = str(MYADDON)

Many examples will use os rather than pathlib but since Python 3.4 pathlib is preferred for simple path manipulation because it is object oriented and less heavy weight.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.