diff options
Diffstat (limited to '__init__.py')
-rw-r--r-- | __init__.py | 98 |
1 files changed, 62 insertions, 36 deletions
diff --git a/__init__.py b/__init__.py index da57085..998f396 100644 --- a/__init__.py +++ b/__init__.py @@ -1,47 +1,73 @@ -# To load stuff that assumes a Blender scene is already loaded -# I need to wait for the BLEND file of the template to load and -# then add the functions and stuff I want after said file loads. -# The use of "bpy.app.handlers" and "persistent" help in this case -# for some reason I don't undertand yet >:] (but meh, it works) - -# References: +# references # https://s-nako.work/2020/09/blender-error-attributeerror-_restrictcontext-object-has-no-attribute-view_layer/ # https://web.archive.org/web/20210925181415/https://blenderbrew.com/custom-application-templates-in-blender/ -import bpy -from bpy.app.handlers import persistent +# blenxy module +import importlib, bpy -# define function that sets all the blenxy stuff: -# galaxy unit, custom collada exporter/importer -# custom CSV animation file (for BCK) exporter/importer +# import the submmodules needed +# all submodules have a register/unregister function +# register function is to be executed on blenxy's load +# name can be anything but I want to keep the same nomenclature +# the dummy variable passed to the register funcs is needed +# since it is passed when being assigned to the load_post list +# (seems to be None always) +from . import required_modules # install needed modules for the bundled python +from . import basic_settings # user settings blenxy has +from . import collada_superbmd_import # custom importer for SuperBMD collada files +from . import collada_superbmd_export # custom exporter for SuperBMD collada files +from . import obj_kcl_export # custom exporter for OBJ files (collision) +from . import obj_neokclcreate_import # custom importer for OBJ files (collision, NeoKCLCreate) +from . import bck_import # custom importer for SMG BCK files +from . import bck_export # custom exporter for SMG BCK files -@persistent -def set_blenxy_env(dummy): # "dummy" is a variable that is somehow - # passed to set_blenxy_env when called by - # bpy.app.handlers.load_post.append - - from . import required_modules # install needed modules for bundled python (awful) - from . import basic_settings # settings blenxy has - from . import collada_superbmd_import # custom importer for SuperBMD collada files - from . import collada_superbmd_export # custom exporter for SuperBMD collada files - from . import obj_kcl_export # custom exporter for OBJ files (Collision) - from . import obj_neokclcreate_import # custom importer for OBJ files (Colllision, NeoKCLCreate) - from . import bck_import # custom importer for SMG BCK files - from . import bck_export # custom exporter for SMG BCK files +# global variables +blenxy_modules = [required_modules, + basic_settings, + collada_superbmd_import, + collada_superbmd_export, + obj_kcl_export, + obj_neokclcreate_import, + bck_import, + bck_export] + +# function used to refresh blenxy's used modules +# so that they can be updated without closing blender +@bpy.app.handlers.persistent +def unload_blenxy_stuff(dummy): - # more scripts can be added here - # ... + # execute the unregister functions from each module + # these functions won't die if the stuff to unregister isn't there + for mod in blenxy_modules: + mod.unregister() + # reload the modules in case they were modified (dev stuff) + for mod in blenxy_modules: + importlib.reload(mod) -# register/unregister stuff -# for set_blenxy_env function +# register function that blender will call when the template is loaded def register(): - print("\nWelcome to Blenxy!\n") - print("Setting environment...") - bpy.app.handlers.load_post.append(set_blenxy_env) + # print the welcome + print("\nWelcome to Blenxy!\n") + # add this function to load_post first + bpy.app.handlers.load_post.append(unload_blenxy_stuff) + # add the register functions to load_post (old_blenxy_modules) + for mod in blenxy_modules: + bpy.app.handlers.load_post.append(mod.register) + +# unregister function that blender will call when a new template is loaded def unregister(): - print("See you later!") - bpy.app.handlers.load_post.remove(set_blenxy_env) -if __name__ == "__main__": - register() + # print the goodbye + print("\nLeaving Blenxy!\n") + # remove the stuff added to the load_post list + bpy.app.handlers.load_post.remove(unload_blenxy_stuff) + for mod in blenxy_modules: + for func in bpy.app.handlers.load_post: + if (mod.__name__ in func.__module__): + bpy.app.handlers.load_post.remove(func) + break + + # execute module's unregister functions + for mod in blenxy_modules: + mod.unregister() |