blender as py module - Addon installation succeeds but the API is not accessible through the bpy module #56829

Closed
opened 2018-09-17 20:30:42 +02:00 by Amir · 14 comments

I have compiled Blender 2.79b as a Python3 module on Ubuntu 16.04 LTS. I install an addon using bpy.ops.wm.addon_install(), enable it using bpy.ops.wm.addon_enable(module='packageName) and save the user preferences using bpy.ops.wm.save_userpref(). However, after I open Python3 again and do import bpy I cannot see the addon API's being added to the bpy module. If I run the same exact "addon installation" script when using Blender with the GUI everything works fine, but not when I have compiled Blender as a Python module. It looks like this must be a bug so I decided to post it here.

In case you're wondering, this is the addon I'm trying to install and below is the script I used to do the installation:

installArmory.sh

wget -O armsdk.zip https://www.dropbox.com/s/a17prr2hzsjkfog/armsdk.zip?dl=1
python3 installArmory.py

installArmory.py

import bpy, os, zipfile

with zipfile.ZipFile("armsdk.zip","r") as zip_ref:
    zip_ref.extractall(os.getcwd() + '/')

# Install the addon
bpy.ops.wm.addon_install(filepath=os.getcwd() + '/armsdk/armory/blender/addon/armory.py')
bpy.ops.wm.addon_enable(module='armory')

# Set the SDK path
user_prefs = bpy.context.user_preferences
addon_prefs = user_prefs.addons['armory'].preferences
addon_prefs.sdk_path = os.getcwd() + '/armsdk'

bpy.ops.wm.save_userpref()
I have compiled Blender 2.79b as a Python3 module on Ubuntu 16.04 LTS. I install an addon using `bpy.ops.wm.addon_install()`, enable it using `bpy.ops.wm.addon_enable(module='packageName)` and save the user preferences using `bpy.ops.wm.save_userpref()`. However, after I open Python3 again and do `import bpy` I cannot see the addon API's being added to the bpy module. If I run the same exact "addon installation" script when using Blender with the GUI everything works fine, but not when I have compiled Blender as a Python module. It looks like this must be a bug so I decided to post it here. In case you're wondering, [this ](https://armory3d.org/manual/#/getting_started/setup?id=using-armory-as-add-on) is the addon I'm trying to install and below is the script I used to do the installation: **installArmory.sh** ``` wget -O armsdk.zip https://www.dropbox.com/s/a17prr2hzsjkfog/armsdk.zip?dl=1 python3 installArmory.py ``` **installArmory.py** ``` import bpy, os, zipfile with zipfile.ZipFile("armsdk.zip","r") as zip_ref: zip_ref.extractall(os.getcwd() + '/') # Install the addon bpy.ops.wm.addon_install(filepath=os.getcwd() + '/armsdk/armory/blender/addon/armory.py') bpy.ops.wm.addon_enable(module='armory') # Set the SDK path user_prefs = bpy.context.user_preferences addon_prefs = user_prefs.addons['armory'].preferences addon_prefs.sdk_path = os.getcwd() + '/armsdk' bpy.ops.wm.save_userpref() ```
Author

Added subscriber: @AmirS

Added subscriber: @AmirS

Added subscribers: @ideasman42, @mont29

Added subscribers: @ideasman42, @mont29
Campbell Barton was assigned by Bastien Montagne 2018-09-18 09:31:39 +02:00

@ideasman42 will know more here, but am pretty sure blender-as-py-module does not handle at all user preferences and such? That would not make much sense at least, imho…

@ideasman42 will know more here, but am pretty sure blender-as-py-module does not handle at all user preferences and such? That would not make much sense at least, imho…
Bastien Montagne changed title from Addon installation succeeds but the API is not accessible through the bpy module to blender as py module - Addon installation succeeds but the API is not accessible through the bpy module 2018-09-18 09:32:04 +02:00

Added subscriber: @brecht

Added subscriber: @brecht

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'

The Python module indeed always starts with "factory settings" by design. This is what you would normally expect from a programming API I think.

The Python module indeed always starts with "factory settings" by design. This is what you would normally expect from a programming API I think.
Author

@ideasman42 @brecht But it doesn't make sense not to be able to install addons if you're using the Python module. The Python module should be Blender with no GUI in my opinion and there shouldn't be any difference. Would you agree with that?
But still, is there a way to manually load a userpref.blend file with the Python module? How can one install addons when using the Python module?

@ideasman42 @brecht But it doesn't make sense not to be able to install addons if you're using the Python module. The Python module should be Blender with no GUI in my opinion and there shouldn't be any difference. Would you agree with that? But still, is there a way to manually load a `userpref.blend` file with the Python module? How can one install addons when using the Python module?

@AmirS no, blender-as-module is a library, not an application. Blender with no GUI is… Blender with no GUI (run from commandline with -b option, you can even build it with WITH_HEADLESS option to actually have no available GUI at all).

So I guess you'd have to handle extra add-ons yourself (i.e. call register helpers yourself every time you need it). Not sure many add-ons will work correctly in blender-as-module context anyway.

@AmirS no, blender-as-module is a library, not an application. Blender with no GUI is… Blender with no GUI (run from commandline with `-b` option, you can even build it with `WITH_HEADLESS` option to actually have no available GUI at all). So I guess you'd have to handle extra add-ons yourself (i.e. call register helpers yourself every time you need it). Not sure many add-ons will work correctly in blender-as-module context anyway.
Author

@mont29 I don't have an issue running Blender with the -b flag but the reason that I'm compiling Blender as Python module is that I have many other packages that I'm using in my project and things are integrated. I do import pytorch, import cv2 etc and it's not possible to disentangle things. On the other hand, I don't think it is possible to compile a heavy code base like PyTorch and add its module into the internal Python that Blender comes with.

So I would really appreciate if someone can give me some clues on how I can install an addon when using Python module-compiled version of Blender. Like how I can manually register this new addon so that its API shows up in the bpy module.

@mont29 I don't have an issue running Blender with the `-b` flag but the reason that I'm compiling Blender as Python module is that I have many other packages that I'm using in my project and things are integrated. I do `import pytorch`, `import cv2` etc and it's not possible to disentangle things. On the other hand, I don't think it is possible to compile a heavy code base like PyTorch and add its module into the internal Python that Blender comes with. So I would really appreciate if someone can give me some clues on how I can install an addon when using Python module-compiled version of Blender. Like how I can manually register this new addon so that its API shows up in the `bpy` module.
Author

@brecht I think it's totally understandable to assume that people who compile Blender as Python module would ONLY need to use the "factory setting" functionalities, but it's not a good idea not to provide an option for others to be able to install addons :(

@brecht I think it's totally understandable to assume that people who compile Blender as Python module would **ONLY** need to use the "factory setting" functionalities, but it's not a good idea not to provide an option for others to be able to install addons :(

I'm not sure I understand, doesn't bpy.ops.wm.addon_enable(module='armory') work, you just have to call it every time?

I'm not sure I understand, doesn't `bpy.ops.wm.addon_enable(module='armory')` work, you just have to call it every time?
Author

@brecht No it shows me the following error although I ran the code above to install it.

addon not found: 'armory'
{'CANCELLED'}
@brecht No it shows me the following error although I ran the code above to install it. ``` addon not found: 'armory' {'CANCELLED'} ```
Author

@brecht I think I fixed it actually but this is not a permanent solution and I'm not sure if Armory is going to work properly this way.

Here are the steps:

bpy.ops.wm.addon_install(filepath=os.getcwd() + '/armsdk/armory/blender/addon/armory.py')
bpy.ops.wm.addon_enable(module='armory')

# Set the SDK path
user_prefs = bpy.context.user_preferences
addon_prefs = user_prefs.addons['armory'].preferences
addon_prefs.sdk_path = os.getcwd() + '/armsdk'

# Use the `start()` function of `bpy.ops.arm_addon` to enable it.
bpy.ops.arm_addon.start()

Now arm shows up in bpy.ops but I don't think this could be a permanent solution. It would be great if installing addons can be more supported in a more coherent way.

@brecht I think I fixed it actually but this is not a permanent solution and I'm not sure if Armory is going to work properly this way. Here are the steps: ``` bpy.ops.wm.addon_install(filepath=os.getcwd() + '/armsdk/armory/blender/addon/armory.py') bpy.ops.wm.addon_enable(module='armory') # Set the SDK path user_prefs = bpy.context.user_preferences addon_prefs = user_prefs.addons['armory'].preferences addon_prefs.sdk_path = os.getcwd() + '/armsdk' # Use the `start()` function of `bpy.ops.arm_addon` to enable it. bpy.ops.arm_addon.start() ``` Now `arm` shows up in `bpy.ops` but I don't think this could be a permanent solution. It would be great if installing addons can be more supported in a more coherent way.

@AmirS nothing prevents you from using system python instead of 'own' blender one, just ensuer you build with system python, disable the WITH_PYTHON_INSTALL build options, and blender will use system python, and you can use any module installed there. That would be by far the simplest solution (especially on linux, where valid system python should nearly always be available).

besides, to “enable” any random add-on (or module) from any random place, something like that should work:

  • add path to that add-on/module to sys.path
  • import the add-on/module

call its register method

That’s kind of basic python module handling really…

Also please note that We do not handle user support on this tracker, only bug reports. User forums like blenderartists.org or blender.stackexchange.com should be used for that matter.

@AmirS nothing prevents you from using system python instead of 'own' blender one, just ensuer you build with system python, disable the `WITH_PYTHON_INSTALL` build options, and blender will use system python, and you can use any module installed there. That would be by far the simplest solution (especially on linux, where valid system python should nearly always be available). besides, to “enable” any random add-on (or module) from any random place, something like that should work: - add path to that add-on/module to `sys.path` - `import` the add-on/module # call its `register` method That’s kind of basic python module handling really… Also please note that We do not handle user support on this tracker, only bug reports. User forums like [blenderartists.org](http:*blenderartists.org/) or [blender.stackexchange.com](http:*blender.stackexchange.com/) should be used for that matter.
Sign in to join this conversation.
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-addons#56829
No description provided.