6
$\begingroup$

I've spend a lot of time testing several functionalities in Blender and it works great. Each functionality has some operators, methods, custom variables and one panel contained in a separate script. So I need to load each script each time I start Blender or make changes in the code.

This is ok, for testing and POC, but I would like to make it a single addon. I have searched all over the place and it seems that Blender/python is not really too glad of multiple files. I have tried the Blender Development (by Jacque Lucke) extension for VS Code, which has an auto load_and_registre function. But It gets bugged down in some path_scanning_and_modifying-rabbit hole and crashes.

Do any of you have some nice toturial or documentation? Preferably starting at the very start and going all the way to a working addon (with many files)

$\endgroup$
2

2 Answers 2

4
$\begingroup$

Every blender addon is simply a python package with some small differences. if you use multiple files in your addon (package) you should have an __init__.py file in the main folder (and also in every folder in your directory so you can import them as your local packages). blender recognizes your package as an addon, if you have bl_info (dictionary) and register and unregister functions in your __init__.py file. as described in the manual the simplest example of addons is:

bl_info = {
    "name": "My Test Add-on",
    "blender": (2, 80, 0),
    "category": "Object",
}
def register():
    print("Hello World")
def unregister():
    print("Goodbye World")

relative importing also works because its python but importing is also a little bit different in addons. you should check if bpy module is added before. for example if you have utils and draw package(folders including __init__.py files) or modules(*.py files) you should import them like this:

if "bpy" in locals():
    import importlib
    importlib.reload(utils)
    importlib.reload(draw)
else:
    from . import utils
    from . import draw

the rest is as same as every python package and depends on you!

$\endgroup$
0
$\begingroup$

You can find a list of the referenced text files associated with the project with bpy.data.texts. Access your scripts like this: bpy.data.texts['myScript.py']. You can then extract its content as a string and execute it:

exec(bpy.data.texts['myScript.py'].as_string())

Create a main script and execute all your scripts from this one. For example:

main.py

exec(bpy.data.texts['myScript1.py'].as_string())
exec(bpy.data.texts['myScript2.py'].as_string())

myScript1.py

print("1")

myScript2.py

print("1")

Output:

1
2
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .