#tnModuleNames are just strings, tnModulesImported refer to actual modules which are imported
tnModuleNames = []
tnModulesImported = []
#If you want Docs, 3Below ;)
def getModuleNames(root, package = ''):
if not isinstance(root, Path):
root = Path(root)
absolutePath = package
for moduleDir, moduleName, ispkg in pkgutil.iter_modules([str(root)]):
#A Package is a
if ispkg:
rootNext = root / moduleName
absolutePathNext = absolutePath + moduleName + '.'
print("Found A Package:- ", moduleName)
yield from getModuleNames(rootNext, absolutePathNext)
else:
print("Found A Module:- ", moduleName)
yield absolutePath + moduleName
def importModules(addonDirName):
#normally getModuleNames yields/returns this load_and_reg file too, so we manually remove that, so that we don't have to check tnModule == __name__ ever again
#Then we Import the other Modules
for tnModuleName in tnModuleNames:
if tnModuleName == __name__:
tnModuleNames.remove(tnModuleName)
continue
else:
yield importlib.import_module('.' + tnModuleName, addonDirName)
#Consists of two steps, Loading the names of the modules in tnModuleNames, STEP 2 is to import the Modules
def loadModules(addonDir):
global tnModuleNames
global tnModulesImported
tnModuleNames = getModuleNames(addonDir)
if not isinstance(addonDir, Path):
addonDir = Path(addonDir)
addonDirName = addonDir.name
tnModulesImported = importModules(addonDirName)
def registerModules():
for tnModule in tnModulesImported:
if hasattr(tnModule, 'register'):
print("Registering module " + str(tnModule))
tnModule.register()
in my init.py I was calling loadModules(currentDir) first, then registerModules().
See those print statements inside getModuleNames() and registerModules()
I was getting outputs like this in the blender console:
Found A Package:- execution
Found A Module:- tn_execution
registering module <module *** from ***> [ *** means actual imported python module info ]
Found A Package:- nodes
Found A Module:- tn_node_base
registering module <module *** from ***>
Found A Module:- tn_nodes
registering module <module *** from ***>
Found A Module:- tn_nops
I mean How is it JUMPING Straight to registerModules function even before the loadModules has returned
[I really apologize for this big code, but this was the least I thought would be needed to understand]
Also note that, My testAddon is working as expected, no bugs so far…
but I would really like to know WHY and How it’s being executed like in this MultiThreaded Looking way when i use yield inside getModuleNames and importModules
If I don’t use yield and directly append to the tnModuleNames or tnModulesImported List, The JUMPING from loadModules to registerModules doesn’t happen