7
$\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$

1 Answer 1

8
$\begingroup$

Part 1. Your user script paths.

Type bpy.utils.script_paths( and press tab which returns

script_paths(*, subdir=None, user_pref=True, check_all=False, use_user=True)
Returns a list of valid script paths.
:arg subdir: Optional subdir.
:type subdir: string
:arg user_pref: Include the user preference script path.
:type user_pref: bool
:arg check_all: Include local, user and system paths rather just the paths
   blender uses.
:type check_all: bool
:return: script paths.
:rtype: list

script_path_user()` returns the env var and falls back to home dir or None. For example –

>>> 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$
2
  • $\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, 2015 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, 2015 at 16:15

You must log in to answer this question.

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