[Bf-python] Add-on import and enable process

Sybren A. Stüvel sybren at stuvel.eu
Wed Jun 28 09:09:52 CEST 2017


Hi James,

I've written a comment on your identical question on Stack Overflow, https://
blender.stackexchange.com/questions/82283/help-with-understanding-the-addon-enable-
process-in-blender

Best,
Sybren

On Wednesday, 28 June 2017 10:40:04 CEST James Crowther wrote:
> Hi there,
> 	I’m trying to make our add-on work when its enabled from blender starting
> up (safe as enabled in user prefs). But for some reason this doesn’t work.
> It works fine when enabled after blender has started. The add-on is
> comprised of a package with sub-packages. The parent package at the upper
> most level has its __init__ file simply import and call register on the sub
> packages as required by the environment. We have binaries in our package
> and so the sub-packages contain different builds for different operating
> systems.
> 
> The issue is that the top level __init__ file imports the sub package it
> needs when it is registered (not when its imported), it then calls the
> imported packages register method (see package structure and code below).
> The problem comes when the package is being loaded at run time, its seems
> that the package is first imported and then register is run, then
> unregister is run. This causes an issue since the cr_platform object
> doesn’t seem to exist when the package is unregistered during the enabling
> of the add-on at load time of blender.
> 
> I wasn’t really expecting the enable process for an add-on to first load
> then unload and then load the add-on again. I really need to understand
> this whole process better to be able to either work around the issue or
> maybe redesign the package if its not compatible? I thought that what I had
> built didn’t violate anything in the python blender api as far as I can
> tell.
> 
> Help would be most appreciated!!
> 
> James
> 
> 
> Package structure
> 
> top_level of package
> 
> __init__.py
> Darwin
> 	__init__.py
> 	bl_2_78
> 		lib
> 	bl_2_79
> 		lib
> Linux
> 	__init__.py
> 	bl_2_78
> 		lib
> 	bl_2_79
> 		lib
> Wind64
> 	__init__.py
> 	bl_2_78
> 		lib
> 	bl_2_79
> 		lib
> 
> 
> 
> ### contents of __init__.py at the top level
> 
> import bpy, imp, subprocess, sys, os
> 
> def select_platform():
>     """ checks python/blender versions and sets import paths accordingly
>     """
> 
> 
>     global cr_platform
> 
>     if sys.platform.startswith('darwin'):
>         from . import Darwin as cr_platform
>     elif sys.platform.startswith('linux'):
>         from . import Linux as cr_platform
>     elif sys.platform.startswith('win'):
>         from . import Win64 as cr_platform
>     else:
>         raise RuntimeError(" unsupported version of python, you have :" +\
>                 python_version +" supported versions are 3.5.2 and 3.5.3")
> 
> 
> 
> def register():
> 
>     select_platform()
> 
>     cr_platform.register()
> 
> 
> def unregister():
> 
>     if 'cr_platform' in locals():
> 
>         cr_platform.unregister()
> 
>     else:
>         raise RuntimeError("could not find the cr_platform while
> unregistering!")
> 
> 
> 
> 
> if __name__ == '__main__':
>     register()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.blender.org/pipermail/bf-python/attachments/20170628/2605ecbc/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 163 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.blender.org/pipermail/bf-python/attachments/20170628/2605ecbc/attachment.sig>


More information about the Bf-python mailing list