1
$\begingroup$

I've created a small addon, which wrap a much bigger addon. In the execute code, I want first to get the addon' path, add it to python path, and then import my main class and call its main function:

user_preferences = context.user_preferences
addon_prefs = user_preferences.addons[__name__].preferences
addon_root = os.path.abspath(addon_prefs.addon_folder)
sys.path.append(addon_root)
import main_addon
main_addon.main(addon_prefs)

As you can see, for now I've let the user to set the addon's path in addon_prefs.addon_folder, but I want to find it automatically. Can I get the full path of my wrapper addon (both the wrapper addon and my main addon exist in the same folder), and get the folder from there? When I've tried

os.path.dirname(os.path.realpath(__file__))

I got the ".../config/blender/2.76/scripts/addons" folder, which isn't the original folder.

$\endgroup$
  • $\begingroup$ If the code above is in __init__.py in its own subfolder of the addons dir, (ie with the folder as a module) then os.path.dirname(__file__) will give you that subfolder, eg "/blender/2.77/scripts/addons/sound_drivers". Have a look at how some of the folder addons are set up using __init__.py $\endgroup$ – batFINGER Mar 30 '16 at 16:43

Your Answer

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

Browse other questions tagged or ask your own question.