# Help with understanding the addon enable process in blender

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
Win64
__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()

• As it is now, your code won't run. There is no bl_info dict, so your addon won't even show up to allow activation, and the global cr_platform statement will fail because there is no such variable. Make sure you post code that actually shows the problem and that we can use to reenact your problem. – dr. Sybren Jun 28 '17 at 7:08
• Hi Sybren, sorry for the late response, I understand I need to show the full code for troubleshooting. No problem, next time I'll post the entire example. Sorry, for that, a bit new to this. – jeducious Jul 11 '17 at 4:00
• Also, hitting return/enter seems to post rather than enter a new line, new to that too! ANyway, thanks for the offer of help, I've been in the dungeon working away at this and miraculously fixed it. I removed the call to raise an exception and now it works fine. Though if you can point me in the direction of any resources to learn more about blenders treatment of addons at startup vs after startup, that would ease my mind. For some reason it seems to do something different and I have no idea what that is! :D I like learning though, so feel free to send me in the right direction. – jeducious Jul 11 '17 at 4:03
• Sharing the full code isn't necessary. It's much better to show us a minimal example that shows your problem. Even creating such a minimal example can sometimes be enough to actually solve the problem. – dr. Sybren Jul 11 '17 at 7:27