3
$\begingroup$

I am developing a program that will include a custom importer and exporter for blender.

I would like the installer for my program to be able to automatically install and enable the import/export python scripts. Here are the steps I think I need to do:

  1. Find the blender add-on directory. My program is windows-only, so I presume this will just be in %appdata%\Blender Foundation
  2. Copy my add-on files to a subdirectory of the add-on directory
  3. Enable the plugin in blender
  4. Save the user preferences so that the plugin stays enabled after a restart.

I can work out 1, and 2 is easy, but I'm not sure that 3 and 4 are even possible without using the UI. Is it possible to do them programmatically, perhaps via a python script that I launch with blender?

$\endgroup$
2
$\begingroup$

Part 1. Your user script paths

>>> bpy.utils.script_path_user(
script_path_user()
returns the env var and falls back to home dir or None
>>> bpy.utils.script_path_user()
'/home/batfinger/.config/blender/2.76/scripts'

And the addons paths

>>> bpy.utils.script_paths(subdir='addons')
['/home/batfinger/src/BlenderGIT/qtcreator_build/bin/2.76/scripts/addons',
 '/home/batfinger/.config/blender/2.76/scripts/addons',
 '/home/batfinger/src/Blend4Web-master/blender_scripts/addons']

Part 3.

Look in scripts/modules/addon_utils.py

from the console

>>> from addon_utils import enable
>>> enable(
enable(module_name, default_set=False, persistent=False, handle_error=None)
Enables an addon by name.
:arg module_name: The name of the addon and module.
:type module_name: string
:return: the loaded module or None on failure.
:rtype: module

To enable my sound drivers addon for instance

enable("sound_drivers")

Check out the source, there are other handy addon utils in there.

and for part 4, there's the operator to save user prefs.

 bpy.ops.wm.save_userpref()
$\endgroup$
  • $\begingroup$ Thanks for the timely answer, it pretty much solved all my issues. I'd upvote it, but my reputation is apparently too low. $\endgroup$ – mushin Sep 29 '15 at 14:49
  • $\begingroup$ @mushin That is ok, you will soon have enough rep. For now you did your part in accepting his answer. Welcome to BSE. $\endgroup$ – David Sep 29 '15 at 16:15

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.